-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
87 lines (84 loc) · 3.5 KB
/
TicTacToe.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
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'low-L': ' ', 'low-M': ' ', 'low-R': ' '}
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
def end(move):
if move == 'quit':
raise SystemExit
def checkBoard(board):
if (board['top-L'] == 'X' and board['mid-L'] == 'X' and board['low-L'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['top-M'] == 'X' and board['mid-M'] == 'X' and board['low-M'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['top-R'] == 'X' and board['mid-R'] == 'X' and board['low-r'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['top-L'] == 'X' and board['top-M'] == 'X' and board['top-R'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['mid-L'] == 'X' and board['mid-M'] == 'X' and board['mid-R'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['low-L'] == 'X' and board['low-M'] == 'X' and board['low-R'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['top-L'] == 'X' and board['mid-M'] == 'X' and board['low-R'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['top-R'] == 'X' and board['mid-M'] == 'X' and board['low-L'] == 'X'):
print('X is winner!')
raise SystemExit
elif (board['top-L'] == 'O' and board['mid-L'] == 'O' and board['low-L'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['top-M'] == 'O' and board['mid-M'] == 'O' and board['low-M'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['top-R'] == 'O' and board['mid-R'] == 'O' and board['low-r'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['top-L'] == 'O' and board['top-M'] == 'O' and board['top-R'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['mid-L'] == 'O' and board['mid-M'] == 'O' and board['mid-R'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['low-L'] == 'O' and board['low-M'] == 'O' and board['low-R'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['top-L'] == 'O' and board['mid-M'] == 'O' and board['low-R'] == 'O'):
print('O is winner!')
raise SystemExit
elif (board['top-R'] == 'O' and board['mid-M'] == 'O' and board['low-L'] == 'O'):
print('O is winner!')
raise SystemExit
turn = 'X'
move = ' '
print('Type in "quit" to end game.')
print('\nTo choose a position, type in which row(top, mid, low) followed by which column(-R, -M, -L')
print('To move to the top left corner, you would type "top-L", bottom mid would be "low-M", without the quotes.')
while move != 'quit':
printBoard(theBoard)
checkBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
move = input()
end(move)
if (move not in theBoard):
print('\nTry again.')
continue
elif (theBoard[move] == ' '):
theBoard[move] = turn
else:
print('\nTry again.')
continue
if turn == 'X':
turn = 'O'
else:
turn = 'X'