Skip to content

Tictactoe cleanup #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified tictactoe/__pycache__/tictactoe.cpython-311.pyc
Binary file not shown.
82 changes: 28 additions & 54 deletions tictactoe/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def player(board):
count = [0, 0]
for i in range(3):
for j in range(3):
if board[i][j] == "X":
if board[i][j] == X:
count[0] = count[0] + 1
elif board[i][j] == "O":
elif board[i][j] == O:
count[1] = count[1] + 1

if count[0] > count[1]:
Expand Down Expand Up @@ -65,66 +65,34 @@ def winner(board):
"""
Returns the winner of the game, if there is one.
"""
for i in range(3):
if (
board[i][0] == board[i][1]
and board[i][0] == board[i][2]
and board[i][0] != None
):
if board[i][0] == "X":
return X
elif board[i][0] == "O":
return O
else:
return None

lines = []

# Check for all possible winning combinations
# Rows, Columns and Diagonals

for i in range(3):
if (
board[0][i] == board[1][i]
and board[1][i] == board[2][i]
and board[0][i] != None
):
if board[0][i] == "X":
return X
elif board[0][i] == "O":
return O
else:
return None

if (
board[0][0] == board[1][1]
and board[1][1] == board[2][2]
and board[0][0] != None
):
if board[0][0] == "X":
return X
elif board[0][0] == "O":
return O
else:
return None

if (
board[2][0] == board[1][1]
and board[1][1] == board[0][2]
and board[2][0] != None
):
if board[2][0] == "X":
return X
elif board[2][0] == "O":
return O
else:
return None

lines.append(board[i]) #Rows
lines.append([board[0][i], board[1][i], board[2][i]]) #Columns
lines.append([board[0][0], board[1][1], board[2][2]]) #Diagonal
lines.append([board[2][0], board[1][1], board[0][2]]) #Diagonal

# Check if any line has all three elements the same and checks what they are
for line in lines:
if line[0] is not None and line.count(line[0]) == 3:
return line[0]
return None


def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) == "X":


if winner(board) == X:
return True
elif winner(board) == "O":
elif winner(board) == O:
return True

for i in range(3):
Expand All @@ -138,9 +106,11 @@ def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == "X":

w = winner(board)
if w == X:
return 1
elif winner(board) == "O":
elif w == O:
return -1
else:
return 0
Expand All @@ -154,6 +124,10 @@ def minimax(board):
if terminal(board):
return None

#Check if the board is empty to make sure the AI always plays bottom right (which it does anyways)
if all(cell is None for row in board for cell in row):
return (2, 2)

# If X's turn
elif player(board) == X:
options = []
Expand Down