Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. import random
  2. from IPython.display import clear_output
  3.  
  4. # Displays the current board
  5. def display_board(board):
  6.     print(f"{board[6]}|{board[7]}|{board[8]}")
  7.     print("-+-+-")
  8.     print(f"{board[3]}|{board[4]}|{board[5]}")
  9.     print("-+-+-")
  10.     print(f"{board[0]}|{board[1]}|{board[2]}")
  11.  
  12. # Player 1 chooses X or O, Player 2 is assigned the other
  13. def player_input():
  14.     PlayerInput=''    
  15.     while not PlayerInput=='X' and not PlayerInput=='O':
  16.         PlayerInput=input('Player 1, choose X or O: ').upper()
  17.        
  18.     Player1=PlayerInput
  19.     if Player1=='X':
  20.         Player2='O'
  21.     else:
  22.         Player2='X'
  23.     return (Player1,Player2)
  24.  
  25. def place_marker(board, marker, position):
  26.     board[position-1]=marker
  27.  
  28. def win_check(board, mark):
  29.     for c in board:
  30.         if c!=' ':
  31.             if (board[6]==mark and board[7]==mark and board[8]==mark) or (board[3]==mark and board[4]==mark and board[5]==mark) or (board[0]==mark and board[1]==mark and board[2]==mark) or (board[6]==mark and board[3]==mark and board[0]==mark) or (board[7]==mark and board[4]==mark and board[1]==mark) or (board[8]==mark and board[5]==mark and board[2]==mark) or (board[6]==mark and board[4]==mark and board[2]==mark) or (board[8]==mark and board[4]==mark and board[0]==mark):    
  32.                 return True
  33.             else:
  34.                 return False
  35.  
  36. def choose_first():
  37.     first=random.randint(1,100)
  38.     if first<=50:
  39.         return True
  40.     elif first>50:
  41.         return False
  42.  
  43. def space_check(board, position):
  44.     if board[position-1]==' ':
  45.         return True
  46.     else:
  47.         return False
  48.  
  49. def full_check(board):
  50.     for i in board:
  51.         if i==' ':
  52.             return False
  53.     return True
  54.  
  55. def player_choice(board):
  56.     choice=input('Choose where you want to place your marker: ')
  57.     intchoice=int(choice)
  58.     while not space_check(board, intchoice):
  59.         print('Space is full. Choose another space.')
  60.     return choice
  61.  
  62. def replay():
  63.     playagain=input('Would you like to play again?\n')
  64.     if playagain.lower=='yes':
  65.         return True
  66.     elif playagain.lower=='no':
  67.         return False
  68.     else:
  69.         print('Please type "yes" or "no."')
  70.  
  71. def tic_tac_toe():
  72.     print('Welcome to Tic-Tac-Toe!')
  73.     # List representing the board, indexes represent the board via the numpad
  74.     board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
  75.    
  76.     # Variables storing whether Player 1 or 2 are X or O
  77.     Player1, Player2 = player_input()
  78.    
  79.     PlayingGame=True
  80.    
  81.     playAgain=True
  82.     isPlayer1turn=choose_first()
  83.     while PlayingGame and playAgain:
  84.         if isPlayer1turn:
  85.             print("Player 1's turn.")
  86.             GridPosition=player_choice(board)
  87.             intGrid=int(GridPosition)
  88.             place_marker(board, Player1, intGrid)
  89.             isPlayer1turn=not isPlayer1turn
  90.             display_board(board)
  91.         elif not isPlayer1turn:
  92.             print("Player 2's turn.")
  93.             GridPosition=player_choice(board)
  94.             intGrid=int(GridPosition)
  95.             place_marker(board, Player2, intGrid)
  96.             isPlayer1turn=not isPlayer1turn
  97.             display_board(board)
  98.         if win_check(board, Player1):
  99.             print('Player 1 wins!')
  100.             playAgain=replay()
  101.             if playAgain:
  102.                 board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
  103.                 continue
  104.             else:
  105.                 print('Game Over!')
  106.                 PlayingGame=False
  107.         if win_check(board, Player2):
  108.             print('Player 2 wins!')
  109.             playAgain=replay()
  110.             if playAgain:
  111.                 board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
  112.                 continue
  113.             else:
  114.                 print('Game Over!')
  115.                 PlayingGame=False
  116.         if full_check(board):
  117.             print('Board is full! Tie game!')
  118.             playAgain=replay()
  119.             if playAgain:
  120.                 board=[' ',' ',' ',' ',' ',' ',' ',' ',' ']
  121.                 continue
  122.             else:
  123.                 print('Game Over!')
  124.                 PlayingGame=False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement