Advertisement
Guest User

TicTacToe.project

a guest
Oct 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. import sys
  2.  
  3. theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
  4.             'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
  5.             'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
  6.  
  7. def printBoard(board):
  8.     print('-------')
  9.     print('|' + board['T-L'] + '|' + board['T-M'] + '|' + board['T-R'] + '|')
  10.     print('|' + '-+-+-' + '|')
  11.     print('|' + board['M-L'] + '|' + board['M-M'] + '|' + board['M-R'] + '|')
  12.     print('|' + '-+-+-' + '|')
  13.     print('|' + board['L-L'] + '|' + board['L-M'] + '|' + board['L-R'] + '|')
  14.     print('-------')
  15.  
  16. def playAgain() :
  17.     print('Do you want to play again?')
  18.     again = 0
  19.     while True:
  20.         print('Press: Y for rematch ; E for exit')
  21.         again = input()
  22.         if again == 'Y':
  23.             turn = 'X'
  24.             theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
  25.             'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
  26.             'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
  27.             break
  28.         if again == 'y':
  29.             turn = 'X'
  30.             theBoard = {'T-L': ' ', 'T-M': ' ', 'T-R': ' ',
  31.             'M-L': ' ', 'M-M': ' ', 'M-R': ' ',
  32.             'L-L': ' ', 'L-M': ' ', 'L-R': ' '}
  33.             break
  34.         elif again == 'E':
  35.             sys.exit()
  36.         elif again == 'e':
  37.             sys.exit()
  38.         else:
  39.             continue
  40.    
  41.    
  42. turn = 'X'
  43.  
  44. while True:
  45.     printBoard(theBoard)
  46.     print('Turn for ' + turn + '. Where do you want to place your ' + turn + '?')
  47.     move=input()
  48.     while True:
  49.         if ' ' in theBoard[move]:
  50.             theBoard[move] = turn
  51.             break
  52.         else:
  53.             print('This spot is taken! try something else')
  54.             print('Where do you want to place your ' + turn + '?')
  55.             move=input()
  56.             continue
  57.  
  58.     if turn in (theBoard['T-L'] and theBoard['T-M'] and theBoard['T-R']):
  59.         print('Result of the game:')
  60.         printBoard(theBoard)
  61.         print('Player ' + turn + ' won the game!')
  62.         playAgain()
  63.        
  64.     if turn in (theBoard['M-L'] and theBoard['M-M'] and theBoard['M-R']):
  65.         print('Result of the game:')
  66.         printBoard(theBoard)
  67.         print('Player ' + turn + ' won the game!')
  68.         playAgain()
  69.  
  70.        
  71.     if turn in (theBoard['L-L'] and theBoard['L-M'] and theBoard['L-R']):
  72.         print('Result of the game:')
  73.         printBoard(theBoard)
  74.         print('Player ' + turn + ' won the game!')
  75.         playAgain()
  76.  
  77.        
  78.     if turn in (theBoard['T-L'] and theBoard['M-M'] and theBoard['L-R']):
  79.         print('Result of the game:')
  80.         printBoard(theBoard)
  81.         print('Player ' + turn + ' won the game!')
  82.         playAgain()
  83.  
  84.        
  85.     if turn in (theBoard['L-L'] and theBoard['M-M'] and theBoard['T-R']):
  86.         print('Result of the game:')
  87.         printBoard(theBoard)
  88.         print('Player ' + turn + ' won the game!')
  89.         playAgain()
  90.  
  91.        
  92.     if turn in (theBoard['T-L'] and theBoard['M-L'] and theBoard['L-L']):
  93.         print('Result of the game:')
  94.         printBoard(theBoard)
  95.         print('Player ' + turn + ' won the game!')
  96.         playAgain()
  97.  
  98.        
  99.     if turn in (theBoard['T-M'] and theBoard['M-M'] and theBoard['L-M']):
  100.         print('Result of the game:')
  101.         printBoard(theBoard)
  102.         print('Player ' + turn + ' won the game!')
  103.         playAgain()
  104.  
  105.        
  106.     if turn in (theBoard['T-R'] and theBoard['M-R'] and theBoard['L-R']):
  107.         print('Result of the game:')
  108.         printBoard(theBoard)
  109.         print('Player ' + turn + ' won the game!')
  110.         playAgain()
  111.  
  112.        
  113.    
  114.     if turn == 'X':
  115.         turn = '0'
  116.     else:
  117.         turn = 'X'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement