#๐Ÿ”’ Help with Classes and Chess

8 messages ยท Page 1 of 1 (latest)

broken knollBOT
#

@plucky terrace

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

plucky terrace
#

I would like some help with the syntax for calling functions and with refining my program. I might be overcomplicating things.

#
class ChessVar():
    """Represents an abstract board game variant of chess."""

    def __init__(self):
        """Initializes the abstract board game variant of chess."""

#    def get_game_state(self):
#        if:
#            return "UNFINISHED"
#        if:
#            return "WHITE_WON"
#        if:
#            return "BLACK_WON"

    def get_x_y(self, square):
        """Returns the name of the piece in the square parameter."""
        x_pos = square[0]
        y_pos = square[1]
        return x_pos, y_pos

    def get_piece_in_position(self, x_pos, y_pos):
        """Returns the name of the piece in the x_pos and y_pos parameters."""
        for piece in piece_list:
            square = piece.get_position()
            if square[0] == x_pos and square[1] == y_pos:
                return piece.get_name()

#    def get_turn(self):
#        """Returns the number of turns since the beginning of the game."""
#        return turn_number

    def convert_x(self, square):
        """Converts the x coordinate letter into a number."""
        x_pos = square[0]
        if x_pos == "a":
            return 1
        if x_pos == "b":
            return 2
        if x_pos == "c":
            return 3
        if x_pos == "d":
            return 4
        if x_pos == "e":
            return 5
        if x_pos == "f":
            return 6
        if x_pos == "g":
            return 7
        if x_pos == "h":
            return 8



#
    def make_move(self, square_start, square_end):
        """Makes the specified move given in the square_start and _square end parameters."""
        name = get_piece_in_position(get_x_y)
        if "Pawn" in name:
            if convert_x(square_start[0]) - convert_x(square_end[0]) == -1 or square_start[0] - square_end[0] == 1 and square_start[1] - square_end[1] == -1 or square_start[1] - square_end[1] == 1:

        #If the square being moved from does not contain a piece belonging to the player whose turn it is
        # or
        #if the indicated move is not legal
        # or
        if get_game_state() != "UNFINISHED": # the game has already been won
            return False
        #else make the indicated move
            #if opposing piece is on that square already, remove it
                #if opposing piece removed was a king
                    #update gamestate
            #update turn counter
            #return True

#    def enter_fairy_piece(self, piece, square_entered):

    def print_board(self):
        print("""   A B C D E F G H
    8  . . . . . . . .  8
    7  . . . . . . . .  7
    6  . . . . . . . .  6
    5  . . . . . . . .  5
    4  . . . . . . . .  4
    3  . . . . . . . .  3
    2  . . . . . . . .  2
    1  . . . . . . . .  1
       A B C D E F G H""")





#
class ChessPiece:
    """Represents a piece in the abstract board game variant of chess."""

    def __init__(self, name, x_pos, y_pos):
        """Initializes a piece in the abstract board game variant of chess."""
        self._name = name
        self._x_pos = x_pos
        self._y_pos = y_pos

    def get_name(self):
        """Returns the name of a piece."""
        return self._name

    def get_position(self):
        """Returns the position of a piece."""
        return self._x_pos, self._y_pos


P1 = ChessPiece("White Pawn 1", "a", "2")
P2 = ChessPiece("White Pawn 2", "b", "2")
P3 = ChessPiece("White Pawn 3", "c", "2")
P4 = ChessPiece("White Pawn 4", "d", "2")
P5 = ChessPiece("White Pawn 5", "e", "2")
P6 = ChessPiece("White Pawn 6", "f", "2")
P7 = ChessPiece("White Pawn 7", "g", "2")
P8 = ChessPiece("White Pawn 8", "h", "2")

R1 = ChessPiece("White Rook 1", "a", "1")
R2 = ChessPiece("White Rook 2", "h", "1")

K1 = ChessPiece("White Knight 1", "b", "1")
K2 = ChessPiece("White Knight 2", "g", "1")

B1 = ChessPiece("White Bishop 1", "c", "1")
B2 = ChessPiece("White Bishop 1", "f", "1")

K = ChessPiece("White King 1", "d", "1")

Q = ChessPiece("White Queen 1", "e", "1")

piece_list = [B1, B2, K, K1, K2, P1, P2, P3, P4, P5, P6, P7, P8, R1, R2, Q]

print(K.get_position())
print(get_piece_in_position())
print_board()
#

I'm specifically focusing on make_move() and the supporting functions, get_piece_in_position, get_x_y, and convert_x.

broken knollBOT
#

@plucky terrace

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.