-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproject - tic-tac-toe sample answer.py
More file actions
93 lines (85 loc) · 2.8 KB
/
project - tic-tac-toe sample answer.py
File metadata and controls
93 lines (85 loc) · 2.8 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
# 4.1.6.13 PROJECT: Tic-Tac-Toe
from random import randrange
def DisplayBoard(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="")
def EnterMove(board):
ok = False # fake assumption - we need it to enter the loop
while not ok:
move = input("Enter your move: ")
ok = len(move) == 1 and move >= '1' and move <= '9' # is user's input valid?
if not ok:
print("Bad move - repeat your input!") # no, it isn't - do the input again
continue
move = int(move) - 1 # cell's number from 0 to 8
row = move // 3 # cell's row
col = move % 3 # cell's column
sign = board[row][col] # check the selected square
ok = sign not in ['O','X']
if not ok: # it's occupied - to the input again
print("Field already occupied - repeat your input!")
continue
board[row][col] = 'O' # set '0' at the selected square
def MakeListOfFreeFields(board):
free = [] # the list is empty initially
for row in range(3): # iterate through rows
for col in range(3): # iterate through columns
if board[row][col] not in ['O','X']: # is the cell free?
free.append((row,col)) # yes, it is - append new tuple to the list
return free
def VictoryFor(board,sgn):
if sgn == "X": # are we looking for X?
who = 'me' # yes - it's computer's side
elif sgn == "O": # ... or for O?
who = 'you' # yes - it's our side
else:
who = None # we should not fall here!
cross1 = cross2 = True # for diagonals
for rc in range(3):
if board[rc][0] == sgn and board[rc][1] == sgn and board[rc][2] == sgn: # check row rc
return who
if board[0][rc] == sgn and board[1][rc] == sgn and board[2][rc] == sgn: # check column rc
return who
if board[rc][rc] != sgn: # check 1st diagonal
cross1 = False
if board[2 - rc][2 - rc] != sgn: # check 2nd diagonal
cross2 = False
if cross1 or cross2:
return who
return None
def DrawMove(board):
free = MakeListOfFreeFields(board) # make a list of free fields
cnt = len(free)
if cnt > 0: # if the list is not empty, choose a place for 'X' and set it
this = randrange(cnt)
row, col = free[this]
board[row][col] = 'X'
board = [ [3 * j + i + 1 for i in range(3)] for j in range(3) ] # make an empty board
board[1][1] = 'X' # set first 'X' in the middle
free = MakeListOfFreeFields(board)
humanturn = True # which turn is it now?
while len(free):
DisplayBoard(board)
if humanturn:
EnterMove(board)
victor = VictoryFor(board,'O')
else:
DrawMove(board)
victor = VictoryFor(board,'X')
if victor != None:
break
humanturn = not humanturn
free = MakeListOfFreeFields(board)
DisplayBoard(board)
if victor == 'you':
print("You won!")
elif victor == 'me':
print("I won")
else:
print("Tie!")