-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe_Checker.py
63 lines (53 loc) · 2.43 KB
/
Tic-Tac-Toe_Checker.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
"""
If we were to set up a Tic-Tac-Toe game, we would want to know whether the board's current state is solved, wouldn't we?
Our goal is to create a function that will check that for us!
Assume that the board comes in the form of a 3x3 array, where the value is 0 if a spot is empty, 1 if it is an "X", or 2 if it is an "O", like so:
[[0, 0, 1],
[0, 1, 2],
[2, 1, 0]]
We want our function to return:
-1 if the board is not yet finished AND no one has won yet (there are empty spots),
1 if "X" won,
2 if "O" won,
0 if it's a cat's game (i.e. a draw).
You may assume that the board passed in is valid in the context of a game of Tic-Tac-Toe.
"""
def is_solved(matrix):
if [matrix[0][0], matrix[0][1], matrix[0][2]] == [1, 1, 1]:
return (1)
elif [matrix[1][0], matrix[1][1], matrix[1][2]] == [1, 1, 1]:
return (1)
elif [matrix[2][0], matrix[2][1], matrix[2][2]] == [1, 1, 1]:
return (1)
elif [matrix[0][0], matrix[1][0], matrix[2][0]] == [1, 1, 1]:
return (1)
elif [matrix[0][1], matrix[1][1], matrix[2][1]] == [1, 1, 1]:
return (1)
elif [matrix[0][2], matrix[1][2], matrix[2][2]] == [1, 1, 1]:
return (1)
elif [matrix[0][0], matrix[1][1], matrix[2][2]] == [1, 1, 1]:
return (1)
elif [matrix[0][2], matrix[1][1], matrix[2][0]] == [1, 1, 1]:
return (1)
elif [matrix[0][0], matrix[0][1], matrix[0][2]] == [2, 2, 2]:
return (2)
elif [matrix[1][0], matrix[1][1], matrix[1][2]] == [2, 2, 2]:
return (2)
elif [matrix[2][0], matrix[2][1], matrix[2][2]] == [2, 2, 2]:
return (2)
elif [matrix[0][0], matrix[1][0], matrix[2][0]] == [2, 2, 2]:
return (2)
elif [matrix[0][1], matrix[1][1], matrix[2][1]] == [2, 2, 2]:
return (2)
elif [matrix[0][2], matrix[1][2], matrix[2][2]] == [2, 2, 2]:
return (2)
elif [matrix[0][0], matrix[1][1], matrix[2][2]] == [2, 2, 2]:
return (2)
elif [matrix[0][2], matrix[1][1], matrix[2][0]] == [2, 2, 2]:
return (2)
elif [0, 0, 0] in matrix or [0, 0, 1] in matrix or [0, 1, 0] in matrix or [1, 0, 0] in matrix or [1, 0, 1] in matrix or [0, 1, 1] in matrix or [1, 1, 0] in matrix:
return (-1)
elif [0, 0, 0] in matrix or [0, 0, 2] in matrix or [0, 2, 0] in matrix or [2, 0, 0] in matrix or [2, 0, 2] in matrix or [0, 2, 2] in matrix or [2, 2, 0] in matrix:
return (-1)
else:
return (0)