-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-rev.py
118 lines (99 loc) · 3.23 KB
/
tic-tac-rev.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
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
# Tic-Tac-Toe game written in python
from random import randrange
board = [ [i + 3 * (j - 1) for i in range(1, 4)] for j in range(1, 4) ] # make an empty board
def display_board(board):
print("+-------" * 3, "+", sep="")
for row in range(3):
print("| " * 3, "|", sep="")
for col in range(3):
print("| " + str(board[row][col]) + " ", end="")
print("|")
print("| " * 3,"|",sep="")
print("+-------" * 3,"+",sep="")
print("WELCOME!")
display_board(board)
def enter_move(board):
while True:
try:
user = int(input("Your turn, 'Make a move by entering the box number': "))
found = False
for i, row in enumerate(board):
for j, col in enumerate(row):
if user == col:
board[i][j] = "O"
found = True
break
if not found:
print("Invalid move! Please choose a box with a number: ")
else: break
except ValueError as e:
print("Invalid move!, 'Enter a valid box number': ")
def draw_move(board):
while True:
found = False
comp = randrange(1, 10)
for i, row in enumerate(board):
for j, col in enumerate(row):
if comp == col:
board[i][j] = 'X'
found = True
break
if not found:
continue
else: break
def make_list_of_free_fields(board):
# The function browses the board and builds a list of all the free squares;
# the list consists of tuples, while each tuple is a pair of row and column numbers.
empty_sq =list()
for i, row in enumerate(board):
for j, col in enumerate(row):
if board[i][j] in range(10):
empty_sq.append((i, j))
return empty_sq
def victory_for(board, sign):
# The function analyzes the board's status in order to check if
# the player using 'O's or 'X's has won the game
if sign == 'X':
who = 'me'
elif sign == 'O':
who = 'you'
else: who = None
# Check rows
for row in board:
if all(element == sign for element in row):
return who
# Check columns
for col in range(3):
if all(board[i][col] == sign for i in range(3)):
return who
# Check diagonals
if all(board[i][i] == sign for i in range(3)):
return who
if all(board[i][2-i] == sign for i in range(3)):
return who
return None
print("I play first")
board[1][1] = "X"
display_board(board)
free = make_list_of_free_fields(board)
human_turn = True
while len(free):
if human_turn:
enter_move(board)
victor = victory_for(board, 'O')
display_board(board)
if victor == 'you':
print('CONGRATULATIONS! YOU WON')
break
else:
draw_move(board)
victor = victory_for(board, 'X')
display_board(board)
if victor == 'me':
print('I WON!')
break
if victor == None and len(free) <= 2:
print('DRAW!')
break
human_turn = not human_turn
free = make_list_of_free_fields(board)