Question

In: Computer Science

Assignment 2 Tic-Tac-Toe Game system design (10 marks) Introduction This document describes the functionalities of the...

Assignment 2 Tic-Tac-Toe Game system design

Introduction This document describes the functionalities of the system and its design requirements. As first introduced in the Lab_Exercise_1, Tic-Tac-Toe is a simple game for two players. In this 1 assignment, you will implement a text-based Tic-Tac-Toe (TTT) game system that offers two game modes: 1) two human players playing against each other and 2) one human player playing against a built-in computer player in the game system. Your mission is to ensure the game playing is straightforward and easy to use.

This game is played on the “console.” That is, it is played in a UNIX terminal like in Linux and MACOS or a Windows command window. You are required to perform a storytelling/storyboard analysis to identify different components (also called subsystems) in this game system. Each subsystem corresponds to one specific functionality/role/actor/character depending on how you analyze the interactions within the game system.

For example, the TTT Drawing Subsystem that you implemented in the Lab_Exercise_1 is the subsystem responsible for displaying and drawing the game board during the game. So the functionality of TTT Drawing Subsystem is to show and draw the game board and other complex drawings needed for the game system. It, in fact, should be a collection of functions. The main control of the game system, or, the game host, will invoke these functions when showing some complex drawings, say, the game board, are needed. Another way to describe the TTT Drawing Subsystem is that it functions like a local “library” for the game system.

2 Assignment Requirements
● Functional Requirements
1) The (game) system provides a system welcome message and shows instructions on how to play this game. (10% ) 3
2) The system provides a virtual dice to allow the players to decide the order of playing. (5%)
3) The system has the functionality of two humans to play the game against each other. (10%)
4) The system also has the functionality of one human to play against the built-in computer player. (5%)
5) The system has a built-in computer player that will try to win when winning is possible. (20%)
6) During the game, the system checks and reports the outcome after each move by either player. The outcome could be a win, a draw, or a state of still in progress. (10%)
7) When the user enters the wrong input, the system will reject the input and request a re-submission. (5%)
8) Additional Requirement
○ User interaction consideration: the system should be easy to understand and easy to follow. (10%)
■ The information of the current state/stage of the game is available to the user.
● The current state of the game (i.e., win, draw, or in progress), if available.
● The current game board, if available. ● Whose turn now, if available.
● Other information you think that is necessary for playing.
■ To interact:
● Are the instructions clear?
● Easy to perform the requested infractions without mistakes or confusion?

● Required system execution flow of the game system
1) The system starts with a system welcome message.
2) Explain and provide the system commands to the user. If the user enters an incorrect command, explain and provide the system commands again. Here are the system commands: System Command Action
0 Exit the system
  1 Begin entering a new game
When the system command is 0, then exit the program. When the system command is 1, go to step 3.
3) The system then asks the user to select the game mode. If the user enters an incorrect game mode command, explain and provide the game mode command screen again. Here are the game mode commands:
Game Mode Command Action
  1 The human player plays against the computer player
  2 Two human players play against each other
When the user enters incorrect input, the system will ask the user to re-enter the selection.
4) Next, the system will interact with the user(s) to determine the play order by throwing a virtual dice. Make sure to explain the rule for determining the play order. The play order must be determined after completing this step. Then, go to step 5.
5) The tic-tac-toe game starts. During the game, the players take turns to select the cell he/she/it wants to take over. The system will accept the input from the player each time and then shows the current game board. If there is a win, the system will announce it and then go to step 2. Otherwise, the system will continue until all cells on the game board are filled. When this happens, the system will announce a draw and then go to step 2. When the user enters incorrect input, the system will ask the user to re-enter

Solutions

Expert Solution

Program in java:

//importing required class
import java.util.Scanner;
//testing the game
public class TicTacToe {
    //main method
    public static void main(String[] args) {
        //creating object for the game board
   GameBoard ttt;
   Scanner input = new Scanner(System.in);
   int x,y,pos=0,choice;
        //showing the welcome message and rules
        System.out.println("Welcome to TICTACTOE\nYou need to make 3 consecutive symbole in row wise column wise or "
                + "digonal wise to win the game.");
        while(true){
            //asking for user choice
            System.out.print("\nSystem Command Action \n0 Exit the system\n1 Begin entering a new game\nYour choice: ");
            //storing the user response
            choice=input.nextInt();
            //working as per user choice
            if(choice==0){
                //exit the program
                break;
            }
            else if(choice==1){
                //creating new board for game
                ttt = new GameBoard();
                //generating 2 random number respectively for both player
                //and comparing the values to choose, who will start the game first
                if(Math.random()>Math.random()){
                    ttt.setPlayer(ttt.X);
                }
                else{
                    ttt.setPlayer(ttt.O);
                }
                while(true){
                    //asking for user choice
                    System.out.print("\nGame Mode Command Action \n1 The human player plays against the computer player\n2 Two human players play against each other\nYour choice: ");
                    //storing the user response
                    choice=input.nextInt();
                    //working as per user choice
                    if(choice==1){
                        //showing message that who will start first
                        if(ttt.player==ttt.X){
                            System.out.println("First player will start the game");
                        }
                        else{
                            System.out.println("Second player will start the game");
                        }
                        //asking for players input and calculating the winner
                        do
                        {
                            //checking for player turn
                            //asking for user input          
                            if(ttt.player==ttt.X){
                                System.out.print("Player1(X) please enter your move (1-9): ");
                                pos=input.nextInt();
                            }
                            else{
                                pos=(int)(Math.random()*10)%9+1;
                                System.out.println("Computer(O) move (1-9): "+pos);
                            }
                            x=(pos-1)/3;
                            y=(pos-1)%3;
                            ttt.markSymbol(x, y);
                            //printing the board position
                            System.out.println(ttt.toString());
                            System.out.println();
                            //printing the winner details
                            ttt.printWinner();
                        }while(ttt.hasEmptySpace);
                        break;
                    }
                    else if(choice==2){
                        //showing message that who will start first
                        if(ttt.player==ttt.X){
                            System.out.println("First player will start the game");
                        }
                        else{
                            System.out.println("Second player will start the game");
                        }
                        //asking for players input and calculating the winner
                        do
                        {
                            //checking for player turn
                            //asking for user input          
                            if(ttt.player==ttt.X){
                                System.out.print("Player1(X) please enter your move (1-9): ");
                            }
                            else{
                                System.out.print("Player2(O) please enter your move (1-9): ");
                            }
                            pos=input.nextInt();
                            x=(pos-1)/3;
                            y=(pos-1)%3;
                            ttt.markSymbol(x, y);
                            //printing the board position
                            System.out.println(ttt.toString());
                            System.out.println();
                            //printing the winner details
                            ttt.printWinner();
                        }while(ttt.hasEmptySpace);
                        break;
                    }
                    else{
                        //error message to user and showing the commands again
                        System.out.println("Invalid input.Try Again...");
                        continue;
                    }
                }
            }
            else{
                //error message to user and showing the commands again
                System.out.println("Invalid input.Try Again...");
                continue;
            }
        }
    }
}
//class for the board
class GameBoard
{
    //variable declaration for players
    int X = 1, O = -1;
    final int FREE = 0;
    int player;
    int[][] board = new int[3][3];
    boolean hasEmptySpace = false;
    //method to set the player turn
    public void setPlayer(int player){
        this.player=player;
    }
    //marking the player choice
    public void markSymbol(int x, int y)
    {
        //checking the input constraints
   if(x<0 || x>2 || y<0 || y>2)
   {
            System.out.println("Invalid x and y coordinate");
   }
   if(board[x][y] != FREE)
   {
            System.out.println("Specified location is already use");
   }
        else{
            //marking player symbol at specified position
            board[x][y] = player;
            player = -player;
        }
    }
    //function to find the winner
    public boolean isWinner(int player)
    {
       return ((board[0][0] + board[0][1] + board[0][2] == player*3) ||
       (board[1][0] + board[1][1] + board[1][2] == player*3) ||
       (board[2][0] + board[2][1] + board[2][2] == player*3) ||
       (board[0][0] + board[1][0] + board[2][0] == player*3) ||
       (board[0][1] + board[1][1] + board[2][1] == player*3) ||
       (board[0][2] + board[1][2] + board[2][2] == player*3) ||
       (board[0][0] + board[1][1] + board[2][2] == player*3) ||
       (board[2][0] + board[1][1] + board[0][2] == player*3));
    }
    //showing the winner
    public void printWinner()
    {
       if(isWinner(X))
   {
            System.out.println("\nPlayer1 is winner");
            hasEmptySpace=false;
   }
   else if(isWinner(O))
   {
            System.out.println("\nPlayer2 is winner");
            hasEmptySpace=false;
   }
   else
   {
            if(!hasEmptySpace)
            {
       System.out.println("It's a tie");
            }
   }
    }
  
    //printing the board details
    public String toString()
    {
   StringBuilder s = new StringBuilder();
   hasEmptySpace = false;
   for(int i=0;i<3;i++)
   {
            for(int j=0;j<3;j++)
            {
                switch(board[i][j])
       {
                    case 1:
                        s.append(" X ");
                        break;
                    case -1:
                        s.append(" O ");
           break;
                    case FREE:
                        s.append("   ");
           hasEmptySpace=true;
           break;
       }
       if(j<2)
       {
                    s.append("|");
       }
            }
            if(i<2)
            {
       s.append("\n-----------\n");
            }
   }
   return s.toString();
    }
}

Output:

run:
Welcome to TICTACTOE
You need to make 3 consecutive symbole in row wise column wise or digonal wise to win the game.

System Command Action
0 Exit the system
1 Begin entering a new game
Your choice: 4
Invalid input.Try Again...

System Command Action
0 Exit the system
1 Begin entering a new game
Your choice: 1

Game Mode Command Action
1 The human player plays against the computer player
2 Two human players play against each other
Your choice: 3
Invalid input.Try Again...

Game Mode Command Action
1 The human player plays against the computer player
2 Two human players play against each other
Your choice: 1
First player will start the game
Player1(X) please enter your move (1-9): 1
X |   |
-----------
   |   |
-----------
   |   |

Computer(O) move (1-9): 6
X |   |
-----------
   |   | O
-----------
   |   |

Player1(X) please enter your move (1-9): 2
X | X |
-----------
   |   | O
-----------
   |   |

Computer(O) move (1-9): 5
X | X |
-----------
   | O | O
-----------
   |   |

Player1(X) please enter your move (1-9): 3
X | X | X
-----------
   | O | O
-----------
   |   |


Player1 is winner

System Command Action
0 Exit the system
1 Begin entering a new game
Your choice: 1

Game Mode Command Action
1 The human player plays against the computer player
2 Two human players play against each other
Your choice: 1
Second player will start the game
Computer(O) move (1-9): 3
   |   | O
-----------
   |   |
-----------
   |   |

Player1(X) please enter your move (1-9): 1
X |   | O
-----------
   |   |
-----------
   |   |

Computer(O) move (1-9): 5
X |   | O
-----------
   | O |
-----------
   |   |

Player1(X) please enter your move (1-9): 2
X | X | O
-----------
   | O |
-----------
   |   |

Computer(O) move (1-9): 8
X | X | O
-----------
   | O |
-----------
   | O |

Player1(X) please enter your move (1-9): 4
X | X | O
-----------
X | O |
-----------
   | O |

Computer(O) move (1-9): 9
X | X | O
-----------
X | O |
-----------
   | O | O

Player1(X) please enter your move (1-9): 7
X | X | O
-----------
X | O |
-----------
X | O | O


Player1 is winner

System Command Action
0 Exit the system
1 Begin entering a new game
Your choice: 0


Related Solutions

The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
How to make tic tac toe game in javascript with vue.js
How to make tic tac toe game in javascript with vue.js
Game of Tic Tac Toe with the following conditions A point system where a  move that leads...
Game of Tic Tac Toe with the following conditions A point system where a  move that leads to a winning game is given 1 point, a move that leads to a tie is given 0 point, and a  lead to a losing game will get -1 point. Grid size of 5x5 A timer that can be set for how long a game can be played 5 symbols in a row to get a point Connected lines cannot be crossed (No diagonal lines)...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes....
Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed (such as #include <stdio.h> etc). Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects...
In a game of tic tac toe. How do you check if all the positions in...
In a game of tic tac toe. How do you check if all the positions in a double array are taken, the double arrays are used to store the x's and o's?
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and...
- Make a 2-D array to represent a Tic-Tac-Toe game/grid. Place a couple of 'X's and 'O's in the grid and display it. Do some extra printing to make it look like a Tic-Tac-Toe board we are suppose to use rows and columns so just make it simple thanks!
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
If you were to write a game of tic-tac-toe, you may store the representation of the...
If you were to write a game of tic-tac-toe, you may store the representation of the game board as a two dimensional list such as   [['X', '', 'X'], ['O', 'X', ''], ['', 'O', 'X']] where each sublist is a row in the board.   Empty strings ('') denote a space that does not yet have a value. Assuming this representation, write functions (contained in the same file) that do the following: a) Create a new empty tic-tac-toe board. This function should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT