remie92

Untitled

Dec 30th, 2025
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. import os
  2.  
  3. def print_board(board):
  4.     for i in range(0, 9, 3):
  5.         print(f" {board[i]} | {board[i+1]} | {board[i+2]} ")
  6.         if i < 6:
  7.             print("-----------")
  8.  
  9. def check_win(board):
  10.     win_patterns = [
  11.         [0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
  12.         [0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
  13.         [0, 4, 8], [2, 4, 6]              # Diagonals
  14.     ]
  15.    
  16.     for pattern in win_patterns:
  17.         print(board[pattern[0]] == board[pattern[1]] and board[pattern[1]] == board[pattern[2]])
  18.         if board[pattern[0]] == board[pattern[1]] and board[pattern[1]] == board[pattern[2]]:           #IT FUCKED THIS UP, ITS NOT CHECKING IF THE BOARD HAS AN X OR O
  19.             return True
  20.  
  21. def clear_screen():
  22.     os.system('cls' if os.name == 'nt' else 'clear')
  23.  
  24. # Initialize game state
  25. board = [' ', ' ', ' ',
  26.          ' ', ' ', ' ',
  27.          ' ', ' ', ' ']
  28. current_player = 'X'
  29. game_mode = 'continue'
  30.  
  31. print("Welcome to Tic Tac Toe! Player X goes first.")
  32. while True:
  33.     print(f"\nPlayer {current_player}'s turn:")
  34.     print_board(board)
  35.    
  36.     if game_mode == 'draw':
  37.         break
  38.    
  39.     move = input("\nEnter position (1-9): ")
  40.     try:
  41.         move = int(move) - 1
  42.     except ValueError:
  43.         print("\nInvalid input. Please enter a number.")
  44.         continue
  45.  
  46.     if board[move] != ' ':
  47.         print("\nThat position is already taken! Choose another one.")
  48.         continue
  49.    
  50.     # Update the board with player's choice
  51.     board[move] = current_player
  52.    
  53.     clear_screen()
  54.    
  55.     # Check for win or draw
  56.     if check_win(board):
  57.         print_board(board)
  58.         winner = 'X' if current_player == 'X' else 'O'
  59.         print(f"\nPlayer {winner} wins!")
  60.         game_mode = 'win'                   #IT DOESNT DO ANYTHING IF GAME_MODE IS WIN
  61.        
  62.     elif all(pos != ' ' for pos in board):
  63.         print("\nGame ended in a draw!")
  64.         game_mode = 'draw'
  65.        
  66.     else:
  67.         # Switch player
  68.         current_player = 'X' if current_player == 'O' else 'O'
  69.  
  70.  
Add Comment
Please, Sign In to add comment