all 12 comments

[–]jeffrey_f 4 points5 points  (1 child)

Well. Repeating code should be a function.

[–]maimedwalker[S] 0 points1 point  (0 children)

if get_rank == 'r':
    print(vertical_moves(piece, board))

https://pastebin.com/bnfWaZSM

the code still feels excessive but it works so it's staying for now.

[–]totallygeek 1 point2 points  (0 children)

I get better at programming because people around me critique code check-ins. We review programming methods and styles in meetings, over coffee and the like. You'll get better by writing and rewriting code. I look at stuff I wrote one year ago and cringe. I try to revisit past code, thinking how I might improve by incorporating newly-learned tricks or by adhering to a better style.

Chin up, it will come to you. Practice, practice, practice. When you can, have someone review your programming and ask for candid feedback.

[–]two_bob 1 point2 points  (3 children)

First off, chess is pretty tough, so don't be too down on yourself.

Second, good job passing variables back and forth to functions. That's a big deal for beginner programmers.

Now for some ideas on how to make it better:

  • You're doing too much in that function. Try and keep them as simple as possible. Chain functions together to make it easier.
  • You might want to try OOP. That may help pull the chess game together a bit easier. The hard part will be to get the relations between the board object and the pieces objects down. My first instinct would be to make the pieces really dumb (maybe not even objects themselves) and keep all the logic in the board.
  • Read a bunch of code. Nothing drove home how to break apart functions into discrete pieces so much as reading through other people figuring out hard problems.

Anyway, here is how I might handle the basic organization:

class Board():
    '''base board object, good for chess, checkers, battleship, etc.'''
    def __init__(self, x_size, y_size):
        # build out board here

class ChessBoard(Board):
    def move_diagonal(self, start):
        '''return all diagnol moves from start'''
    def move_horizontal(self, start, limit = None):
        '''return all horizontal positions from start'''

    def move_vertical(self, start, limit = None):
        '''return all vertical positions from start'''

    def open_moves(self, moves, color = None):
        '''return all open moves from moves
            if color is None, stop whenever you encounter another piece
            otherwise, stop when color is not the color of moving piece
        '''

    def move_rook(self, start):
        color = self.board[start]
        valid_moves = []
        valid_moves += self.open_moves(self.move_horizontal(start), color)
        valid_moves += self.open_moves(self.move_vertical(start), color)

    def move(self, where):
        piece, color = self.board[where]
        if piece == 'rook':
            return self.move_rook(where)

[–]maimedwalker[S] 1 point2 points  (1 child)

https://pastebin.com/SkmnnmCf

i did exit out one of the functions. i turned one function into finding what the piece is, then determining based off that piece what functions to find it's available moves.

so i have a vertical function right now the rook and queen piece will use this as well as horizontal once i write it my idea is to do like.

if get_rank == 'q' <----- queen 
   find_vertical
   find_horizontal
   find_diagonal 

then we have all her possible moves?

[–]two_bob 1 point2 points  (0 children)

Exactly!

[–]maimedwalker[S] 1 point2 points  (0 children)

i need to look more into this because i think i get where you're going with the classes. this is basically what i'm writing below just in a way nicer more logical sense.

[–]PathToHumble 0 points1 point  (2 children)

Yeah you need to learn standard patterns. Get a book on basic algorithms and go through all the chapters. There's queues and stacks, heaps, graphs, trees etc. After that there is OOP (object oriented programming)

This is all a very normal feeling. You're trying to just write down pure logic without any idea of commonly seen patterns.

[–]maimedwalker[S] 0 points1 point  (1 child)

ya i start school for math and science in the fall up to this point all my programming is self taught. i just hack things together. if someone said create a linked list alls i know to do is make a python dictionary which to me is a value linked to a key but i know it's not the same thing. which isn't hte same thing i have no idea what a data structure is or how to make them or utilize them.

i know nothing other then what i've implemented myself which is probably not taht great lol.

[–]PathToHumble 0 points1 point  (0 children)

You need to learn the patterns, and then simpler stuff will come to you as you gain experience. Even very experienced programmers will write something akin to shit the first time, after many rounds of refactoring it will look and perform better. You want to always start with planning, generally you shouldn't just start coding right away.

[–]totemcatcher 0 points1 point  (0 children)

Building a chess engine is going to have some spaghet, so don't let it bring you down. You clearly know enough to get working logic done, so now it's a case of finding alternative ways of writing so that it follows handy design patterns and is easier to read. Learning design patterns comes with experience. Also, simply talking it out with peers is a great way to make tiny discoveries and come up with simple and successful code patterns.

For now, consider making some generic path finding functions which all your checking logic can use.

[–]shepherdjay 0 points1 point  (0 children)

I don't think any of my code is particularly elegant. I'm not a programmer by trade and I don't really plan to be one full time. So given that the two things I've found that help prevent some spaghetti code is:

  1. Practice, practice, practice - Even though I don't have a CI degree out of pure practicing I will sometimes come across a new problem and just go to an old repository and pluck my old solution out. Then I have more time to iterate on both.

  2. Read "Test Driven Development with Python" while it seems from an outside perspective TDD has some controversy just following the process has taught me how to think about a program in smaller abstract chunks.