Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create a playable 2-person tic tac toe game.
- import copy
- print('What is player 1\'s name?')
- player1 = input()
- player1 = [player1, 'X']
- print('What is player 2\'s name?')
- player2 = input()
- player2 = [player2, 'O']
- theBoard = { '1': ' ', '2': ' ', '3': ' ',
- '4': ' ', '5': ' ', '6': ' ',
- '7': ' ', '8': ' ', '9': ' '}
- def printBoard(board):
- print(theBoard['1'] + '|' + theBoard['2'] + '|' + theBoard['3'])
- print('-+-+-')
- print(theBoard['4'] + '|' + theBoard['5'] + '|' + theBoard['6'])
- print('-+-+-')
- print(theBoard['7'] + '|' + theBoard['8'] + '|' + theBoard['9'])
- turn1 = copy.copy(player1)
- turn = turn1
- for i in range(9):
- printBoard(theBoard)
- print('Turn for ' + turn[0] + '. Move on which space?')
- move = input()
- while move not in theBoard:
- print('You must enter a number from 1-9.') #So you don't accidentally lose a turn.
- move = input()
- while theBoard[move] != ' ': # So you can't steal another player's space.
- print('You cannot overwrite another player\'s move. Try again.')
- move = input()
- theBoard[move] = turn[1] # Establishes the player's position.
- if theBoard['1' and '2' and '3' \
- or '4' and '5' and '6' \
- or '7' and '8' and '9' \
- or '1' and '4' and '7' \
- or '2' and '5' and '8' \
- or '3' and '6' and '9' \
- or '1' and '5' and '9' \
- or '3' and '5' and '7'] == player1[1]: # Establishes winner.
- print('Congratulations ' + player1[0] + '! You have won the game.')
- break
- if theBoard['1' and '2' and '3' \
- or '4' and '5' and '6' \
- or '7' and '8' and '9' \
- or '1' and '4' and '7' \
- or '2' and '5' and '8' \
- or '3' and '6' and '9' \
- or '1' and '5' and '9' \
- or '3' and '5' and '7'] == player2[1]: # Establishes winner.
- print('Congratulations ' + player2[0] +'! You have won the game.')
- break
- if turn == turn1: # Changes turn
- turn = copy.copy(player2)
- else:
- turn = turn1
- printBoard(theBoard)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement