-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
171 lines (135 loc) · 5.98 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import copy
import pygame
import time
from statistics import mean, median
import helper
import algorithms as alg
from Game import Game
from State import State
import gameevents
def multiplayer_game_loop():
pMoves = [0, 0]
i = 0
startTime = int(round(time.time() * 1000))
while Game.running:
hexgame.drawBoard()
events = pygame.event.get()
doneTurn = gameevents.handleEvents(events, hexgame, currentState)
if doneTurn:
endTime = int(round(time.time() * 1000))
print('{} has thought for {} miliseconds'.format(Game.currentPlayer.capitalize(), endTime - startTime))
Game.currentPlayer = Game.otherPlayer(Game.currentPlayer)
pMoves[i] += 1
print()
print(hexgame.text)
i = (i + 1) % 2
return pMoves, int(round(time.time() * 1000))
def singleplayer_game_loop():
pMoves = 0
cMoves = 0
cThinkingTimes = []
noGenNodes = []
startTime = int(round(time.time() * 1000))
while Game.running:
if currentState.currentPlayer == Game.JMIN:
hexgame.drawBoard()
events = pygame.event.get()
doneTurn = gameevents.handleEvents(events, hexgame, currentState)
if doneTurn:
endTime = int(round(time.time() * 1000))
Game.currentPlayer = Game.otherPlayer(Game.currentPlayer)
pMoves += 1
print('Player has thought for {} miliseconds'.format(endTime - startTime))
print()
print(hexgame.text)
else:
hexgame.drawBoard()
startTime = int(round(time.time() * 1000))
currentState.board.matrix = copy.deepcopy(hexgame.matrix)
currentState.depth = Game.MAX_DEPTH
if algorithm == 'minimax':
updatedState = alg.minimax(currentState)
else:
updatedState = alg.alpha_beta(-500, 500, currentState)
endTime = int(round(time.time() * 1000))
cMoves += 1
Game.currentPlayer = Game.otherPlayer(Game.currentPlayer)
cThinkingTimes.append(endTime - startTime)
currentState.board = updatedState.chosenState.board
hexgame.emptyTiles -= 1
hexgame.matrix = copy.deepcopy(currentState.board.matrix)
hexgame.showMatrix()
print('Computer has thought for {} miliseconds'.format(endTime - startTime))
print('The computer\'s estimation:', updatedState.score)
print('Number of nodes generated by the algorithm: {}'.format(alg.noOfNodes))
print()
noGenNodes.append(alg.noOfNodes)
alg.noOfNodes = 0
for i in range(len(hexgame.matrix)):
for j in range(len(hexgame.matrix[i])):
if hexgame.matrix[i][j] == hexgame.JMAX[0].upper():
for tile in hexgame.hexTiles():
if tile.gridPosition == (j, i):
tile.colour = hexgame.playerColours[hexgame.JMAX]
hexgame.matrix[i][j] = hexgame.JMAX[0].lower()
hexgame.solution = hexgame.findSolution()
if not hexgame.gameOver():
hexgame.text = '{}\'s turn'.format(hexgame.JMIN.capitalize())
else:
hexgame.text = 'Game over! {} wins!'.format(hexgame.JMAX.capitalize())
print(hexgame.text)
currentState.currentPlayer = Game.otherPlayer(currentState.currentPlayer)
startTime = int(round(time.time() * 1000))
return pMoves, cMoves, cThinkingTimes, noGenNodes, int(round(time.time() * 1000))
if __name__ == '__main__':
programStartTime = int(round(time.time() * 1000))
pygame.init()
icon = pygame.image.load('hex.png')
pygame.display.set_caption("Hex Game © Diana Elena Vasiliu")
pygame.display.set_icon(icon)
hexgame = Game()
display = pygame.display.set_mode(size=hexgame.screenSize)
hexgame.initialiseGame(display, hexgame)
Game.JMIN, algorithm, difficulty, gameType = helper.homePage(hexgame, display)
Game.JMAX = 'red' if Game.JMIN == 'blue' else 'blue'
if difficulty == 'beginner':
Game.MAX_DEPTH = 1
elif difficulty == 'medium':
Game.MAX_DEPTH = 3
elif difficulty == 'advanced':
Game.MAX_DEPTH = 5
print('Start')
hexgame.showMatrix()
print(hexgame.text)
currentState = State(hexgame, 'red', hexgame.MAX_DEPTH)
computerThinkingTimes = []
playerMoves = 0
computerMoves = 0
Game.currentPlayer = 'red'
if gameType == 'pvp':
playerMoves, programFinalTime = multiplayer_game_loop()
print()
print("The Red player moved {} times.".format(playerMoves[0]))
print("The Blue player moved {} times.".format(playerMoves[1]))
else:
playerMoves, computerMoves, computerThinkingTimes, noOfGeneratedNodes, programFinalTime = \
singleplayer_game_loop()
if not computerThinkingTimes:
computerThinkingTimes = [0]
if not noOfGeneratedNodes:
noOfGeneratedNodes = [0]
print()
print("Minimum computer think time:", min(computerThinkingTimes))
print("Maximum computer think time:", max(computerThinkingTimes))
print("Average computer think time:", mean(computerThinkingTimes))
print("Median of computer think time:", median(computerThinkingTimes))
print()
print("Minimum number of generated nodes:", min(noOfGeneratedNodes))
print("Maximum number of generated nodes:", max(noOfGeneratedNodes))
print("Average number of generated nodes:", mean(noOfGeneratedNodes))
print("Median of number of generated nodes:", median(noOfGeneratedNodes))
print()
print("The player moved {} times.".format(playerMoves))
print("The computer moved {} times.".format(computerMoves))
print()
print("The program has run for {} miliseconds.".format(programFinalTime - programStartTime))