-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
223 lines (182 loc) · 9.26 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
## main.py
This module is the entry point of the application. It allows the user to choose between two games: Tic Tac Toe and Reversi.
The games are played 100 times, with different player strategies. The results (number of wins for each player and ties) are
then printed out.
The module imports the necessary classes and functions from the gamestate, game, and players modules.
Functions:
- main(): The main function of the module. It handles the game choice, plays the games, and prints out the results.
This module is part of a first semester project in Artificial Intelligence at University of Peloponnese,
Department of Informatics and Telecommunications.
Authors:
- Giannopoulos Georgios
- Giannopoulos Ioannis
"""
from gamestate.gamestate import GameState
from game.game import Game
from game.tic_tac_toe import TicTacToe
from game.reversi import Reversi
from players.players import manual_player, minmax_player, random_player, alpha_beta_player, mcts_player, alpha_beta_cutoff_player
import time
def main():
x_wins = 0
o_wins = 0
ties = 0
while(True):
print("\t\t\tΕργασία Μαθήματος Τεχνητής Νοημοσύνης")
print("Τα ερωτήματα εκτελούνται σειριακά εκτός του ερωτήματος 3.1 που")
print("είναι το παιχνίδι του manual player VS minimax για το Reversi.")
print("Πληκτρολογήστε\n (1) για τα ερωτήματα της τρίλιζας\n (2) για τα ερωτήματα του Reversi\n (3) για το ερώτημα 3.1 και έπειτα cntr+c για την διακοπή του.\n Οτιδήποτε άλλο για έξοδο\n")
print("Ποιό παιχνίδι θέλεις να παίξεις?\n (1) Tic Tac Toe\n (2) Reversi\n (3) Ερώτημα 3.1")
game_choice = input("Πληκτρολόγησε τον αριθμό του παιχνιδιού >> ")
if (game_choice== '1' or game_choice == '2' or game_choice == '3'):
x_wins = 0
o_wins = 0
ties = 0
if game_choice == '1':
game = TicTacToe()
print("Ερώτημα 2.1 MiniMax vs Random player \n")
x_wins = 0
o_wins = 0
ties = 0
start_time = time.time()
# minmax player VS random player
for i in range(100):
utility = game.play_game(minmax_player, random_player)
if utility == 1:
print("'X' won!")
x_wins += 1
elif utility == -1:
print("'O' won!")
o_wins += 1
else:
print('Tie!')
ties += 1
end_time = time.time()
total_time = (end_time - start_time)
print("X wins: ", x_wins/100 * 100, "%")
print("O wins: ", o_wins / 100 * 100, "%")
print("Ties: ", ties / 100 * 100, "%")
print(f"Time elapsed: {total_time:.2f} seconds\n")
next_quest = input("\nΠατήστε οτιδήποτε για να συνεχίσετε στην επόμενη ερώτηση >> ")
print("=============================================================================================")
print("\nΕρώτημα 2.2 α-β player VS Random player")
x_wins = 0
o_wins = 0
ties = 0
# alpha beta player VS random player
start_time = time.time()
for i in range(100):
utility = game.play_game(alpha_beta_player, random_player)
if utility == 1:
print("'X' won!")
x_wins += 1
elif utility == -1:
print("'O' won!")
o_wins += 1
else:
print('Tie!')
ties += 1
end_time = time.time()
total_time = (end_time - start_time)
print("X wins: ", x_wins/100 * 100, "%")
print("O wins: ", o_wins / 100 * 100, "%")
print("Ties: ", ties / 100 * 100, "%")
print(f"Time elapsed: {total_time:.2f} seconds\n")
next_quest = input("\nΠατήστε οτιδήποτε για να συνεχίσετε στην επόμενη ερώτηση >> ")
print("=============================================================================================")
print("\nΕρώτημα 2.3 Monte Carlo player VS Random player")
x_wins = 0
o_wins = 0
ties = 0
# Monte Carlo tree search player VS random player
start_time = time.time()
for i in range(100):
utility = game.play_game(mcts_player, random_player)
if utility == 1:
print("'X' won!")
x_wins += 1
elif utility == -1:
print("'O' won!")
o_wins += 1
else:
print('Tie!')
ties += 1
end_time = time.time()
total_time = (end_time - start_time)
print("X wins: ", x_wins/100 * 100, "%")
print("O wins: ", o_wins / 100 * 100, "%")
print("Ties: ", ties / 100 * 100, "%")
print(f"Time elapsed: {total_time:.2f} seconds\n")
# break
elif game_choice == '2':
game = Reversi()
print("Ερώτημα 3.2 Τυχαίος παίκτης εναντίον Τυχαίου παίκτη\n")
start_time = time.time()
for i in range(100):
utility = game.play_game(random_player, random_player)
if utility == 1:
print("'X' won!")
x_wins += 1
elif utility == -1:
print("'O' won!")
o_wins += 1
else:
print('Tie!')
ties += 1
end_time = time.time()
total_time = (end_time - start_time)
print("X wins: ", x_wins / 100 * 100, "%")
print("O wins: ", o_wins / 100 * 100, "%")
print("Ties: ", ties / 100 * 100, "%")
print(f"Time elapsed: {total_time:.2f} seconds\n")
next_quest = input("\nΠατήστε οτιδήποτε για να συνεχίσετε στην επόμενη ερώτηση >> ")
print("=============================================================================================")
print("\nΕρώτημα 3.4 Πριόνισμα α-β με περιορισμό βάθους VS Random player\nΠαίρνει αρκετά λεπτά για να ολοκληρωθεί, περίπου 20 λεπτά\n")
x_wins = 0
o_wins = 0
ties = 0
start_time = time.time()
for i in range(100):
utility = game.play_game(alpha_beta_cutoff_player, random_player)
if utility == 1:
print("'X' won!")
x_wins += 1
elif utility == -1:
print("'O' won!")
o_wins += 1
else:
print('Tie!')
ties += 1
end_time = time.time()
total_time = (end_time - start_time) / 60
print("X wins: ", x_wins/100 * 100, "%")
print("O wins: ", o_wins / 100 * 100, "%")
print("Ties: ", ties / 100 * 100, "%")
print(f"Time elapsed: {total_time:.2f} minutes\n")
# break
elif(game_choice=='3'):
print("Ερώτημα 3.1 Παιχνίδι Reversi μεταξύ χειροκίνητου και minimax παίκτη\n")
game = Reversi()
x_wins = 0
o_wins = 0
ties = 0
utility = game.play_game(manual_player, minmax_player)
if utility == 1:
print("'X' won!")
x_wins += 1
elif utility == -1:
print("'O' won!")
o_wins += 1
else:
print('Tie!')
ties += 1
else:
print("Έξοδος...\n")
break
# print("X wins: ", x_wins/100 * 100, "%")
# print("O wins: ", o_wins / 100 * 100, "%")
# print("Ties: ", ties / 100 * 100, "%")
# print(f"Time elapsed: {total_time:.2f} minutes\n")
if __name__ == "__main__":
main()