Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- def print_board(board):
- for i in range(0, 9, 3):
- print(f" {board[i]} | {board[i+1]} | {board[i+2]} ")
- if i < 6:
- print("-----------")
- def check_win(board):
- win_patterns = [
- [0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
- [0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
- [0, 4, 8], [2, 4, 6] # Diagonals
- ]
- for pattern in win_patterns:
- print(board[pattern[0]] == board[pattern[1]] and board[pattern[1]] == board[pattern[2]])
- 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
- return True
- def clear_screen():
- os.system('cls' if os.name == 'nt' else 'clear')
- # Initialize game state
- board = [' ', ' ', ' ',
- ' ', ' ', ' ',
- ' ', ' ', ' ']
- current_player = 'X'
- game_mode = 'continue'
- print("Welcome to Tic Tac Toe! Player X goes first.")
- while True:
- print(f"\nPlayer {current_player}'s turn:")
- print_board(board)
- if game_mode == 'draw':
- break
- move = input("\nEnter position (1-9): ")
- try:
- move = int(move) - 1
- except ValueError:
- print("\nInvalid input. Please enter a number.")
- continue
- if board[move] != ' ':
- print("\nThat position is already taken! Choose another one.")
- continue
- # Update the board with player's choice
- board[move] = current_player
- clear_screen()
- # Check for win or draw
- if check_win(board):
- print_board(board)
- winner = 'X' if current_player == 'X' else 'O'
- print(f"\nPlayer {winner} wins!")
- game_mode = 'win' #IT DOESNT DO ANYTHING IF GAME_MODE IS WIN
- elif all(pos != ' ' for pos in board):
- print("\nGame ended in a draw!")
- game_mode = 'draw'
- else:
- # Switch player
- current_player = 'X' if current_player == 'O' else 'O'
Add Comment
Please, Sign In to add comment