-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
67 lines (54 loc) · 2.14 KB
/
game.py
File metadata and controls
67 lines (54 loc) · 2.14 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
import board
# The game class stores the board and players and plays the game
class Game:
# Store the players and create a board with the given specification
# IMPORTANT: Note that the coursework player is always player 1 (and so always goes first)
def __init__(self, cwPlayer, player2, rows, columns, winNum):
self.player1 = cwPlayer
self.player2 = player2
self.gameBoard = board.Board(rows, columns, winNum)
self.listOfPlayers = (cwPlayer, player2)
# Play the game itself, with or without alpha-beta pruning according to whether the pruning
# argument is true or false respectively.
def playGame(self, pruning):
# Keep track of whether the game is won or the board is full
won = False
full = False
# index is used to keep track of which player's move it is
index = 0
# play the game until a player wins or it is a draw (i.e., the board is full)
while not won and not full:
# Get the current player, and update the index
currPlayer = self.listOfPlayers[index]
print("Moving with player " + str(currPlayer.name))
if index == 0 and pruning:
move = currPlayer.getMoveAlphaBeta(self.gameBoard.copy())
else:
move = currPlayer.getMove(self.gameBoard.copy())
moveDone = self.gameBoard.addPiece(move, currPlayer.name)
if moveDone == True:
won = self.gameBoard.checkWin()
full = self.gameBoard.checkFull()
# Uncomment the following line to print each move
self.gameBoard.printBoard()
else:
print("Player made illegal move. Turn lost.")
index = (index + 1) % 2
if won and currPlayer == self.player1:
print("You Win!")
print("Nodes expanded:", self.player1.numExpanded)
print("Branches pruned:", self.player1.numPruned)
self.gameBoard.printBoard()
return 1
if won and currPlayer == self.player2:
print("You Lose!")
print("Nodes expanded:", self.player1.numExpanded)
print("Branches pruned:", self.player1.numPruned)
self.gameBoard.printBoard()
return -1
if full and not won:
print("It's a Draw!")
print("Nodes expanded:", self.player1.numExpanded)
print("Branches pruned:", self.player1.numPruned)
self.gameBoard.printBoard()
return 0