Skip to content
Closed
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 added tictactoe/__pycache__/tictactoe.cpython-313.pyc
Binary file not shown.
66 changes: 66 additions & 0 deletions tictactoe/game_play.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import tictactoe

def print_board(board):
"""
Prints the board in a human-readable format.
"""
for row in board:
print("|", end=" ")
for cell in row:
print(cell if cell is not None else " ", end=" | ")
print("\n" + "-" * 13)


def get_human_move(board):
"""
Prompts user to input a valid move.
"""
while True:
try:
print("Enter row and column num")
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
if (row, col) in tictactoe.actions(board):
return (row, col)
else:
print("Invalid move. Try again.\n")
except ValueError:
print("Please enter valid integers.\n")


def main():
board = tictactoe.initial_state()
human_player = input("Do you want to play as X or O? ").strip().upper()

if human_player not in ["X", "O"]:
print("Invalid choice. Defaulting to X.")
human_player = "X"

ai_player = tictactoe.O if human_player == tictactoe.X else tictactoe.X

while not tictactoe.terminal(board):
print_board(board)
current = tictactoe.player(board)

if current == human_player:
print(f"Your turn ({human_player})")
move = get_human_move(board)
else:
print(f"AI is thinking... ({ai_player})")
move = tictactoe.minimax(board)

board = tictactoe.result(board, move)

# Final board and result
print_board(board)
win = tictactoe.winner(board)
if win is None:
print("It's a draw!")
elif win == human_player:
print("You win!")
else:
print("AI wins!")


if __name__ == "__main__":
main()
68 changes: 18 additions & 50 deletions tictactoe/tictactoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,56 +65,24 @@ 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

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

win_combos = [
# Rows
[(0,0), (0,1), (0,2)],
[(1,0), (1,1), (1,2)],
[(2,0), (2,1), (2,2)],
# Columns
[(0,0), (1,0), (2,0)],
[(0,1), (1,1), (2,1)],
[(0,2), (1,2), (2,2)],
# Diagonals
[(0,0), (1,1), (2,2)],
[(0,2), (1,1), (2,0)],
]

for combo in win_combos:
a, b, c = combo
if board[a[0]][a[1]] == board[b[0]][b[1]] == board[c[0]][c[1]] != None:
return board[a[0]][a[1]]
return None


Expand Down