-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleboard.py
118 lines (103 loc) · 4.7 KB
/
singleboard.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class SingleBoard:
def __init__(self,board_id):
self.player1 = False
self.player2 = False
self.pieces = 0
self.board=[None for j in range(9)]
self.id = board_id
def isMyTurn(self,all_board):
self.getPieces(all_board)
if (self.pieces % 2) == 0:
if self.player1 == True:
return True
else:
return False
else:
if self.player1 == True:
return False
else:
return True
# return True
def joinGame(self,all_board):
self.getPieces(all_board)
if self.pieces == 0:
self.player1 = True
return 1
elif self.pieces == 1:
self.player2 = True
return 2
else:
return 0
def reJoin(self,pawn):
if pawn == 1:
self.player1 = True
return 1
elif pawn == 2:
self.player2 = True
return 2
else:
return 0
def checkWinner(self,all_board):
self.getPieces(all_board)
pemenang = None
#check horisontal
index_horizontal = 0
for i in range(3):
if self.board[index_horizontal] == self.board[index_horizontal+1] and self.board[index_horizontal+1] == self.board[index_horizontal+2] and self.board[index_horizontal] != None:
pemenang = self.board[index_horizontal]
break
else:
index_horizontal = index_horizontal + 3
#check vertikal
index_vertikal = 0
for i in range(3):
if self.board[index_vertikal] == self.board[index_vertikal+3] and self.board[index_vertikal+3] == self.board[index_vertikal+6] and self.board[index_vertikal] != None:
pemenang = self.board[index_vertikal]
break
else:
index_vertikal = index_vertikal + 1
#check diagonal
if self.board[0] == self.board[4] and self.board[4] == self.board[8] and self.board[0] != None:
pemenang = self.board[0]
if self.board[2] == self.board[4] and self.board[4] == self.board[6] and self.board[2] != None:
pemenang = self.board[2]
#check draw
if self.pieces==9 and pemenang==None:
return 3
return pemenang
def extractBoard(self,all_board):
self.board=[None for j in range(9)]
if self.id == 0:
self.board = [all_board[0][0],all_board[1][0],all_board[2][0], all_board[0][1],all_board[1][1],all_board[2][1], all_board[0][2],all_board[1][2],all_board[2][2]]
elif self.id == 1:
self.board = [all_board[3][0],all_board[4][0],all_board[5][0], all_board[3][1],all_board[4][1],all_board[5][1], all_board[3][2],all_board[4][2],all_board[5][2]]
elif self.id == 2:
self.board = [all_board[6][0],all_board[7][0],all_board[8][0], all_board[6][1],all_board[7][1],all_board[8][1], all_board[6][2],all_board[7][2],all_board[8][2]]
elif self.id == 3:
self.board = [all_board[0][3],all_board[1][3],all_board[2][3], all_board[0][4],all_board[1][4],all_board[2][4], all_board[0][5],all_board[1][5],all_board[2][5]]
elif self.id == 4:
self.board = [all_board[3][3],all_board[4][3],all_board[5][3], all_board[3][4],all_board[4][4],all_board[5][4], all_board[3][5],all_board[4][5],all_board[5][5]]
elif self.id == 5:
self.board = [all_board[6][3],all_board[7][3],all_board[8][3], all_board[6][4],all_board[7][4],all_board[8][4], all_board[6][5],all_board[7][5],all_board[8][5]]
elif self.id == 6:
self.board = [all_board[0][6],all_board[1][6],all_board[2][6], all_board[0][7],all_board[1][7],all_board[2][7], all_board[0][8],all_board[1][8],all_board[2][8]]
elif self.id == 7:
self.board = [all_board[3][6],all_board[4][6],all_board[5][6], all_board[3][7],all_board[4][7],all_board[5][7], all_board[3][8],all_board[4][8],all_board[5][8]]
elif self.id == 8:
self.board = [all_board[6][6],all_board[7][6],all_board[8][6], all_board[6][7],all_board[7][7],all_board[8][7], all_board[6][8],all_board[7][8],all_board[8][8]]
def getPieces(self,all_board):
self.extractBoard(all_board)
countable = 0
for i in range(9):
if self.board[i] != None:
countable = countable + 1
self.pieces = countable
def countPieces(self,all_board):
self.getPieces(all_board)
return self.pieces
def countBoard(self,all_board):
self.getPieces(all_board)
return self.board
return self.pieces
def getMyBoardID(self):
return self.id