-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
120 lines (103 loc) · 3.67 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Simple Chess Game in Python
class Piece:
def __init__(self, color):
self.color = color
def get_possible_moves(self, board, position):
raise NotImplementedError("This method should be implemented by subclasses.")
class Pawn(Piece):
def get_possible_moves(self, board, position):
# Implement pawn-specific movement logic
pass
class Rook(Piece):
def get_possible_moves(self, board, position):
# Implement rook-specific movement logic
pass
class Knight(Piece):
def get_possible_moves(self, board, position):
# Implement knight-specific movement logic
pass
class Bishop(Piece):
def get_possible_moves(self, board, position):
# Implement bishop-specific movement logic
pass
class Queen(Piece):
def get_possible_moves(self, board, position):
# Implement queen-specific movement logic
pass
class King(Piece):
def get_possible_moves(self, board, position):
# Implement king-specific movement logic
pass
class Board:
def __init__(self):
self.board = self.setup_board()
def setup_board(self):
# Initialize the board with pieces in their starting positions
board = [[None for _ in range(8)] for _ in range(8)]
# Place pawns
for i in range(8):
board[1][i] = Pawn('black')
board[6][i] = Pawn('white')
# Place other pieces
placement = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook]
for i, piece_class in enumerate(placement):
board[0][i] = piece_class('black')
board[7][i] = piece_class('white')
return board
def print_board(self):
for row in self.board:
print(" | ".join([self.get_piece_symbol(piece) for piece in row]))
print("-" * 33)
def get_piece_symbol(self, piece):
if piece is None:
return " "
symbols = {
Pawn: 'P', Rook: 'R', Knight: 'N',
Bishop: 'B', Queen: 'Q', King: 'K'
}
color = 'w' if piece.color == 'white' else 'b'
return color + symbols[type(piece)]
def move_piece(self, from_pos, to_pos):
fx, fy = from_pos
tx, ty = to_pos
piece = self.board[fy][fx]
if piece and self.is_valid_move(piece, from_pos, to_pos):
self.board[ty][tx] = piece
self.board[fy][fx] = None
return True
else:
return False
def is_valid_move(self, piece, from_pos, to_pos):
# Implement move validation logic
return True
def parse_position(pos_str):
columns = {'a': 0, 'b': 1, 'c': 2, 'd': 3,
'e': 4, 'f': 5, 'g': 6, 'h': 7}
try:
col = columns[pos_str[0].lower()]
row = 8 - int(pos_str[1])
return (col, row)
except (KeyError, ValueError, IndexError):
return None
def main():
board = Board()
current_player = 'white'
while True:
board.print_board()
print(f"{current_player.capitalize()}'s turn.")
move = input("Enter your move (e.g., e2 e4): ")
try:
from_str, to_str = move.strip().split()
from_pos = parse_position(from_str)
to_pos = parse_position(to_str)
if from_pos and to_pos:
if board.move_piece(from_pos, to_pos):
current_player = 'black' if current_player == 'white' else 'white'
else:
print("Invalid move. Try again.")
else:
print("Invalid input. Use the format 'e2 e4'.")
except ValueError:
print("Invalid input. Use the format 'e2 e4'.")
if __name__ == "__main__":
main()