-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
121 lines (99 loc) · 4.32 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
import pygame
from CheckerGame import CheckerBoard, Piece
from CheckerAI import CheckerAI
from Transition import BoardTransition
from constants import HEIGHT, WIDTH, _P1PIECE, _P2PIECE, _FORCED_CAPTURE, Q_TABLE_FILE, DEBUG_HEIGHT
if __name__ == "__main__":
window = pygame.display.set_mode((WIDTH, HEIGHT + DEBUG_HEIGHT))
clock = pygame.time.Clock()
ai = CheckerAI("")
bt = BoardTransition()
CB = CheckerBoard()
player = _P2PIECE
replayable = True
gameActive = True
winnerMessage = ""
while(replayable):
CB.initializeBoard()
# print(CB)
nextBoardStates = bt.getAllBoards(CB)
# Treat as pointer to a piece
selectedPiece:Piece = None
currentBoardEval = 0
repeatedMoves = dict()
while gameActive:
CB.drawBoard(window)
clock.tick(60)
time = pygame.time.get_ticks()
# if its players turn
if (CB.turn == player):
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameActive = False
replayable = False
if event.type == pygame.MOUSEBUTTONDOWN:
if (selectedPiece is None):
selectedPiece = CB.selectPiece()
else:
if (CB.placePiece(selectedPiece)):
nextBoardStates = bt.getAllBoards(CB)
wonPlayer = CB.gameEnd(len(nextBoardStates))
if (wonPlayer == player * -1):
print("AI Won!")
winnerMessage = "AI Won!"
gameActive = False
elif (wonPlayer == player):
print("You Won!")
winnerMessage = "You Won!"
gameActive = False
selectedPiece = None
if (selectedPiece is not None):
selectedPiece.drawOutline(window)
currentBoardEval = ai.evaluateBoard(CB)
# if its AI's turn
else:
depth = 4
# depth = int(180/(CB.player1NumPieces + CB.player2NumPieces + 36))
nextBestMove = ai.nextBestMove(CB, depth)
CB < nextBestMove
currentBoardEval = ai.evaluateBoard(CB)
# Game Draw logic
if (repeatedMoves.get(nextBestMove) is not None):
if (repeatedMoves[nextBestMove] >= 3):
print("Boards repeated resulted in a draw!")
winnerMessage = "Its a draw!"
gameActive = False
else:
repeatedMoves[nextBestMove] += 1
else:
repeatedMoves[nextBestMove] = 1
nextBoardStates = bt.getAllBoards(CB)
wonPlayer = CB.gameEnd(len(nextBoardStates))
if (wonPlayer == player * -1):
print("AI Won!")
winnerMessage = "AI Won!"
ai.applyQReward(wonPlayer)
gameActive = False
elif (wonPlayer == player):
print("You Won!")
winnerMessage = "You Won!"
ai.applyQReward(wonPlayer)
gameActive = False
# Monitoring Performance
# clock.tick()
# print(clock.get_fps())
CB.drawPieces(window)
CB.debug(window, currentBoardEval, "Current Board Evaluation")
pygame.display.update()
# Continuation Logic
CB.drawBoard(window)
CB.debug(window, currentBoardEval, winnerMessage + " " + "Click Here to Play again!")
for event in pygame.event.get():
if event.type == pygame.QUIT:
replayable = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX, mouseY = pygame.mouse.get_pos()
if (mouseY > HEIGHT and mouseY < HEIGHT + DEBUG_HEIGHT):
gameActive = True
pygame.display.update()
pygame.quit()