1. Navigate to the Notebook
Open the tic-tac-toe notebook file:
_notebooks/Foundation/F-projects/2025-08-18-tictactoe-game.ipynb
You can do this by using VSCode’s file explorer
2. Set Up Virtual Environment
Run the virtual environment setup script:
./scripts/venv.sh
This script will:
- Create or activate your Python virtual environment
- Install necessary packages
- Prepare your environment for running Jupyter notebooks
⚠️ Important
Make sure you’re in your project root directory when running this command!
3. Select the Correct Kernel
In VS Code or Jupyter, select your virtual environment kernel:
- Click on “Select Kernel” (usually in the top-right of the notebook)
- Choose “Python Environments”
- Select your venv kernel from the list
💡 Pro Tip
The kernel should show your venv path, not system Python!
4. Run the Game
Execute the code cells to start playing:
- Click the play button next to each cell
- Follow the game prompts in the output
🎉 Success!
You’re ready to play! Choose positions 1-9 to make your moves.
🔧 Troubleshooting
Common Issues
If the game doesn’t run, check that you’ve selected the correct venv kernel and that all packages are installed in your virtual environment.
# Define colors
RED = "\033[31m"
BLUE = "\033[34m"
GREEN = "\033[32m"
RESET = "\033[0m"
class Player:
def __init__(self, name, symbol):
self.name = name
self.symbol = symbol
class Board:
def __init__(self):
self.grid = [" "] * 9
def display(self):
print("\n")
print("\n")
print(" " + RED + self.grid[0] + BLUE + " │ " + RED + self.grid[1] + BLUE + " │ " + RED + self.grid[2])
print(BLUE + "───┼───┼───")
print(" " + RED + self.grid[3] + BLUE + " │ " + RED + self.grid[4] + BLUE + " │ " + RED + self.grid[5])
print(BLUE + "───┼───┼───")
print(" " + RED + self.grid[6] + BLUE + " │ " + RED + self.grid[7] + BLUE + " │ " + RED + self.grid[8])
print(RESET + "\n")
def display_reference(self):
reference = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
print("Board positions:\n")
print("\n")
print(" " + RED + reference[0] + BLUE + " │ " + RED + reference[1] + BLUE + " │ " + RED + reference[2])
print(BLUE + "───┼───┼───")
print(" " + RED + reference[3] + BLUE + " │ " + RED + reference[4] + BLUE + " │ " + RED + reference[5])
print(BLUE + "───┼───┼───")
print(" " + RED + reference[6] + BLUE + " │ " + RED + reference[7] + BLUE + " │ " + RED + reference[8])
print(RESET + "\n")
def is_full(self):
return " " not in self.grid
def make_move(self, position, symbol):
index = position - 1
if index < 0 or index > 8:
print("Invalid position. Choose a number between 1 and 9.")
return False
if self.grid[index] != " ":
print("That spot is already taken. Try again.")
return False
self.grid[index] = symbol
return True
def check_winner(self, symbol):
win_combinations = [
[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 combo in win_combinations:
if (self.grid[combo[0]] == symbol and
self.grid[combo[1]] == symbol and
self.grid[combo[2]] == symbol):
return True
return False
class TicTacToe:
def __init__(self, player1, player2):
self.board = Board()
self.players = [player1, player2]
self.current_player = player1
def switch_player(self):
self.current_player = (
self.players[1] if self.current_player == self.players[0] else self.players[0]
)
def play(self):
print("Welcome to Tic-Tac-Toe!")
print(f"{GREEN}{self.players[0].name}{RESET} is '{self.players[0].symbol}'")
print(f"{GREEN}{self.players[1].name}{RESET} is '{self.players[1].symbol}'")
print("Players take turns choosing a position (1–9).\n")
self.board.display_reference()
self.board.display()
while True:
try:
move = input(f"{self.current_player.name} ({self.current_player.symbol}), enter your move (1-9): ")
if move == "exit":
print("Exiting the game... (Official result is draw)")
raise SystemExit
else:
move = int(move)
except ValueError:
print("Invalid input. Please enter a number from 1 to 9.")
continue
if not self.board.make_move(move, self.current_player.symbol):
continue
self.board.display()
if self.board.check_winner(self.current_player.symbol):
print(f"{self.current_player.name} ({self.current_player.symbol}) wins!")
break
if self.board.is_full():
print("It's a tie!")
break
self.switch_player()
if __name__ == "__main__":
player1_name = input("Who wants to play as X? ")
player2_name = input("Who wants to play as O? ")
player1 = Player(player1_name, "X")
player2 = Player(player2_name, "O")
game = TicTacToe(player1, player2)
game.play()
Welcome to Tic-Tac-Toe!
Samarth is 'X'
Anish is 'O'
Players take turns choosing a position (1–9).
Board positions:
[31m1[34m | [31m2[34m | [31m3
[34m---+---+---
[31m4[34m | [31m5[34m | [31m6
[34m---+---+---
[31m7[34m | [31m8[34m | [31m9
[0m
[31m [34m | [31m [34m | [31m
[34m---+---+---
[31m [34m | [31m [34m | [31m
[34m---+---+---
[31m [34m | [31m [34m | [31m
[0m
[31m [34m | [31m [34m | [31m
[34m---+---+---
[31m [34m | [31mX[34m | [31m
[34m---+---+---
[31m [34m | [31m [34m | [31m
[0m
[31m [34m | [31m [34m | [31mO
[34m---+---+---
[31m [34m | [31mX[34m | [31m
[34m---+---+---
[31m [34m | [31m [34m | [31m
[0m
Exiting the game... (Official result is draw)
An exception has occurred, use %tb to see the full traceback.
SystemExit
/home/kasm-user/CompSciTeam/student/venv/lib/python3.13/site-packages/IPython/core/interactiveshell.py:3707: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)