r/pythonhelp • • Nov 05 '24

this file wont run

please can someone help i cant get it to run i get this error:

"

PS C:\Users\Sidne> & C:/Users/Sidne/AppData/Local/Programs/Python/Python313/python.exe c:/Users/Sidne/Desktop/project.py

PS C:\Users\Sidne>

"

import numpy as np

import random

# Define the TicTacToe game class

class TicTacToe:

def __init__(self):

# Initialize the game board as a 3x3 grid filled with zeros

# 0 represents an empty cell, 1 represents Player 1, and -1 represents Player 2

self.board = np.zeros((3, 3), dtype=int)

# Set the starting player; Player 1 (represented by 1) starts the game

self.current_player = 1

def reset(self):

# Resets the board to its initial empty state and sets Player 1 as the current player

self.board = np.zeros((3, 3), dtype=int)

self.current_player = 1

def available_actions(self):

# Returns a list of all available (empty) cells on the board

# Each action is represented as a tuple (i, j) for the cell coordinates

return [(i, j) for i in range(3) for j in range(3) if self.board[i, j] == 0]

def make_move(self, action):

# Make a move on the board at the specified action (i, j) if the cell is empty

if self.board[action] == 0:

# Place the current player's marker (1 or -1) in the specified cell

self.board[action] = self.current_player

# Switch to the other player for the next move

self.current_player = -self.current_player

return True # Move was successful

return False # Move was unsuccessful

def check_winner(self):

# Check if there is a winner in the current board state

# A player wins if any row, column, or diagonal adds up to 3 (Player 1) or -3 (Player -1)

# Check rows and columns for a win

for i in range(3):

# Check row i

if abs(sum(self.board[i, :])) == 3:

return self.board[i, 0] # Return the winning player (1 or -1)

# Check column i

if abs(sum(self.board[:, i])) == 3:

return self.board[0, i] # Return the winning player (1 or -1)

# Check diagonals for a win

# Primary diagonal (top-left to bottom-right)

if abs(self.board[0, 0] + self.board[1, 1] + self.board[2, 2]) == 3:

return self.board[0, 0] # Return the winning player

# Secondary diagonal (top-right to bottom-left)

if abs(self.board[0, 2] + self.board[1, 1] + self.board[2, 0]) == 3:

return self.board[0, 2] # Return the winning player

# If no winner and empty cells remain, game continues (return 0)

# If no empty cells remain, it's a draw (return None)

return 0 if any(0 in row for row in self.board) else None

def display_board(self):

# Display the current board state with X, O, and empty cells

for row in self.board:

# Convert each cell: 1 to 'X', -1 to 'O', and 0 to a blank space

print(' | '.join('X' if x == 1 else 'O' if x == -1 else ' ' for x in row))

print('-' * (3 * 2 - 1)) # Print separator line

# Define a Q-Learning agent for TicTacToe

class QLearningAgent:

def __init__(self, alpha=0.1, gamma=0.9, epsilon=0.1):

# Initialize the Q-table, which maps state-action pairs to Q-values

self.q_table = {}

# Set hyperparameters

self.alpha = alpha # Learning rate: controls how much new information overrides old Q-values

self.gamma = gamma # Discount factor: determines the importance of future rewards

self.epsilon = epsilon # Exploration rate: chance to choose a random action for exploration

def get_q_value(self, state, action):

# Return the Q-value for a given state-action pair, defaulting to 0 if not present in Q-table

return self.q_table.get((state, action), 0.0)

def choose_action(self, state, actions):

# Choose an action based on epsilon-greedy strategy

# With probability epsilon, choose a random action for exploration

if random.uniform(0, 1) < self.epsilon:

return random.choice(actions)

# Otherwise, choose the action with the highest Q-value for the current state

q_values = [self.get_q_value(state, a) for a in actions]

return actions[np.argmax(q_values)] # Select action with maximum Q-value

def update_q_value(self, state, action, reward, next_state, next_actions):

# Update the Q-value for a given state-action pair using the Q-learning formula

# Find the maximum Q-value for the next state (future reward estimation)

max_future_q = max([self.get_q_value(next_state, a) for a in next_actions], default=0)

# Get the current Q-value for the state-action pair

current_q = self.get_q_value(state, action)

# Calculate the new Q-value

new_q = current_q + self.alpha * (reward + self.gamma * max_future_q - current_q)

# Update the Q-table with the new Q-value

self.q_table[(state, action)] = new_q

# Function to train the agent on the TicTacToe game over a series of episodes

def train(agent, game, episodes=5000):

# Loop over a specified number of episodes to train the agent

for episode in range(episodes):

# Reset the game to the initial state at the start of each episode

game.reset()

# Represent the current board state as a tuple (hashable for Q-table)

state = tuple(game.board.flatten())

# Play the game until it ends (win, lose, or draw)

while True:

# Get available actions for the current state

actions = game.available_actions()

# Choose an action based on the Q-learning agent's policy

action = agent.choose_action(state, actions)

# Make the chosen move on the game board

game.make_move(action)

# Get the updated board state after the move

next_state = tuple(game.board.flatten())

# Check if there is a winner after the move

winner = game.check_winner()

# Define rewards based on game outcome

if winner == 1: # Agent wins

reward = 1

agent.update_q_value(state, action, reward, next_state, [])

break # End episode

elif winner == -1: # Opponent wins

reward = -1

agent.update_q_value(state, action, reward, next_state, [])

break # End episode

elif winner is None: # Draw

reward = 0.5

agent.update_q_value(state, action, reward, next_state, [])

break # End episode

else: # Game continues

reward = 0 # No reward yet as the game is still ongoing

# Update Q-value and continue to the next state

agent.update_q_value(state, action, reward, next_state, game.available_actions())

# Update the current state to the next state for the next loop iteration

state = next_state

0 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/bishpenguin Nov 10 '24 edited Nov 10 '24

It's still not an error.

On which line are you calling the draw board method to see any output? I don't see (and I'm on my phone, so could be wrong) where you actually tell it to output anything? Your class names are different, class q learning agent is just called agent later in the code? display board isn't called and game isn't initialized. Unless I'm missing something?