-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
89 lines (78 loc) · 2.94 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
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
88
89
# Bunaren TicTacToe Board
# Created by Fransiscus Emmanuel Bunaren
# This file is for the TicTacToe Board
class Board:
def __init__(self):
self.symbol2value = lambda x: 1 if x == "X" else (
-1 if x == "O" else 0)
self.board = list(" " * 9)
self.turn = "X"
def setBoard(self, inputs):
if type(inputs) != list:
inputs = list(inputs)
self.board = inputs
return self.board
# For printing the current state of board
def print(self, show_turn=False):
print(" " + str(['1',
'2',
'3']).replace("[",
" ").replace("]",
" ").replace("'",
"").replace(",",
" "))
print(1, str([self.board[0], self.board[1], self.board[2]]).replace(
"'", "").replace(",", "|"))
print(2, str([self.board[3], self.board[4], self.board[5]]).replace(
"'", "").replace(",", "|"))
print(3, str([self.board[6], self.board[7], self.board[8]]).replace(
"'", "").replace(",", "|"))
if show_turn:
print("\n" + self.turn, "turns")
def getTurn(self):
return self.turn
def changeTurn(self):
if self.turn == "X":
self.turn = "O"
else:
self.turn = "X"
def isOver(self):
# Checking Rows
for i in [[0, 1, 2], [3, 4, 5], [6, 7, 8]]:
is_over = abs(sum([self.symbol2value(self.board[k]) for k in i]))
if is_over == 3:
return self.board[i[0]]
# Columns
tmp = [0, 3, 6]
for i in range(3):
is_over = abs(
sum([self.symbol2value(self.board[i + k]) for k in tmp]))
if is_over == 3:
return self.board[i + tmp[0]]
# Diagonal
is_over = abs(sum([self.symbol2value(self.board[0]), self.symbol2value(
self.board[4]), self.symbol2value(self.board[8])]))
if is_over == 3:
return self.board[0]
is_over = abs(sum([self.symbol2value(self.board[2]), self.symbol2value(
self.board[4]), self.symbol2value(self.board[6])]))
if is_over == 3:
return self.board[2]
# If none of the above satisfied, then check if it is a draw
if self.board.count(" ") <= 0:
return "Draw"
else:
return False
# For checking if a position is valid
def isValid(self, pos):
if pos < 9 and pos >= 0 and self.board[pos] == " ":
return True
else:
return False
def move(self, pos):
if self.isValid(pos):
self.board[pos] = self.turn
self.changeTurn()
return self.board
else:
return False