-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
107 lines (92 loc) · 2.7 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'''
0 - White square, 1 - Black square
2 - White pawn, 4 - White King
3 - Black pawn, 5 - Black King
'''
def createBoard(color="Black"):
if color == "Black":
board = [
[0, 2, 0, 2, 0, 2, 0, 2],
[2, 0, 2, 0, 2, 0, 2, 0],
[0, 2, 0, 2, 0, 2, 0, 2],
[1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1],
[3, 0, 3, 0, 3, 0, 3, 0],
[0, 3, 0, 3, 0, 3, 0, 3],
[3, 0, 3, 0, 3, 0, 3, 0]]
return board
'''
Reassign Board
Become king
in attack move check for double jump
'''
def updateBoard(board, turn, slcrow, slccol, dstrow, dstcol):
contAttack = False
if (dstrow == 0) and turn == 3:
board[dstrow][dstcol] = turn + 2
elif (dstrow == 7) and turn == 2:
board[dstrow][dstcol] = turn + 2
else:
board[dstrow][dstcol] = board[slcrow][slccol]
board[slcrow][slccol] = 1
if (slcrow - dstrow == 2) or (dstrow - slcrow == 2):
board[(dstrow + slcrow) // 2][(dstcol + slccol) // 2] = 1
contAttack = isContinue(board, turn, dstrow, dstcol)
return board, contAttack
'''
Check game is finished
'''
def isFinish(board, running):
whites, blacks = 0, 0
for i in range(8):
for j in range(8):
if board[i][j] == 2 or board[i][j] == 4:
whites += 1
elif board[i][j] == 3 or board[i][j] == 5:
blacks += 1
if (whites == 0):
print("BLACK WON!")
running = False
return running, "Black"
elif (blacks == 0):
print("WHITE WON!")
running = False
return running, "White"
else:
return True, None
'''
Check for double jump
'''
def isContinue(board, turn, row, col):
#if turn == 3:
# opp = 2
# next = [(-1, -1), (-1, +1)]
#else:
# opp = 3
# next = [(+1, -1), (+1, +1)]
match board[row][col]:
case 2:
opp = 3
oppk = 5
next = [(+1, -1), (+1, +1)]
case 3:
opp = 2
oppk = 4
next = [(-1, -1), (-1, +1)]
case 4:
opp = 3
oppk = 5
next = [(+1, -1), (+1, +1), (-1, -1), (-1, +1)]
case 5:
opp = 2
oppk = 4
next = [(+1, -1), (+1, +1), (-1, -1), (-1, +1)]
for (r, c) in next:
if (1 <= (row + r) < 7) and (1 <= (col + c) < 7) and \
(board[row + r][col + c] == opp or board[row + r][col + c] == oppk) and \
board[row + 2 * r][col + 2 * c] == 1:
return True
return False
def printBoard(board):
for i in board:
print(i)