G2A Many GEOs
SHARE
TWEET

Python Tic Tac Toe code

Manusman May 6th, 2019 150 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2.  
  3.  
  4. the_board = {1: '1', 2: '2', 3: '3',
  5.              4: '4', 5: '5', 6: '6',           # the player board
  6.              7: '7', 8: '8', 9: '9'}
  7.  
  8.  
  9. def print_board(board):
  10.     print(board[1] + '|' + board[2] + '|' + board[3])
  11.     print('-+-+-')
  12.     print(board[4] + '|' + board[5] + '|' + board[6])          # prints the board with boundaries
  13.     print('-+-+-')
  14.     print(board[7] + '|' + board[8] + '|' + board[9])
  15.  
  16.  
  17. print('Enter player 1 name: ')
  18. player1 = input()
  19. print('Enter player 2 name: ')
  20. player2 = input()                           # set player names, turn var and scoreboard vars
  21. player = player1
  22. turn = 'X'
  23. p1wins = 0
  24. p2wins = 0
  25.  
  26.  
  27. def check_win(board):
  28.     global p1wins, p2wins                      # checks win possibilities and determines a winner
  29.     if (board[1] == board[2] == board[3] != ' ') or (board[4] == board[5] == board[6] != ' ') or \
  30.             (board[7] == board[8] == board[9] != ' ') or (board[1] == board[4] == board[7] != ' ') or \
  31.             (board[2] == board[5] == board[8] != ' ') or (board[3] == board[6] == board[9] != ' ') or \
  32.             (board[3] == board[5] == board[7] != ' ') or (board[1] == board[5] == board[9] != ' '):
  33.         print_board(board)
  34.         print('player ' + player + ' wins!!')
  35.         if turn == 'X':
  36.             p1wins += 1
  37.         else:
  38.             p2wins += 1
  39.         return True
  40.     else:
  41.         return False
  42.  
  43.  
  44. def play():
  45.     global turn, p1wins, p2wins, player1, player2, player
  46.     for i in range(9):
  47.         print_board(the_board)
  48.         print("It's your turn " + player + '.')
  49.         move = input()
  50.         if turn == 'X':
  51.             the_board[int(move)] = '\033[1;31m' + turn + '\033[m'      # the game itself
  52.         else:
  53.             the_board[int(move)] = '\033[1;32m' + turn + '\033[m'
  54.         if check_win(the_board):
  55.             break
  56.         else:
  57.             if turn == 'X':
  58.                 turn = '0'
  59.                 player = player2
  60.             else:
  61.                 turn = 'X'
  62.                 player = player1
  63.     print('GAME OVER')
  64.     print(player1 + ' has ' + str(p1wins) + ' wins.')                   # scoreboard
  65.     print(player2 + ' has ' + str(p2wins) + ' wins.')
  66.  
  67.  
  68. print('Imagine the board like this, when you want to enter the desired space simply type in the number as shown here:')
  69. print_board(the_board)
  70. k = 1
  71. while k <= 9:
  72.     the_board[k] = ' '
  73.     k += 1
  74. print('------------------------------------------------')
  75. pos = ['Y', 'YES']
  76. play()
  77. while True:
  78.     print('Do you want to play again?')
  79.     ans = input().upper()
  80.     if ans in pos:
  81.         for x in the_board:
  82.             the_board[x] = ' '
  83.         print('loading...')                          # play again
  84.         time.sleep(2)
  85.         play()
  86.     else:
  87.         break
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top