🎮 Davina's Tic Tac Toe (Noughts and Crosses)¶
This notebook demonstrates a basic implementation of a single-move Tic Tac Toe game (also known as Noughts and Crosses) using Python. It initializes a 3x3 board, allows a player to input their move, and checks for a win condition.
🧱 1. Board Initialization¶
symbols = ["O", "X", "."]
-
"O"
and"X"
are player symbols. "."
represents an empty cell on the board.
The board is a 3x3 grid initialized with "."
:
board = [
[".", ".", "."],
[".", ".", "."],
[".", ".", "."]
]
👀 2. Display Functions¶
show_symbols(symbols)
¶
Prints the available symbols (used for clarity or debugging).
show_board(board)
¶
Prints the current state of the board row by row.
🏆 3. Win Condition Check¶
check_win(board, symbol)
¶
Checks if the given player (symbol
) has won:
- ✅ Three in a row (horizontal)
- ✅ Three in a column (vertical)
- ✅ Three diagonally (left-to-right or right-to-left)
🎮 4. User Interaction (Single Move)¶
The user is prompted to:
- Enter a row number (
y
) between 0 and 2 - Enter a column number (
x
) between 0 and 2 -
Choose a symbol (
O
orX
) to place at the chosen spot
Input is validated for:
- Out-of-bounds indices
- Invalid symbols
- Attempting to place outside the board
If valid:
- The symbol is placed on the board
- The board is updated and printed
- A win check is performed immediately after the move
❗ Limitations¶
- Only one move is processed per run.
- No loop for alternating turns.
- No check for full-board draw conditions.
- No prevention of placing a symbol on an occupied cell.
✅ Sample Output¶
=== Welcome to Davina's Tic Tac Toe ===
Here's the board:
['.', '.', '.']
['.', '.', '.']
['.', '.', '.']
Give a value between 0 and 2: 0
Give another value between 0 and 2: 0
Which symbol do you want to place at that spot (O/X)? O
Here's the board:
['O', '.', '.']
['.', '.', '.']
['.', '.', '.']
O won! # (only if it happens to be a winning move)
📚 Next Steps¶
To turn this into a full game:
- Add a loop for alternating turns between players
- Track game state and move count
- Check for draw conditions when the board is full
- Prevent overwriting existing symbols
print("=== Welcome to Davina's Tic Tac Toe ===")
symbols = ["O", "X", "."]
board = [
[symbols[2], symbols[2], symbols[2]],
[symbols[2], symbols[2], symbols[2]],
[symbols[2], symbols[2], symbols[2]],
]
def show_symbols(symbols):
print("These are the available symbols:")
print(symbols)
def show_board(board):
print("Here's the board:")
for row in board:
print(row)
def check_win(board, symbol):
# Check rows
for row in board:
if all(cell == symbol for cell in row):
return True
# Check columns
for col in range(3):
if all(board[row][col] == symbol for row in range(3)):
return True
# Check diagonals
if all(board[i][i] == symbol for i in range(3)):
return True
if all(board[i][2 - i] == symbol for i in range(3)):
return True
return False
show_board(board)
y = int(input("Give a value between 0 and 2:"))
if y > len(board) - 1:
print(f"Error: y must be between 0 and 2.")
else:
row = board[y]
x = int(input("Give another value between 0 and 2"))
if x > len(row) - 1:
print(f"Error: x must be between 0 and 2.")
else:
symbol = input("Which symbol do you want to place at that spot (O/X)?")
if symbol not in symbols:
print(f"Error: symbol '{symbol}' not found.")
else:
board[y][x] = symbol
show_board(board)
if check_win(board, symbol): print(f"{symbol} won!")
=== Welcome to Davina's Tic Tac Toe === Here's the board: ['.', '.', '.'] ['.', '.', '.'] ['.', '.', '.']
Here's the board: ['O', '.', '.'] ['.', '.', '.'] ['.', '.', '.']
🎮 Davina's Full Tic Tac Toe Game (Game Loop Version)¶
This notebook features a fully playable version of
Tic Tac Toe (Noughts and Crosses) in Python.
Two players take turns placing "O"
and
"X"
on a 3x3 board. The game ends when one player
wins or the board is full (a draw).
🧱 1. Game Setup¶
Symbols:¶
symbols = ["O", "X"]
placeholder = "."
-
"O"
and"X"
are used by players. "."
represents an empty cell on the board.
Board:¶
board = [
[".", ".", "."],
[".", ".", "."],
[".", ".", "."]
]
A 3x3 matrix (list of lists) initialized with placeholders.
📺 2. Functions¶
show_board(board)
¶
Prints the board in a readable grid format:
. | . | .
. | . | .
. | . | .
check_win(board, symbol)
¶
Checks if a player (symbol
) has a winning
combination:
- All 3 cells in any row
- All 3 cells in any column
- All 3 cells in either diagonal
is_full(board)
¶
Returns True
if the board has no
"."
left (i.e., the game is a draw).
🔁 3. Game Loop¶
Key Mechanics:¶
-
Players alternate turns using
current_player
(0 or 1). -
Prompts for
row
andcolumn
inputs. -
Validates:
- That the input is within bounds (0–2)
- That the selected cell is empty
- Places the player’s symbol in the chosen cell.
-
After each move:
- Checks if the current player has won.
- Checks if the board is full (draw).
- Switches to the other player if the game continues.
Loop Termination:¶
The loop ends when:
- A player wins
- The board is full (no more valid moves)
✅ Sample Gameplay¶
=== Welcome to Davina's Tic Tac Toe ===
. | . | .
. | . | .
. | . | .
Player O's turn.
Enter row (0-2): 0
Enter column (0-2): 0
O | . | .
. | . | .
. | . | .
Player X's turn.
...
Player X wins!
🛠️ Possible Enhancements¶
- Add input for player names
- Add score tracking across rounds
- Improve display with row/column labels
- Add replay option after a win or draw
- Prevent accidental overwrite with visual cues
👩💻 Author¶
Developed by Davina as a practice project for learning Python and game logic using Jupyter Notebook.
print("=== Welcome to Davina's Tic Tac Toe ===")
symbols = ["O", "X"]
placeholder = "."
board = [
[placeholder, placeholder, placeholder],
[placeholder, placeholder, placeholder],
[placeholder, placeholder, placeholder],
]
def show_board(board):
for row in board:
print(" | ".join(row))
print()
def check_win(board, symbol):
# Check rows and columns
for i in range(3):
if all(board[i][j] == symbol for j in range(3)) or \
all(board[j][i] == symbol for j in range(3)):
return True
# Check diagonals
if all(board[i][i] == symbol for i in range(3)) or \
all(board[i][2 - i] == symbol for i in range(3)):
return True
return False
def is_full(board):
return all(cell != placeholder for row in board for cell in row)
# Game loop
current_player = 0 # 0 for O, 1 for X
while True:
show_board(board)
print(f"Player {symbols[current_player]}'s turn.")
try:
y = int(input("Enter row (0-2): "))
x = int(input("Enter column (0-2): "))
if board[y][x] != placeholder:
print("That spot is already taken. Try again.\n")
continue
board[y][x] = symbols[current_player]
if check_win(board, symbols[current_player]):
show_board(board)
print(f"Player {symbols[current_player]} wins!")
break
if is_full(board):
show_board(board)
print("It's a draw!")
break
current_player = 1 - current_player # Switch player
except (ValueError, IndexError):
print("Invalid input. Please enter numbers between 0 and 2.\n")
=== Welcome to Davina's Tic Tac Toe === . | . | . . | . | . . | . | . Player O's turn.
O | . | . . | . | . . | . | . Player X's turn.
O | X | . . | . | . . | . | . Player O's turn.
O | X | . . | O | . . | . | . Player X's turn.
O | X | . . | O | . . | . | X Player O's turn.