-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerfectBot.py
More file actions
70 lines (54 loc) · 1.93 KB
/
Copy pathPerfectBot.py
File metadata and controls
70 lines (54 loc) · 1.93 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
#!/usr/bin/python
import random
import sys
import os
import queue
import Game
class PerfectBot():
botChoice = None
choice = None
def score(self,game):
if game.playerWon():
return -10
elif game.botWon():
return 10
else:
return 0
def get_choice(self):
return self.choice
def minimax(self,game,deapth,isMax,alpha, beta, deapthMax):
if game.playerWon() or game.botWon() or game.draw() or deapth == deapthMax:
return self.score(game)
'''
mv = game.availableMoves()
for move in mv:
# State in which we may be
possibleGame = game.possibleMove(move)
scores.append(self.minimax(possibleGame))
moves.append(move)
'''
#if is the bot's turn then we need to maximize the chances of winning
if(isMax):
maxim = -sys.maxsize - 1
for i in range(1,game.get_w() + 1):
for j in range(1,game.get_w() + 1):
if game.Harta[i][j] == 0:
game.Harta[i][j] = -1
maxim = max(maxim,self.minimax(game, deapth+1, False,alpha,beta,deapthMax))
alpha = max(alpha, maxim)
game.Harta[i][j] = 0
if beta <= alpha:
break
return maxim - deapth
else:
minim = sys.maxsize
for i in range(1,game.get_w() + 1):
for j in range(1,game.get_w() + 1):
if game.Harta[i][j] == 0:
game.Harta[i][j] = 1
minim = min(minim,self.minimax(game, deapth+1, True,alpha,beta,deapthMax))
beta = min(beta,minim)
game.Harta[i][j] = 0
if beta <= alpha:
break
return minim + deapth