forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
83 lines (65 loc) · 2.97 KB
/
game.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
from piece import Piece ,Pawn ,Rook ,Bishop ,Queen ,King ,Knight
from board import Board
class Game:
def __init__(self ,color):
self.board = Board()
self.current_color = color
self.is_over = False
def switch_turns(self):
self.current_color = 'black' if self.current_color == "white" else "white"
def is_checkmate(self ,color:str) -> bool:
pass
def is_in_check(self ,color:str) -> bool:
king_position = None
""" finding king's position """
for i in range(8):
for j in range(8):
piece = self.board.board[i][j]
if piece is not None and isinstance(piece,King) and piece.color == color:
king_position = (i,j)
break
opponent_color = 'black' if color == 'white' else 'white'
for i in range(8):
for j in range(8):
piece = self.board.board[i][j]
if piece is not None and piece.color == opponent_color:
if piece.is_valid_move((i,j),king_position,self.board):
return True # king is in check
return False
def play(self):
while not self.is_over:
self.board.print_board()
print(f"{self.current_color.capitalize()} is playing")
start_position = self.get_user_input("Enter the start position (row ,col)")
if self.board.board[start_position[0]][start_position[1]] is not None:
piece = self.board.board[start_position[0]][start_position[1]]
print(f"{piece.color} {piece.__class__.__name__} selected")
end_position = self.get_user_input("Enter the end position (row ,col)")
if self.board.move_pieces(start_position ,end_position):
if self.check_game_(self.current_color):
self.is_over = True
self.switch_turns()
else:
print("Invalid move.. Please Take Another move")
def get_user_input(self, prompt: str) -> tuple[int, int]:
"""take the input from user and convert to a tuple of integers."""
while True:
try:
user_input = input(prompt)
row, col = map(int, user_input.split(","))
if 0 <= row < 8 and 0 <= col < 8:
return (row, col)
else:
print("Invalid input.. Enter values between 0 and 7.")
except ValueError:
print("Invalid input format.. Use row,col format.")
def check_game_(self ,color:str) -> bool:
""" """
if self.is_checkmate(color):
pass
if self.is_in_check(color):
print(f"{color} King is in check")
return False
if __name__ == "__main__":
object = Game(color=input('Select color : black or white '))
object.play()