-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-gameIsOver.py
More file actions
executable file
·116 lines (94 loc) · 2.87 KB
/
Copy pathtest-gameIsOver.py
File metadata and controls
executable file
·116 lines (94 loc) · 2.87 KB
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
def winInRow(row, board):
column_0 = index_of(row, 0)
column_1 = index_of(row, 1)
column_2 = index_of(row, 2)
isNotBlank = board[column_0] == O or board[column_0] == X
isTheSameInAllColumns = (
board[column_0] == board[column_1] and board[column_1] == board[column_2]
)
return isNotBlank and isTheSameInAllColumns
def winInColumn(column, board):
return False
def winTopLeftToBottomRight(board):
return False
def winBottomLeftToTopRight(board):
return False
def outOfTurns(turns):
return turns >= 9
def gameIsOver(board, turns):
# There are 8 win conditions:
# Win in the nth row
# Win in the nth column
# Win in one of the two diagonals
# And one "end game" non-win condition, a full board
if winInRow(0, board):
return True
if winInRow(1, board):
return True
if winInRow(2, board):
return True
if winInColumn(0, board):
return True
if winInColumn(1, board):
return True
if winInColumn(2, board):
return True
if winTopLeftToBottomRight(board):
return True
if winBottomLeftToTopRight(board):
return True
if outOfTurns(turns):
return True
return False
def test(board, turns, expectedValue):
isGameOver = gameIsOver(board, turns)
if isGameOver != expectedValue:
print()
print("Test failed.")
printBox(board)
print("Expected isGameOver to be", expectedValue)
print()
def index_of(row, column):
return (row * 3) + column
def printBox(values):
for row in range(3):
for column in range(3):
index = index_of(row, column)
value = values[index]
print(value, end=" ")
print("")
O = "O"
X = "X"
B = "-"
# test(["O", "O", "O", "X", "X", "-", "-", "-", "-"], 5, True)
# test(["O", "-", "O", "X", "X", "-", "-", "-", "-"], 4, False)
# Win in the 0th row
test([O, O, O, X, X, B, B, B, B], 0, True)
# Win in the 1th row
test([O, O, B, X, X, X, B, B, B], 0, True)
# Win in the 2th row
test([O, O, B, B, B, B, X, X, X], 0, True)
# Win in the 0th column
test([O, X, B, O, X, B, O, B, B], 0, True)
# Win in the 1th column
test([O, X, B, O, X, B, B, X, B], 0, True)
# Win in the 2th column
test([O, B, X, O, B, X, B, B, X], 0, True)
# Win bottom left to top right
test([O, B, X, O, X, B, X, B, B], 0, True)
# Win top left to bottom right
test([X, B, O, O, X, B, B, B, X], 0, True)
# Nobody wins, but game is over
test([X, O, X, O, X, O, O, X, O], 9, True)
# Game is not over
test([B, B, B, B, B, B, B, B, B], 0, False)
# Game is not over
test([X, B, B, B, B, B, B, B, B], 0, False)
# Game is not over
test([X, B, B, B, O, B, B, B, B], 0, False)
# Game is not over
test([X, B, X, B, O, B, B, B, B], 0, False)
# Game is not over
test([X, O, X, B, O, B, B, B, B], 0, False)
# Game is not over
test([X, O, X, B, O, B, B, X, B], 0, False)