-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticTacToe.py
114 lines (101 loc) · 3.54 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
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
import sys
def icon(playerNumber):
"""
returns 'O' or 'X' if input playerNumber 1 or 2, otherwise returns a blank space string.
"""
if playerNumber == 1:
return 'O'
elif playerNumber == 2:
return 'X'
else:
return ' '
def showBoard(gameState):
"""
prints the board in its current gameState.
"""
print('')
for row in gameState:
print([icon(square) for square in row])
def swapTurn(playerNumber):
"""
flip player turn between 1 and 2.
"""
if playerNumber == 1:
return 2
elif playerNumber == 2:
return 1
def checkWin(game, w):
"""
for given game state 'game' and player 'w'. returns True if player 'w' has won, or False if not.
"""
win = False
if game[0] == [w, w, w] or game[1] == [w, w, w] or game[2] == [w, w, w]:
win = True
elif game[0][0] == w and game[1][0] == w and game[2][0] == w:
win = True
elif game[0][1] == w and game[1][1] == w and game[2][1] == w:
win = True
elif game[0][2] == w and game[1][2] == w and game[2][2] == w:
win = True
elif game[0][0] == w and game[1][1] == w and game[2][2] == w:
win = True
elif game[0][2] == w and game[1][1] == w and game[2][0] == w:
win = True
return win
def turnCycle(playerNumber, gameState):
try:
move = input('player {} ({}), make your move [row, col]: '.format(playerNumber, icon(playerNumber))).strip()
if move.lower() == 'quit':
sys.exit()
move = move.split(',')
if len(move) != 2:
raise ValueError
move = [int(coord) - 1 for coord in move]
if gameState[move[0]][move[1]] == ' ':
gameState[move[0]][move[1]] = playerNumber
else:
print('illegal move. please try another sqaure.')
except ValueError:
print('\nerror. please enter your move with the correct format:')
print('row, column')
print('for example to mark the top right square, enter: 1, 3\n')
def main():
print('***********************')
print('welcome to Tic Tac Toe!')
print('***********************')
print('in turns please enter your desired move coordinates in the following format:')
print('row, column')
print('for example to mark the top right square, enter: 1, 3\n')
# initial conditions:
stateState = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
startingPlayer = 1
whosTurn = startingPlayer
gameState = [list(row) for row in stateState]
gameOn = True
while gameOn:
turnCycle(whosTurn, gameState)
showBoard(gameState)
if checkWin(gameState, whosTurn):
gameOn = False
print('************************')
print('game over! player {} won!'.format(whosTurn))
print('************************')
whosTurn = swapTurn(whosTurn)
fullSquares = 0
for square in gameState[0] + gameState[1] + gameState[2]:
if square != ' ':
fullSquares += 1
if fullSquares == 9 and gameOn:
gameOn = False
print('*************************')
print('game over! it\'s a draw...')
print('*************************')
if not gameOn:
yn = input('do you want to play another game [type Y for yes or anything else to quit]?\n')
if yn.lower() == 'y':
gameState = [list(row) for row in stateState]
gameOn = True
else:
sys.exit()
if __name__ == '__main__':
main()