0% found this document useful (0 votes)
16 views2 pages

Class ChessGame

The document defines a ChessGame class that initializes a chessboard and manages player turns. It includes methods for displaying the board, making moves, and playing the game interactively. The valid moves for each chess piece are predefined, and the game continues until terminated by the user.

Uploaded by

roeckenssseth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Class ChessGame

The document defines a ChessGame class that initializes a chessboard and manages player turns. It includes methods for displaying the board, making moves, and playing the game interactively. The valid moves for each chess piece are predefined, and the game continues until terminated by the user.

Uploaded by

roeckenssseth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

class ChessGame:

def __init__(self):

[Link] = [

["rook", "knight", "bishop", "queen", "king", "bishop", "knight", "rook"],

["pawn", "pawn", "pawn", "pawn", "pawn", "pawn", "pawn", "pawn"],

[" ", " ", " ", " ", " ", " ", " ", " "],

[" ", " ", " ", " ", " ", " ", " ", " "],

[" ", " ", " ", " ", " ", " ", " ", " "],

[" ", " ", " ", " ", " ", " ", " ", " "],

["pawn", "pawn", "pawn", "pawn", "pawn", "pawn", "pawn", "pawn"],

["rook", "knight", "bishop", "queen", "king", "bishop", "knight", "rook"]

self.player_turn = 1

self.valid_moves = {

"rook": [[-1, 0], [1, 0], [0, -1], [0, 1]],

"knight": [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]],

"bishop": [[-1, -1], [-1, 1], [1, -1], [1, 1]],

"queen": [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]],

"king": [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]],

"pawn": [[-1, 1], [-1, -1], [1, 1], [1, -1]] if self.player_turn == 1 else [[1, 1], [1, -1], [-1, 1], [-1, -1]]

def display_board(self):

for row in [Link]:

print(" ".join(row))

def make_move(self, start, end):

x0, y0 = start

x1, y1 = end

piece = [Link][y0][x0]

move_directions = self.valid_moves[piece]
is_valid_move = False

for direction in move_directions:

dx, dy = direction

if (x1 == x0 + dx) and (y1 == y0 + dy):

is_valid_move = True

break

if is_valid_move:

[Link][y1][x1] = [Link][y0][x0]

[Link][y0][x0] = " "

self.player_turn = 2 if self.player_turn == 1 else 1

else:

print("Invalid move. Try again.")

def play_game(self):

while True:

self.display_board()

print(f"Player {self.player_turn}'s turn.")

start = list(map(int, input("Enter the starting coordinates: ").split()))

end = list(map(int, input("Enter the ending coordinates: ").split()))

self.make_move(start, end)

if __name__ == "__main__":

game = ChessGame()

game.play_game()

You might also like