Activity 2.31
Source Code
import math
import random
def main():
"""
Main function that initializes the game and presents a menu system to the user.
Allows selection between different game modes, including a two-player game, playing
against an AI, viewing scores, game history, and exiting the game.
"""
# Create a 3x3 board initialized with "-" (empty spaces)
done = False # Variable to track if the game loop should continue
while not done:
# Display the menu options
print("Menu System")
print("E1 - Example 1")
print("\nTIC-TAC-TOE Game")
print("0. Practice")
print("1. Two-player game")
print("2. Play against AI")
print("3. View score")
print("4. View game history")
print("5. Exit")
print("Q - Quit")
# Get the user's choice
choice = input("Choice: ").upper()
if choice == "E1":
print("Example 1")
# Display the board as an example
elif choice == "1":
# Start a two-player game
two_player()
elif choice == "Q":
# Exit the game
print("Quitting!")
done = True
else:
# Handle invalid inputs
print("Invalid choice")
def print_matrix(matrix):
"""
Function to print the current state of the Tic-Tac-Toe board.
It iterates over each row and prints the elements in a structured format.
"""
for row in matrix:
for element in row:
print(element, end=" ") # Print each element with a space
print() # Move to the next line after each row
def two_player(board):
"""
Function to handle a two-player Tic-Tac-Toe game.
This function should:
- Allow two players to take turns making moves
- Display the updated board after each move
- Check for a win or draw condition
- Announce the winner or a tie when the game ends
"""
pass # Placeholder for implementation
def make_move(board, row, col, player):
"""
Function to place a player's marker ('X' or 'O') on the board at the given position.
- board: The game board (2D list)
- row, col: The coordinates where the player wants to place their marker
- player: The character representing the player ('X' or 'O')
This function should:
- Check if the chosen position is valid and unoccupied
- Update the board with the player's move
"""
pass # Placeholder for implementation
def check_win(board, player):
"""
Function to check if a player has won the game.
- board: The game board (2D list)
- player: The character representing the player ('X' or 'O')
This function should:
- Check all rows for a winning line
- Check all columns for a winning line
- Check both diagonals for a winning line
- Return True if the player has won, otherwise return False
"""
pass # Placeholder for implementation
def is_board_full(board):
"""
Function to check if the board is full (i.e., no empty spaces left).
- board: The game board (2D list)
This function should:
- Iterate through the board and check if any cell is still empty ("-")
- Return True if the board is full, otherwise return False
"""
pass # Placeholder for implementation
def ai_move(board, ai_player):
"""
Function for the AI to make a move.
- board: The game board (2D list)
- ai_player: The character representing the AI ('X' or 'O')
The AI should follow this logic:
1. If it has a winning move, take it.
2. If the opponent has a winning move, block it.
3. If the center space is available, take it.
4. Otherwise, choose a random available space.
"""
pass # Placeholder for implementation
# Call the main function to start the game (DO NOT DELETE)
main()
Last updated