Advertisement
Flaunchy

Tic Tac Toe

Feb 4th, 2016
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. # Create a playable 2-person tic tac toe game.
  2. import copy
  3.  
  4. print('What is player 1\'s name?')
  5. player1 = input()
  6. player1 = [player1, 'X']
  7.  
  8. print('What is player 2\'s name?')
  9. player2 = input()
  10. player2 = [player2, 'O']
  11.  
  12.  
  13. theBoard = { '1': ' ', '2': ' ', '3': ' ',
  14.              '4': ' ', '5': ' ', '6': ' ',
  15.              '7': ' ', '8': ' ', '9': ' '}
  16.  
  17. def printBoard(board):
  18.     print(theBoard['1'] + '|' + theBoard['2'] + '|' + theBoard['3'])
  19.     print('-+-+-')
  20.     print(theBoard['4'] + '|' + theBoard['5'] + '|' + theBoard['6'])
  21.     print('-+-+-')
  22.     print(theBoard['7'] + '|' + theBoard['8'] + '|' + theBoard['9'])
  23.  
  24. turn1 = copy.copy(player1)
  25. turn = turn1
  26.  
  27. for i in range(9):
  28.     printBoard(theBoard)
  29.     print('Turn for ' + turn[0] + '. Move on which space?')
  30.     move = input()
  31.     while move not in theBoard:
  32.         print('You must enter a number from 1-9.') #So you don't accidentally lose a turn.
  33.         move = input()
  34.     while theBoard[move] != ' ':     # So you can't steal another player's space.
  35.         print('You cannot overwrite another player\'s move. Try again.')
  36.         move = input()
  37.     theBoard[move] = turn[1]   # Establishes the player's position.
  38.     if theBoard['1' and '2' and '3' \
  39.             or '4' and '5' and '6' \
  40.             or '7' and '8' and '9' \
  41.             or '1' and '4' and '7' \
  42.             or '2' and '5' and '8' \
  43.             or '3' and '6' and '9' \
  44.             or '1' and '5' and '9' \
  45.             or '3' and '5' and '7'] == player1[1]: # Establishes winner.
  46.         print('Congratulations ' + player1[0] + '! You have won the game.')
  47.         break
  48.     if theBoard['1' and '2' and '3' \
  49.             or '4' and '5' and '6' \
  50.             or '7' and '8' and '9' \
  51.             or '1' and '4' and '7' \
  52.             or '2' and '5' and '8' \
  53.             or '3' and '6' and '9' \
  54.             or '1' and '5' and '9' \
  55.             or '3' and '5' and '7'] == player2[1]: # Establishes winner.
  56.         print('Congratulations ' + player2[0] +'! You have won the game.')
  57.         break
  58.     if turn == turn1: # Changes turn
  59.         turn = copy.copy(player2)
  60.     else:
  61.         turn = turn1
  62.  
  63. printBoard(theBoard)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement