-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCannonMain.py
152 lines (125 loc) · 5.11 KB
/
CannonMain.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pygame
from AIAlgorithms.IterativeAI import IterativeAI
from AIAlgorithms.MinimaxAI import Minimax, MinimaxAB, MinimaxABTT
from AIAlgorithms.NegamaxAI import Negamax, NegamaxAB, NegamaxABTT
from AIAlgorithms.RandomAI import findRandomMove
from GameEngine import CannonEngine
from GUI.GUIHelperFunctions import drawGameState, drawText, loadImages
from GUI.GUIVariables import max_fps, square_size, surface_size
def main():
pygame.init()
font = pygame.font.SysFont('Courier', 20)
surface = pygame.display.set_mode((surface_size, surface_size))
clock = pygame.time.Clock()
gs = CannonEngine.GameState()
possibleMoves = gs.getAllPossbileMoves()
moveMade = False
loadImages()
running = True
gameOver = False
sqSelected = ()
playerClicks = []
redIsPerson = False
# redIsPerson = True
blackIsPerson = False
# blackIsPerson = True
while running:
if len(possibleMoves) == 0:
gs.noMoveLeft = True
personTurn = (gs.redToMove and redIsPerson) or (
not gs.redToMove and blackIsPerson)
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
running = False
break
elif ev.type == pygame.KEYDOWN:
key = ev.dict['key']
if key == ord('z'):
if redIsPerson == True and blackIsPerson == True:
gs.undoMove()
if redIsPerson == False or blackIsPerson == False:
gs.undoMove()
gs.undoMove()
moveMade = True
gameOver = False
if key == ord('r'):
gs = CannonEngine.GameState()
possibleMoves = gs.getAllPossbileMoves()
moveMade = False
sqSelected = ()
playerClicks = []
gameOver = False
elif ev.type == pygame.MOUSEBUTTONDOWN:
if not gameOver and personTurn:
if ev.button == 3:
sqSelected = ()
playerClicks = []
continue
location = pygame.mouse.get_pos()
col = (location[0] + 30) // square_size - 1
row = (location[1] + 30) // square_size - 1
if row == -1 or col == -1 or row == 10 or col == 10:
sqSelected = ()
playerClicks = []
continue
if sqSelected == (row, col):
sqSelected = ()
playerClicks = []
else:
sqSelected = (row, col)
playerClicks.append(sqSelected)
if len(playerClicks) == 2:
move = CannonEngine.Move(
playerClicks[0], playerClicks[1], gs.board)
print(move.getCannonNotation())
moveFound = False
for possibleMove in possibleMoves:
if move.moveID == possibleMove.moveID:
gs.makeMove(possibleMove)
moveMade = True
sqSelected = ()
playerClicks = []
moveFound = True
if not moveFound:
playerClicks = [sqSelected]
if not personTurn and not gs.noMoveLeft:
# AIMove = AI.findRandomMove(possibleMoves)
maxDepth = 3
maxTime = 1.0
# AIEngine = Minimax(maxDepth)
# AIEngine = MinimaxAB(maxDepth)
AIEngine = MinimaxABTT(maxDepth)
# AIEngine = Negamax(maxDepth)
# AIEngine = NegamaxAB(maxDepth)
# AIEngine = NegamaxABTT(maxDepth)
# AIEngine = IterativeAI(maxTime)
AIMove = AIEngine.findBestMove(gs, possibleMoves)
if AIMove == None:
if gs.redToMove:
print("red AI moves random")
else:
print("black AI moves random")
# print(gs.board)
AIMove = findRandomMove(possibleMoves)
gs.makeMove(AIMove)
moveMade = True
if moveMade:
possibleMoves = gs.getAllPossbileMoves()
moveMade = False
drawGameState(surface, gs, font, possibleMoves, sqSelected)
if gs.townCapture:
gameOver = True
if gs.redToMove:
drawText(surface, font, 'Black captured the town')
else:
drawText(surface, font, 'Red captured the town')
elif gs.noMoveLeft:
gameOver = True
if gs.redToMove:
drawText(surface, font, 'No moves left for red')
else:
drawText(surface, font, 'No moves left for black')
clock.tick(max_fps)
pygame.display.flip()
if __name__ == '__main__':
main()