[text] 1

Viewer

  1. # Define the game state
  2. current_player = 1
  3. game_over = False
  4.  
  5. # Define the game loop
  6. while not game_over:
  7.     # Display the game board
  8.     for row in board:
  9.         print("|".join(str(cell) for cell in row))
  10.         print("-" * (size * 2 + 1))
  11.  
  12.     # Get the user input
  13.     row = int(input("Player {}: Enter the row (0-2): ".format(current_player)))
  14.     col = int(input("Player {}: Enter the column (0-2): ".format(current_player)))
  15.  
  16.     # Check if the cell is empty
  17.     if board[row][col] == 0:
  18.         # Place the piece
  19.         board[row][col] = current_player
  20.  
  21.         # Check for winning condition
  22.         if check_win(current_player, board):
  23.             print("Player {} wins!".format(current_player))
  24.             game_over = True
  25.  
  26.         # Switch to the next player
  27.         current_player = 2 if current_player == 1 else 1
  28.  
  29.         # Check for a tie
  30.         if not any(0 in row for row in board):
  31.             print("It's a tie!")
  32.             game_over = True
  33.     else:
  34.         print("Cell already occupied. Try again.")

Editor

You can edit this paste and save as new:


File Description
  • 1
  • Paste Code
  • 27 Apr-2024
  • 1.03 Kb
You can Share it: