Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

function evaluate(node):

# Check if the current game state results in a win for the player.
if is_winner(node, 'X'):
return 10 # Player X wins
elif is_winner(node, 'O'):
return -10 # Player O wins
elif is_board_full(node):
return 0 # It's a draw
else:
return 0 # No winner yet; return a neutral value

# Check if a player has won.


def is_winner(node, player):
# Check rows, columns, and diagonals for a winning combination.
for i in range(3):
if (
(node[i][0] == node[i][1] == node[i][2] == player) or
(node[0][i] == node[1][i] == node[2][i] == player)
):
return True
if (
(node[0][0] == node[1][1] == node[2][2] == player) or
(node[0][2] == node[1][1] == node[2][0] == player)
):
return True
return False

# Check if the board is full (a draw).


def is_board_full(node):
for row in node:
for cell in row:
if cell == ' ':
return False
return True

You might also like