forked from psounis/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise11.py
More file actions
167 lines (134 loc) · 4.55 KB
/
exercise11.py
File metadata and controls
167 lines (134 loc) · 4.55 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from random import randrange, seed
from datetime import datetime
# globals
board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
]
# functions
def 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")
def check_row(row):
return (board[row][0] == board[row][1] and board[row][1] == board[row][2]) and board[row][0] != " "
def check_col(col):
return (board[0][col] == board[1][col] and board[1][col] == board[2][col]) and board[0][col] != " "
def check_diagonals():
return ((board[0][0] == board[1][1] and board[1][1] == board[2][2]) and board[0][0] != " ") or \
(board[2][0] == board[1][1] and board[1][1] == board[0][2]) and board[2][0] != " "
def computer_moves(level):
def danger_sequence(sequence):
xs = 0
empty = None
for sq in sequence:
if board[sq[0]][sq[1]] == "X":
xs += 1
elif board[sq[0]][sq[1]] == " ":
empty = sq
if xs == 2 and empty is not None:
return empty
return None
def danger():
for i in range(3):
row = [(i,j) for j in range(3)]
sq = danger_sequence(row)
if sq is not None:
return sq
for j in range(3):
col = [(i,j) for i in range(3)]
sq = danger_sequence(col)
if sq is not None:
return sq
sq = danger_sequence([(0,0),(1,1),(2,2)])
if sq is not None:
return sq
sq = danger_sequence([(0,2),(1,1),(2,0)])
if sq is not None:
return sq
if level == 1:
squares = []
for i in range(3):
for j in range(3):
if board[i][j] == " ":
squares += [(i,j)]
return squares[randrange(len(squares))]
else: # hard
sq = danger()
if sq is not None:
return sq
elif board[1][1] == " ":
return (1,1)
else:
corner_squares = [(0,0), (0,2), (2,0), (2,2)]
empty_squares = []
for sq in corner_squares:
if board[sq[0]][sq[1]] == " ":
empty_squares += [sq]
if empty_squares:
return empty_squares[randrange(len(empty_squares))]
middle_squares = [(0,1), (1,0), (1,2), (2,1)]
empty_squares = []
for sq in middle_squares:
if board[sq[0]][sq[1]] == " ":
empty_squares += [sq]
if empty_squares:
return empty_squares[randrange(len(empty_squares))]
def main():
seed(datetime.now())
level = int(input("Choose level(1-easy, 2-hard): "))
player = "O"
for _ in range(9):
print_board()
# choose next player
if player == "X":
player = "O"
else:
player = "X"
# user input
while player == "X":
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
if player == "O":
sq = computer_moves(level)
board[sq[0]][sq[1]] = "O"
# check winner
winner = False
for i in range(3):
if check_row(i):
winner = player
break
if check_col(i):
winner = player
break
if not winner:
if check_diagonals():
winner = player
if winner:
print_board()
print("Player " + player + " won! ")
break
else:
print_board()
print("Draw! ")
# main
main()