forked from psounis/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise6.py
More file actions
84 lines (75 loc) · 2.99 KB
/
exercise6.py
File metadata and controls
84 lines (75 loc) · 2.99 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
board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
]
player = "O"
for _ in range(9):
# print board
print(" +---+---+---+")
print(str(2) + " | " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " |")
print(" +---+---+---+")
print(str(1) + " | " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " |")
print(" +---+---+---+")
print(str(0) + " | " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " |")
print(" +---+---+---+")
print(" 0 1 2")
# choose next player
if player == "X":
player = "O"
else:
player = "X"
# user input
while True:
print("Player " + player + " plays! ")
row = int(input("Give row: "))
col = int(input("Give column: "))
if row < 0 or row > 2:
print("Row out of bounds (0-2). ")
continue
elif col < 0 or col > 2:
print("Column out of bounds (0-2). ")
continue
elif board[row][col]!=" ":
print("Pick an empty box")
continue
else:
board[row][col]=player
break
# check winner
winner = False
if (board[0][0] == board[0][1] and board[0][1] == board[0][2]) and board[0][0] != " ":
winner = player
elif (board[1][0] == board[1][1] and board[1][1] == board[1][2]) and board[1][0] != " ":
winner = player
elif (board[2][0] == board[2][1] and board[2][1] == board[2][2]) and board[2][0] != " ":
winner = player
elif (board[0][0] == board[1][0] and board[1][0] == board[2][0]) and board[0][0] != " ":
winner = player
elif (board[0][1] == board[1][1] and board[1][1] == board[2][1]) and board[0][1] != " ":
winner = player
elif (board[0][2] == board[1][2] and board[1][2] == board[2][2]) and board[0][2] != " ":
winner = player
elif (board[0][0] == board[1][1] and board[1][1] == board[2][2]) and board[0][0] != " ":
winner = player
elif (board[2][0] == board[1][1] and board[1][1] == board[0][2]) and board[2][0] != " ":
winner = player
if winner:
print("+---+---+---+")
print("| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " |")
print("+---+---+---+")
print("| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " |")
print("+---+---+---+")
print("| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " |")
print("+---+---+---+")
print("Player " + player + "won! ")
break
else:
print("+---+---+---+")
print("| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " |")
print("+---+---+---+")
print("| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " |")
print("+---+---+---+")
print("| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " |")
print("+---+---+---+")
print("Isopalia! ")