Skip to content

agniveshsp/tic-tac-toe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tic Tac Toe

A simple console based 'tic tac toe' game that has a player vs player mode as well as a player vs Ai mode.


Instructions

How to run the program and play the game.

  1. Run main.py
  2. Press 'y' to play against computer, 'n' to play against another player.
  3. Enter a number ranging from 1-9 to mark your marker.
  4. When the game ends press 'y' for a rematch or 'n' to exit.

Game Mechanics

When the program starts, it asks the user whether they want to play against a computer or another player. If the user chooses to play with another player, the program assigns the 'X' mark to player 1 and 'O' mark to player 2. By convention, 'X' moves first in the first round. Each box on the game board is denoted by a number from 1 to 9. These numbers are keys in a dictionary where 'X' and 'O' are assigned.

After each player's move, the program checks if any three markers in a horizontal row, vertical row, or diagonal row match. If they do, the program announces the game is over and prompts for a rematch. If it's the second round, then 'O' plays first. If any of the plots a player chooses have already been used, the program asks them to pick another plot.

If the player chooses to play against the computer, both the user and computer are assigned a random player_id, which can be 'P1' or 'P2'. By convention, 'P1' moves first. If it's the AI's turn, a random box is marked with an 'X' and prompts the user for their move. If the user marks the center plot and the AI does not mark one of the corner plots, it means victory is guaranteed for the user. So, using randomization, the AI chooses a corner plot to mark its next move. The AI prioritizes its moves as follows: first, it checks for a winning move where two plots are marked with the AI's marker and one plot is empty. Next, it looks for defensive moves where two plots are marked with the user's marker and one plot is empty. The AI will occupy this plot to prevent the user from winning. Then, it checks for offensive moves, where the AI scans all possible directions for any plots with two empty boxes to mark its next move. Finally, if none of the conditions are met, the AI randomly chooses an empty plot and marks it. To make the AI more human-like, it is given a random error chance. This means that instead of defending and preventing the user from winning, the AI may make a plot randomly chosen between offense and random boxes. Each move is followed by checking arrays to see if any of the three markers match. If the game is over and all plots are filled with a marker, the game ends in a draw.


Documentation

Modules

main

         main.is_game_over(mode, player_id)[source]

                Checks if the current game has ended.

                Args:

                   mode (Logic): A subclass which inherits from Logic. (VsPlayer , VsAi)

                   player_id (str): A string representing the id of the player.(“P1”,”P2”)

                Returns:

                   True if the game is won or ended in draw.

          main.player_vs_player()[source]

                Function that handles the player vs player gameplay.

                Returns: None

          main.player_vs_ai()[source]

                Function that handles the player vs ai gameplay.

                Returns: None

logic

    class logic.Logic[source]

         Bases: object

         player_turn(player_id)[source]

                Takes user inputs and assigns them to an empty plot on the game board.

                Parameters:

                   player_id (str): A string representing the id of the player.(“P1”,”P2”) :type player_id: str

                Returns: None

         game_status_check(player_id)[source]

                Checks if the markers on the board satisfy the condition for a win or draw.
                If three of the markers match horizontally,vertically or diagonally then condition is met.
                If there are no more empty plots left on the board then game ends in a draw.

                Parameters:

                   player_id (str) – A string representing the id of the player.(“P1”,”P2”)

                Returns:

                   Returns the winning marker, ‘X’ or ‘O’. If the game ends in a draw, return ‘Draw’.

         update_dictionary_array()[source]

                Updates horizontal,vertical and diagonal arrays with updated values from the board.

                Returns: None

         reset_board()[source]

                Resets the Tic Tac Toe board (board.dict) to its default stateand resets the p_list.

                Returns: None

         score_update(mark)[source]

                Updates the score of the first player to match three markers.

                Parameters:

                   mark – either “X”, “O” or None. Default is None

                Returns:

                   A tuple consisting of player 1 score and player 2 score.

    class logic.VsAi(ai_id, player_id, corner_prob, defence_prob, offense_prob, random_prob)[source]

         Bases: Logic

              Instantiates a subclass object of Logic class.

                Args:

                   ai_id(str): Ai id , "P1" or "P2".

                   player_id(str): Player id , "P1" or "P2".

                   corner_prob(float): Probability that Ai marks a corner after player marks the center plot. Min-0, Max-1.

                   defence_prob(float): Probability that Ai prioritizes a defensive move. Min-0, Max-1.

                   offense_prob(float): Probability that Ai prioritizes a offensive move. Min-0, Max-1.

                   random_prob(float): Probability that Ai prioritizes a random move. Min-0, Max-1.

                Raises:

                   COUNTERROR : defence_prob,offense_prob and random_prob values should add upto 1.

         ai_turn()[source]

                Method responsible for ai moves on the board.

                Returns: None

         linear_check(linear_list, start_key, iteration_increment, row_start=0, row_increment=0)[source]

                Checks horizontal or vertical arrays and appends the moves that Ai can make to suitable lists.

                Parameters:

                   linear_list (list) – A horizontally or vertically sliced list from the game board.

                   start_key (int) – The number in which the iteration starts.

                   iteration_increment (int) – Increment after each loop.

                   row (int_,_ optional) –Starting number of the row. Default is 0.

                   row_increment (int_,_ optional) – Number in which rows are incremented. Default is 0.

                Returns: None

         diagonal_check(diagonal_list, start_key, iteration_increment)[source]

                Checks diagonal arrays and appends the moves that Ai can make to suitable lists.

                Parameters:

                   diagonal_list (list) – A diagonally sliced list from the game board.

                   start_key (int) – The number in which the iteration starts.

                   iteration_increment (int) – Increment after each loop.

                Returns: None

    class logic.VsPlayer[source]

         Bases: Logic

              Instantiates a subclass object of Logic class.

game_display

    class game_display.GameDisplay(vs_mode, ai_is_p2=False)[source]

         Bases: object

               Instantiates a GameDisplay object.

                Args:

                   mode(str):Current gamemode. 'pvp' for player vs player and 'ai' for player vs ai.

                   ai_is_p2(bool):True if Ai is playing 'O'. Default is False

         static console_clear()[source]

              Static method that clears the console and adds the title art.

                Returns: None

         static welcome_screen()[source]

              Welcome screen displayed once when the program starts.Is a static method.

                Returns:None

         display_board(board_dict, scoretuple=(0, 0))[source]

              Configures and Displays the game board on the console.

                Parameters:

                   board_dict (dict) – Dictionary representing the game board.

                   scoretuple (tuple) – Tuple with 0th element being player 1 score and 1st element being player 2 score.

                Returns:None

         game_result(marker)[source]

              Prints the game result on to the console.

                Parameters:

                   marker (str) – “X”,”O” or “Draw”.

                Returns:None


Flowchart

Class Diagrams

classdiagarram

main
    start

mainrun

    functions

mainfunc

logic

logicclass

gamedisplay

gamedisplay

About

Console based tic tac toe game with ai opponent.

Topics

Resources

Stars

Watchers

Forks

Languages