Advertisement
Guest User

TTT;TMTTM #3

a guest
Oct 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. import random
  2.  
  3. print('Welcome to Tic Tac Toe')
  4.  
  5. #displays board
  6. def Display(board):
  7.     print('\n'*100)
  8.     print(board[7] + ' | ' + board[8] + ' | ' + board[9])
  9.     print("- | - | -")
  10.     print(board[4] + ' | ' + board[5] + ' | ' + board[6])
  11.     print("- | - | -")
  12.     print(board[1] + ' | ' + board[2] + ' | ' + board[3])
  13.  
  14. #gets users input for which mark they will be
  15. def PlayerInput():
  16.     player1 = ''
  17.    
  18.     while player1 != 'X' and player1 != 'O':
  19.         player1 = input('To begin, Player 1 please choose your symbol "X" or "O": ').upper()
  20.        
  21.     if player1 == 'X':
  22.         player2 = 'O'
  23.     else:
  24.         player2 = 'X'
  25.        
  26.     return(player1, player2)
  27.  
  28.  
  29.  
  30. #Places the users mark on the board at the given position
  31. def PlaceMark(board, marker, position):
  32.     if position > 9 or (marker != 'X' and marker != 'x' and marker != 'O' and marker != 'o'):
  33.         pass
  34.     else:
  35.         board[position] = marker.upper()
  36.  
  37. #Returns true if there was a winner and false if there was not
  38. def Winner(board, mark):
  39.     mark = mark.upper()
  40.     threeinarow = [mark, mark, mark]
  41.     # check for a horizontal row
  42.     return board[1:4] == threeinarow or board[4:7] == threeinarow or board[7:10] == threeinarow
  43.     # check for a vertical column
  44.     return board[1:8:3] == threeinarow or board[2:9:3] == threeinarow or board[3:10:3] == threeinarow
  45.     # check either diagonal
  46.     print(f'board[1:10:4]={board[1:10:4]}, board[3:8:2]={board[3:8:2]}')
  47.     return board[1:10:4] == threeinarow or board[3:8:2] == threeinarow
  48.  
  49. #randomly determines who plays first
  50. def ChooseTurn():
  51.     if random.randint(1,2) == 1:
  52.         return 'Player 1'
  53.     else:
  54.         return 'Player 2'
  55.  
  56. #Returns true if that given position is free and false if it is not
  57. def Space(board, position):
  58.     return board[position] == ' '
  59.  
  60. #Returns true if the board is full and false if it is not
  61. def IsFull(board):
  62.     for i in range(1,10):
  63.         if Space(board, i):
  64.             return False
  65.     return True
  66.  
  67. #Gets the users input for what space they want to put there mark.
  68. #Also shows a second board with available spaces the user can choose from
  69. def Choice(board, availablePos):
  70.     position = 0
  71.    
  72.     while position not in range(1,10) or not Space(board, position):
  73.         #display available positions
  74.         print(f'{availablePos[7]} | {availablePos[8]} | {availablePos[9]}')
  75.         print("- | - | -")
  76.         print(f'{availablePos[4]} | {availablePos[5]} | {availablePos[6]}')
  77.         print("- | - | -")
  78.         print(f'{availablePos[1]} | {availablePos[2]} | {availablePos[3]}')
  79.         position = int(input("Enter number to place your mark: "))
  80.         availablePos[position] = ' '
  81.  
  82.     return position
  83.  
  84. #Returns true if the user wants to replay the game and false if they do not
  85. def Replay():
  86.     choice = ''
  87.     while choice != 'yes' and choice != 'Yes' and choice != 'no' and choice != 'No':
  88.         choice = input('Play Again?')
  89.     if choice == 'yes' or choice == 'Yes':
  90.         return True
  91.     else:
  92.         return False
  93.  
  94.  
  95.  
  96. while True:
  97.      #initialize my two boards
  98.      available = list(range(0,10))
  99.      board = [' '] * 10
  100.      
  101.      p1, p2 = PlayerInput()
  102.      turn = ChooseTurn()
  103.      
  104.      play = True
  105.  
  106.      while play:
  107.         if turn == 'Player 1':
  108.             Display(board)
  109.             print(turn + "'s turn")
  110.             PlaceMark(board, p1, Choice(board, available))
  111.  
  112.             if Winner(board, p1):
  113.                 Display(board)
  114.                 print('Congratulations ' + turn + ', you win! ')
  115.                 play = False
  116.             else:
  117.                 if IsFull(board):
  118.                     Display(board)
  119.                     print('The game is a draw!')
  120.                     break
  121.                 else:
  122.                     turn = 'Player 2'
  123.  
  124.         else:
  125.             Display(board)
  126.             print(turn + "'s turn")
  127.             position = Choice(board, available)
  128.             PlaceMark(board, p2, position)
  129.  
  130.             if Winner(board, p2):
  131.                 Display(board)
  132.                 print('Congratulations ' + turn + ', you win! ')
  133.                 play = False
  134.             else:
  135.                 if IsFull(board):
  136.                     Display(board)
  137.                     print('The game is a draw!')
  138.                     break
  139.                 else:
  140.                     turn = 'Player 1'
  141.  
  142.      playAgain = Replay()
  143.      
  144.      if not playAgain:
  145.          break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement