-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameclass.py
More file actions
87 lines (78 loc) · 2.7 KB
/
gameclass.py
File metadata and controls
87 lines (78 loc) · 2.7 KB
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
class aigamebot:
def __init__(self):
self.numstring = []
self.score = [] #[max,min]
def __str__(self):
return '[Comp: '+str(self.score[0])+', '+str(self.numstring)+', Player: '+str(self.score[1])
def setvals(self,lst,humscore,aiscore):
self.numstring.clear()
self.score.clear()
self.numstring.extend(lst)
ascore = aiscore.split(":")
hscore = humscore.split(":")
self.score.extend([int(ascore[1]),int(hscore[1])])
def chkwin(self):
if len(self.numstring) == 1:
return True
else:
return False
def whowon(self,player):
plin = player - 1
if len(self.numstring) == 1:
if self.score[plin] == max(self.score):
return True
else:
return False
else:
return False
def drawcheck(self):
if len(self.numstring) == 1:
if self.score[0] == self.score[1]:
return True
else:
return False
else:
return False
def comp_act(self):
bestscore = -10
bestnum = 0
for num in self.numstring:
self.numstring.remove(num)
self.score[0] = self.score[0] - num
score = self.minimax(0,False)
self.numstring.append(num)
self.score[0] = self.score[0] + num
if(score > bestscore):
bestscore = score
bestnum = num
print("Computer Choose ",bestnum)
return bestnum
def minimax(self, depth, isMaxi):
if self.whowon(1):
return 1
elif self.whowon(2):
return -1
elif self.drawcheck():
return 0
if(isMaxi):
bestscore = -10
for num in self.numstring:
self.numstring.remove(num)
self.score[0] = self.score[0] - num
score = self.minimax(depth + 1,False)
self.numstring.append(num)
self.score[0] = self.score[0] + num
if(score > bestscore):
bestscore = score
return bestscore
else:
bestscore = 10
for num in self.numstring:
self.numstring.remove(num)
self.score[1] = self.score[1] - num
score = self.minimax(depth + 1,True)
self.numstring.append(num)
self.score[1] = self.score[1] + num
if(score > bestscore):
bestscore = score
return bestscore