-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtic-tac-toe.py
66 lines (57 loc) · 1.47 KB
/
tic-tac-toe.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
board = [' ']*9
keyMap = {
'tL':0,
'tM':1,
'tR':2,
'mL':3,
'mM':4,
'mR':5,
'bL':6,
'bM':7,
'bR':8
}
def hasWon(board, turn):
# return true if the board has been won by the char entered (either 'X' or '0')
# check each row, each column and each diagonal
target = [turn] * 3
return ((board[0:3] == target)
or (board[3:6] == target)
or (board[6:9] == target)
or (board[0:9:3] == target)
or (board[1:9:3] == target)
or (board[2:9:3] == target)
or (board[0:9:4] == target)
or (board[2:8:2] == target))
def hasEmpty(board):
# check if there is any empty space where anything can be entered
return ' ' in board
def print_board(board):
print(' | '.join(board[0:3]))
print('*********')
print(' | '.join(board[3:6]))
print('*********')
print(' | '.join(board[6:9]))
turn = 'X'
for i in range(9):
print(turn + "\'s turn. Which space do you wanna move?:")
move = keyMap[input()]
if board[move] == ' ':
board[move] = turn
else:
print("This space has already been chosen. \nChoose other space")
move = keyMap[input()]
board[move] = turn
print_board(board)
if hasWon(board, turn):
print(turn, " has won")
break
elif not hasEmpty(board):
print("It's a tie")
break
# if control comes here, it means move is possible.
# flip the turn
if turn == 'X':
turn = 'O'
else:
turn = 'X'
print("turn:" + turn)