-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
54 lines (46 loc) · 1.56 KB
/
board.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
import random
import numpy as np
from randomPlayer import RandomPlayer
class Board:
def __init__(self,num_games = 10_000,player1 = RandomPlayer(),
player2 = RandomPlayer()):
self.num_games = num_games
self.player1 = player1
self.player2 = player2
@staticmethod
def winCheck(b):
cols = np.sum(b,axis=1)
rows = np.sum(b,axis=0)
diag1= int(sum(np.diag(b)))
diag2 = int(sum(np.diag(np.fliplr(b))))
if 3 in cols or 3 in rows or diag1 == 3 or diag2 == 3:
return 1
elif -3 in cols or -3 in rows or diag1 == -3 or diag2 == -3:
return -1
elif 0 not in b:
return 0
return None
def playGame(self,first_move = -1):
move_fn = [None,self.player1.move,self.player2.move]
playerID = first_move
board = np.zeros((3,3))
gameState = Board.winCheck(board)
while gameState == None:
board[move_fn[playerID](board)] = playerID
playerID*=-1
gameState = Board.winCheck(board)
return gameState
def playGames(self):
first_move = -1
for i in range(self.num_games):
print(i)
gameState = self.playGame(first_move)
if gameState == 0:
self.player1.onDraw()
self.player2.onDraw()
elif gameState == 1:
self.player1.onWin()
self.player2.onLoss()
elif gameState == -1:
self.player2.onWin()
self.player1.onLoss()