This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.py
71 lines (63 loc) · 1.7 KB
/
algorithm.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
import game
import copy
# import random
# import sys
# A subclass of game that has an added method for making the best/smartest move
# Simulates algorithms move
def refactoredA(gameObj):
losses = 0
for nextMoveA in gameObj.possMoves(): # loop through all the possible moves
newGameObj = copy.deepcopy(gameObj)
codeA = newGameObj.play(nextMoveA)
if codeA == 10:
continue
elif codeA != 1:
continue
else:
losses += refactoredB(newGameObj)
return losses
# Simulates players move
def refactoredB(ghostBoardA):
losses = 0
for nextMoveB in ghostBoardA.possMoves():
ghostBoardB = copy.deepcopy(ghostBoardA)
codeB = ghostBoardB.play(nextMoveB)
if codeB == 12:
losses += 1
continue
elif codeB != 1:
continue
else:
losses += refactoredA(ghostBoardB)
return losses
class SmartGame(game.Game):
def smartMove(self):
moveRepo = {}
for nextMoveA in self.possMoves():
losses = 0 # choose the move that has the least number of losses
ghostBoardA = copy.deepcopy(self)
codeA = ghostBoardA.play(nextMoveA)
if codeA == 10: # if a winning move is availible play immediately
self.play(nextMoveA)
return
elif codeA != 1:
continue
else:
losses += refactoredB(ghostBoardA)
moveRepo[nextMoveA] = losses
bestMove = min(moveRepo.items(), key=lambda x: x[1])[0]
self.play(bestMove)
def __init__(self):
super().__init__()
# takes in the game as a argument. Returns how many losses
def versus(self):
while self.winner() is None:
if not self.pTurn:
move = int(input("Where would you like to go: "))
self.play(move)
else:
self.smartMove()
print(self.normalDisplay())
if __name__ == '__main__':
b = SmartGame()
b.versus()