-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
160 lines (144 loc) · 5.81 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
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
import os
from os import system
from time import sleep
from minmax import min_max, expectimax
from ghost import Ghost
from player import Player
class Game:
def __init__(self):
self.ground = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
self.player = Player([9, 9])
self.ghost1 = Ghost([5, 18])
self.ghost2 = Ghost([5, 1])
self.score = 0
def print_ground(self):
for i in range(0, 11):
for j in range(0, 20):
if self.player.location == [i, j]:
print("P", end=" ")
elif self.ghost1.location == [i, j]:
print("G", end=" ")
elif self.ghost2.location == [i, j]:
print("G", end=" ")
elif self.ground[i][j] == 0:
print("#", end=" ")
elif self.ground[i][j] == 1:
print(".", end=" ")
elif self.ground[i][j] == 2:
print(" ", end=" ")
print()
def is_game_over(self):
if self.score == 106:
return True
if self.player.location == self.ghost1.location or self.player.location == self.ghost2.location:
return True
else:
return False
def move_player_up(self):
if self.player.is_move_up_valid(self.ground):
self.player.move_up()
if self.ground[self.player.location[0]][self.player.location[1]] == 1:
self.score += 1
self.ground[self.player.location[0]][self.player.location[1]] = 2
def move_player_down(self):
if self.player.is_move_down_valid(self.ground):
self.player.move_down()
if self.ground[self.player.location[0]][self.player.location[1]] == 1:
self.score += 1
self.ground[self.player.location[0]][self.player.location[1]] = 2
def move_player_left(self):
if self.player.is_move_left_valid(self.ground):
self.player.move_left()
if self.ground[self.player.location[0]][self.player.location[1]] == 1:
self.score += 1
self.ground[self.player.location[0]][self.player.location[1]] = 2
def move_player_right(self):
if self.player.is_move_right_valid(self.ground):
self.player.move_right()
if self.ground[self.player.location[0]][self.player.location[1]] == 1:
self.score += 1
self.ground[self.player.location[0]][self.player.location[1]] = 2
def move_player(self, depth, mode):
movement = ""
if mode == "expectimax":
movement = expectimax(self, 0, "player_turn", depth, 0)
elif mode == "minimax":
movement = min_max(self, 0, "player_turn", depth, 0, -1000000000, +1000000000)
if movement == "":
raise Exception("Invalid movement")
if movement == "up":
self.move_player_up()
elif movement == "down":
self.move_player_down()
elif movement == "left":
self.move_player_left()
elif movement == "right":
self.move_player_right()
def run_game(self, depth, mode):
command = "clear"
if os.name == "nt":
command = "cls"
itr = 0
while not self.is_game_over():
itr += 1
print("Score: ", self.score)
print("Iteration: ", itr)
self.print_ground()
sleep(0.05)
self.move_player(depth, mode)
if self.is_game_over():
break
self.ghost1.make_move(self.ground)
if self.is_game_over():
break
self.ghost2.make_move(self.ground)
sleep(0.05)
system(command)
os.system(command)
print("Score: ", self.score)
print("Iteration: ", itr)
self.print_ground()
return self.score, itr
def test_run_game(self, depth, mode):
itr = 0
while not self.is_game_over():
itr += 1
self.move_player(depth, mode)
if self.is_game_over():
break
self.ghost1.make_move(self.ground)
if self.is_game_over():
break
self.ghost2.make_move(self.ground)
return self.score, itr
def test(mode, count_of_plays):
for d in range(1, 4):
count_of_lost = 0
avg_itr = 0
for i in range(0, count_of_plays):
game = Game()
score, itr = game.test_run_game(d, mode)
if score != 106:
count_of_lost += 1
avg_itr += itr
avg_itr /= count_of_plays
print("Mode: ", mode)
print("Depth: ", d)
print("Count of plays: ", count_of_plays)
print("Count of lost: ", count_of_lost)
print("Average iteration: ", avg_itr)
print("winning rate: ", (count_of_plays - count_of_lost)/count_of_plays * 100, "%")
print("losing rate: ", count_of_lost/count_of_plays*100, "%")
print("---------------------------------")