😎
CS1.dev
  • Welcome to Computer Science 1
  • Unit 2
    • Activity 2.1
    • Activity 2.2
    • Activity 2.3
    • Activity 2.4
    • Activity 2.5
    • Activity 2.6
    • Activity 2.7
    • Activity 2.9
    • Activity 2.10
    • Project 2
    • Activity 2.8
    • Activity 2.11
    • Activity 2.12
    • Activity 2.13
    • Activity 2.14
    • Activity 2.15
    • Activity 2.16
    • Activity 2.17
    • Activity 2.18
    • Activity 2.19
    • Project 3
    • Activity 2.20
    • Activity 2.21
    • Activity 2.22
    • Activity 2.23
    • Activity 2.24
    • Project 4
    • Activity 2.25
    • Activity 2.26
    • Activity 2.27
    • Activity 2.28
    • Project 5
    • Activity 2.29
    • Activity 2.30
    • Activity 2.31
    • Activity 2.32
    • Activity 2.33
    • Activity 2.34
    • Activity 2.35
    • Activity 2.36
  • Unit 3
    • Activity 3.1
    • Activity 3.2
    • Activity 3.3
    • Activity 3.4
    • Activity 3.5
    • Activity 3.6
    • Activity 3.7
    • Activity 3.8
    • Activity 3.9
    • Activity 3.10
    • Activity 3.11
    • Project 6
    • Activity 3.12
  • Activity 3.13
  • Activity 3.14
  • Activity 3.15
  • Activity 3.16
  • Project 7
  • Activity 3.17
  • Activity 3.18
  • Activity 3.19
  • Project 8
  • Linux
    • bash
    • cat
    • cd
    • chmod
    • df
    • echo
    • find
    • grep
    • less
    • ls
    • mkdir
    • more
    • pwd
    • tar
    • touch
    • unzip
    • zip
Powered by GitBook
On this page
  1. Unit 2

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()


PreviousActivity 2.30NextActivity 2.32

Last updated 3 months ago