Printboard: Print Print Print Print Print
Printboard: Print Print Print Print Print
In [ ]:
theBoard = {'7': ' ' , '8': ' ' , '9': ' ' ,
'4': ' ' , '5': ' ' , '6': ' ' ,
'1': ' ' , '2': ' ' , '3': ' ' }
board_keys = []
def printBoard(board):
print(board['7'] + '|' + board['8'] + '|' + board['9'])
print('-+-+-')
print(board['4'] + '|' + board['5'] + '|' + board['6'])
print('-+-+-')
print(board['1'] + '|' + board['2'] + '|' + board['3'])
def game():
turn = 'X'
count = 0
for i in range(10):
printBoard(theBoard)
print("It's your turn," + turn + ".Move to which place?")
move = input()
if count >= 5:
if theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the top
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the m
iddle
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the b
ottom
printBoard(theBoard)
print("\nGame Over.\n")
print(" **** " +turn + " won. ****")
break
elif theBoard['1'] == theBoard['4'] == theBoard['7'] != ' ': # down the lef
t side
printBoard(theBoard)
print("\nGame Over.\n")
localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 5/7
4/17/2020 Untitled1
if count == 9:
print("\nGame Over.\n")
print("It's a Tie!!")
if turn =='X':
turn = 'O'
else:
turn = 'X'
game()
if __name__ == "__main__":
game()
localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 6/7
4/17/2020 Untitled1
| |
-+-+-
| |
-+-+-
| |
It's your turn,X.Move to which place?
5
| |
-+-+-
|X|
-+-+-
| |
It's your turn,O.Move to which place?
1
| |
-+-+-
|X|
-+-+-
O| |
It's your turn,X.Move to which place?
1
That place is already filled.
Move to which place?
| |
-+-+-
|X|
-+-+-
O| |
It's your turn,X.Move to which place?
2
| |
-+-+-
|X|
-+-+-
O|X|
It's your turn,O.Move to which place?
4
| |
-+-+-
O|X|
-+-+-
O|X|
It's your turn,X.Move to which place?
8
|X|
-+-+-
O|X|
-+-+-
O|X|
Game Over.
In [ ]:
localhost:8888/nbconvert/html/Untitled1.ipynb?download=false 7/7