-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
87 lines (77 loc) · 3.34 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
84
85
86
87
from Utils.Player import *
from AI.MinMaxLocalSearch import *
class Game:
def __init__ (self,chess,board, gamemode):
self.chess = chess
self.board = board
self.gamemode = gamemode
self.player1 = Player('1','white')
self.player2 = Player('2','black')
self.turn = self.player1
self.chessboot = MinMaxLocalSearch()
def start_game(self):
if self.gamemode == "1":
self.chess.board.bind("<Button-1>", self.click_handler_2_player)
elif self.gamemode == "2":
self.chess.board.bind("<Button-1>", self.click_handler_bot)
else:
print("Invalid game mode entred")
exit(0)
def click_handler_bot(self,event):
if self.turn.equals(self.player1):
posX = self.chess.size_to_x_coordinates(event.x)
posY = self.chess.size_to_y_coordinates(event.y)
if self.board.containsPlayerPice(posX, posY, self.turn) and not self.board.isMoveCell(posX,posY):
self.clean_board_moves()
self.board.actualizeMoves(posX, posY)
elif (self.board.isMoveCell(posX,posY) and not self.board.containsPlayerPice(posX, posY, self.turn)) or (self.board.containsPlayerPice(posX, posY, self.turn) and self.board.isMoveCell(posX,posY)):
move = self.board.board[posY][posX]['m']
self.board.movePice(move)
self.clean_board_moves()
self.changeTurn()
else:
self.clean_board_moves()
self.reset_board()
if self.check_end_of_game():
self.chess.window.destroy()
if self.turn.equals(self.player2):
self.board=self.chessboot.bot_move(self.board)
self.changeTurn()
self.reset_board()
if self.check_end_of_game():
self.chess.window.destroy()
def click_handler_2_player(self,event):
posX = self.chess.size_to_x_coordinates(event.x)
posY = self.chess.size_to_y_coordinates(event.y)
if self.board.containsPlayerPice(posX, posY, self.turn) and not self.board.isMoveCell(posX,posY):
self.clean_board_moves()
self.board.actualizeMoves(posX, posY)
elif self.board.isMoveCell(posX,posY) and not self.board.containsPlayerPice(posX, posY, self.turn):
move = self.board.board[posY][posX]['m']
self.board.movePice(move)
self.clean_board_moves()
self.changeTurn()
elif self.board.containsPlayerPice(posX, posY, self.turn) and self.board.isMoveCell(posX,posY):
move = self.board.board[posY][posX]['m']
self.board.movePice(move)
self.clean_board_moves()
self.changeTurn()
else:
self.clean_board_moves()
if self.check_end_of_game():
self.chess.window.destroy()
self.reset_board()
def changeTurn(self):
if self.turn.equals(self.player1):
self.turn = self.player2
else:
self.turn = self.player1
def reset_board(self):
self.chess.create_board()
self.chess.display_pices(self.board)
def clean_board_moves(self):
for row in self.board.board:
for cell in row:
cell['m'] = ''
def check_end_of_game(self):
return not self.board.check_kings_alive()