-
Notifications
You must be signed in to change notification settings - Fork 43
/
nxn-board-game.py
82 lines (69 loc) · 2.59 KB
/
nxn-board-game.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
'''You are given an nxn board in which players place red and black pieces on. When three pieces of the same color
are placed adjacent to each other (horizontally, vertically, or diagonally), the player responsible earns a point.
Given that the board is filled in with 1s and 0s to represent the red and black pieces, create a function to
evaluate the number of points each player has and the winner.'''
def winner(matrix):
n = len(matrix)
black, red = 0, 0
for i in range(n):
for j in range(n):
# left
if i-2 >= 0 and matrix[i-2][j] == matrix[i-1][j] == matrix[i][j]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# right
if i+2 < n and matrix[i][j] == matrix[i+1][j] == matrix[i+2][j]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# up
if j-2 >= 0 and matrix[i][j-2] == matrix[i][j-1] == matrix[i][j]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# down
if j+2 < n and matrix[i][j] == matrix[i][j+1] == matrix[i][j+2]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# left-up diagonal
if i-2 >= 0 and j-2 >= 0 and matrix[i-2][j-2] == matrix[i-1][j-1] == matrix[i][j]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# right-up diagonal
if i+2 < n and j-2 >= 0 and matrix[i][j] == matrix[i+1][j-1] == matrix[i+2][j-2]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# right-down diagonal
if i+2 < n and j+2 < n and matrix[i][j] == matrix[i+1][j+1] == matrix[i+2][j+2]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
# left-down diagonal
if i-2 >= 0 and j+2 < n and matrix[i-2][j+2] == matrix[i-1][j+1] == matrix[i][j]:
if matrix[i][j] == 0:
black += 1
else:
red += 1
red /= 2
black /= 2
print("Black: %d\nRed: %d" % (black, red))
if red > black:
print("Red is the winner.")
return 1
elif red < black:
print("Black is the winner")
return 0
else:
print("Tie game")
return -1