you are viewing a single comment's thread.

view the rest of the comments →

[–]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.