SHARE
TWEET

Untitled

a guest Mar 21st, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # --------- Global Variables -----------
  2.  
  3. # Will hold our game board data
  4. board = ["-", "-", "-",
  5.          "-", "-", "-",
  6.          "-", "-", "-"]
  7.  
  8. # Lets us know if the game is over yet
  9. game_still_going = True
  10.  
  11. # Tells us who the winner is
  12. winner = None
  13.  
  14. # Tells us who the current player is (X goes first)
  15. current_player = "X"
  16.  
  17.  
  18. # ------------- Functions ---------------
  19.  
  20. # Play a game of tic tac toe
  21. def play_game():
  22.  
  23.   # Show the initial game board
  24.   display_board()
  25.  
  26.   # Loop until the game stops (winner or tie)
  27.   while game_still_going:
  28.  
  29.     # Handle a turn
  30.     handle_turn(current_player)
  31.  
  32.     # Check if the game is over
  33.     check_if_game_over()
  34.  
  35.     # Flip to the other player
  36.     flip_player()
  37.  
  38.   # Since the game is over, print the winner or tie
  39.   if winner == "X" or winner == "O":
  40.     print(winner + " won.")
  41.   elif winner == None:
  42.     print("Tie.")
  43.  
  44.  
  45. # Display the game board to the screen
  46. def display_board():
  47.   print("\n")
  48.   print(board[0] + " | " + board[1] + " | " + board[2] + "     1 | 2 | 3")
  49.   print(board[3] + " | " + board[4] + " | " + board[5] + "     4 | 5 | 6")
  50.   print(board[6] + " | " + board[7] + " | " + board[8] + "     7 | 8 | 9")
  51.   print("\n")
  52.  
  53.  
  54. # Handle a turn for an arbitrary player
  55. def handle_turn(player):
  56.  
  57.   # Set global variables we need to edit
  58.   global winner
  59.  
  60.   # Get position from player
  61.   print(player + "'s turn.")
  62.   position = input("Choose a position from 1-9: ")
  63.  
  64.   # Whatever the user inputs, make sure it is a valid input, and the spot is open
  65.   valid = False
  66.   while not valid:
  67.  
  68.     # Make sure the input is valid
  69.     while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  70.       position = input("Choose a position from 1-9: ")
  71.  
  72.     # Get correct index in our board list
  73.     position = int(position) - 1
  74.  
  75.     # Then also make sure the spot is available on the board
  76.     if board[position] == "-":
  77.       valid = True
  78.     else:
  79.       print("You can't go there. Go again.")
  80.  
  81.   # Put the game piece on the board
  82.   board[position] = player
  83.  
  84.   # Show the game board
  85.   display_board()
  86.  
  87.  
  88. # Check if the game is over
  89. def check_if_game_over():
  90.   check_for_winner()
  91.   check_for_tie()
  92.  
  93.  
  94. # Check to see if somebody has won
  95. def check_for_winner():
  96.   # Set global variables
  97.   global winner
  98.   # Check if there was a winner anywhere
  99.   row_winner = check_rows()
  100.   column_winner = check_columns()
  101.   diagonal_winner = check_diagonals()
  102.   # Get the winner
  103.   if row_winner:
  104.     winner = row_winner
  105.   elif column_winner:
  106.     winner = column_winner
  107.   elif diagonal_winner:
  108.     winner = diagonal_winner
  109.   else:
  110.     winner = None
  111.  
  112.  
  113. # Check the rows for a win
  114. def check_rows():
  115.   # Set global variables
  116.   global game_still_going
  117.   # Check if any of the rows have all the same value (and is not empty)
  118.   row_1 = board[0] == board[1] == board[2] != "-"
  119.   row_2 = board[3] == board[4] == board[5] != "-"
  120.   row_3 = board[6] == board[7] == board[8] != "-"
  121.   # If any row does have a match, flag that there is a win
  122.   if row_1 or row_2 or row_3:
  123.     game_still_going = False
  124.   # Return the winner
  125.   if row_1:
  126.     return board[0]
  127.   elif row_2:
  128.     return board[3]
  129.   elif row_3:
  130.     return board[6]
  131.   # Or return None if there was no winner
  132.   else:
  133.     return None
  134.  
  135.  
  136. # Check the columns for a win
  137. def check_columns():
  138.   # Set global variables
  139.   global game_still_going
  140.   # Check if any of the columns have all the same value (and is not empty)
  141.   column_1 = board[0] == board[3] == board[6] != "-"
  142.   column_2 = board[1] == board[4] == board[7] != "-"
  143.   column_3 = board[2] == board[5] == board[8] != "-"
  144.   # If any row does have a match, flag that there is a win
  145.   if column_1 or column_2 or column_3:
  146.     game_still_going = False
  147.   # Return the winner
  148.   if column_1:
  149.     return board[0]
  150.   elif column_2:
  151.     return board[1]
  152.   elif column_3:
  153.     return board[2]
  154.   # Or return None if there was no winner
  155.   else:
  156.     return None
  157.  
  158.  
  159. # Check the diagonals for a win
  160. def check_diagonals():
  161.   # Set global variables
  162.   global game_still_going
  163.   # Check if any of the columns have all the same value (and is not empty)
  164.   diagonal_1 = board[0] == board[4] == board[8] != "-"
  165.   diagonal_2 = board[2] == board[4] == board[6] != "-"
  166.   # If any row does have a match, flag that there is a win
  167.   if diagonal_1 or diagonal_2:
  168.     game_still_going = False
  169.   # Return the winner
  170.   if diagonal_1:
  171.     return board[0]
  172.   elif diagonal_2:
  173.     return board[2]
  174.   # Or return None if there was no winner
  175.   else:
  176.     return None
  177.  
  178.  
  179. # Check if there is a tie
  180. def check_for_tie():
  181.   # Set global variables
  182.   global game_still_going
  183.   # If board is full
  184.   if "-" not in board:
  185.     game_still_going = False
  186.     return True
  187.   # Else there is no tie
  188.   else:
  189.     return False
  190.  
  191.  
  192. # Flip the current player from X to O, or O to X
  193. def flip_player():
  194.   # Global variables we need
  195.   global current_player
  196.   # If the current player was X, make it O
  197.   if current_player == "X":
  198.     current_player = "O"
  199.   # Or if the current player was O, make it X
  200.   elif current_player == "O":
  201.     current_player = "X"
  202.  
  203.  
  204. # ------------ Start Execution -------------
  205. # Play a game of tic tac toe
  206. play_game()
RAW Paste Data
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