Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description (50 points) Code a Tic TacToe class for the forth game in Homework 1

ID: 3778249 • Letter: D

Question

Description

(50 points) Code a TicTacToe class for the forth game in Homework 12, that allows the computer plays a 3 by 3 Tic Tac Toe game against a human player. This game displays the lines and the position of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when there is a winner and tell who won. The player is then asked if he would like to play again. If the game is played the second time the computer is first to move. The player and computer (making random but valid move) take turns being first. Your program should duplicate the output found in the Sample Output From a Test Run below.

Extra Credits Opportunities:


Extra credit (20 points): Code a TicTacToeExpendable class in such way that a player is allowed to decide the dimension of the game board (3 or above) by entering an integer number. Then the game starts on a game board on the desired dimension.
Extra credit (30 points): Code a TicTacToeSmart class so the computer plays a 3 X 3 Tie Tac Toe game to win. When it is the computer’s move the first thing to determine is if there is a move that will win the game. If the computer does not have a winning move then determine if a computer move will block the human from winning. Code some methods to make the computer smart to win.
Extra credit (50 points): Code a TicTacToeSmartExpend class so the computer plays Tic Tac Toe game on a expendable game board against a human player to win.

Here are some Tic tac toe design considerations:

Understand two-dimensional arrays.

Play the game using a three by three char type array.
       ___ ___ ___
       |___|___|___|
       |___|___|___|
       |___|___|___|

Know the index locations of the characters X and O when they are placed.

An X or O can be placed on the game board by assigning the character X or O to the proper position

Design the game at a high level. Each level is then broken down further (by defining more methods; mostly private). High level might look like this

While there is not a winner and the count of moves is less than 9
Add one to the number of moves
If it is my turn then I choose a move
Else the computer chooses a move
If there are 9 moves then the game is over.
If there is not a winner it is a “cats” game.

Suggestion of methods:

A method that introduces the game rules.

A method shows the game board position numbers.

A method checks if a move is legal. i.e. only within 1 through 6 and if the position is not already occupied.

A method to make a mark on the game board

A method to display the game board

A method to "clean" the game board to start anew.

Check if the current board has a winnerA method for player to make a move

horizontally

vertically

diagonally (from upper left to lower right or from upper right to lower left)

A method for computer to make a move (randomly or intelligently).

A method for a human player to make a move.

Others.......

Sample Interactions and Output From a Test Run of the Tic Tac Toe Game


        3 by 3 Tic Tac Toe Game
I am very happy to play this game with you. When we play, you will be the X and I will be the O
We'll take turns to pick moves; one at a time, and take turn to start. I'll let you start first in
the first game. To pick a move, simply enter an integer number between 1 and 9 inclusive
starting from 1 on top row, left to right, continued to the next row, from left to right again,
and so on. Natually, it's invalid to pick a spot already taken by any of the previous moves.
Whoever gets 3 in a row, either horizontally, dianglelly or vertically first, wins the game.
            May the best player wins.


            Game Board Position
        ____________________________

        |    1   |    2   |    3   |       
        ____________________________

        |    4   |    5   |    6   |       
        ____________________________

        |    7   |    8   |    9   |       
        ____________________________


Enter your move: 7
        ____________________________

        |        |        |        |       
        ____________________________

        |        |        |        |       
        ____________________________

        |    X   |        |        |       
        ____________________________


My turn, go to 5
        ____________________________

        |        |        |        |       
        ____________________________

        |        |    O   |        |       
        ____________________________

        |    X   |        |        |       
        ____________________________


Enter your move: 1
        ____________________________

        |    X   |        |        |       
        ____________________________

        |        |    O   |        |       
        ____________________________

        |    X   |        |        |       
        ____________________________


My turn, go to 6
        ____________________________

        |    X   |        |        |       
        ____________________________

        |        |    O   |    O   |       
        ____________________________

        |    X   |        |        |       
        ____________________________


Enter your move: 2
        ____________________________

        |    X   |    X   |        |       
        ____________________________

        |        |    O   |    O   |       
        ____________________________

        |    X   |        |        |       
        ____________________________


My turn, go to 4
        ____________________________

        |    X   |    X   |        |       
        ____________________________

        |    O   |    O   |    O   |       
        ____________________________

        |    X   |        |        |       
        ____________________________


        ******** Ha, I Won ********

Do you like to play this game again? YES or NO: yes

My turn, go to 6
        ____________________________

        |        |        |        |       
        ____________________________

        |        |        |    O   |       
        ____________________________

        |        |        |        |       
        ____________________________


Enter your move: 5
        ____________________________

        |        |        |        |       
        ____________________________

        |        |    X   |    O   |       
        ____________________________

        |        |        |        |       
        ____________________________


My turn, go to 2
        ____________________________

        |        |    O   |        |       
        ____________________________

        |        |    X   |    O   |       
        ____________________________

        |        |        |        |       
        ____________________________


Enter your move: 3
        ____________________________

        |        |    O   |    X   |       
        ____________________________

        |        |    X   |    O   |       
        ____________________________

        |        |        |        |       
        ____________________________


My turn, go to 4
        ____________________________

        |        |    O   |    X   |       
        ____________________________

        |    O   |    X   |    O   |       
        ____________________________

        |        |        |        |       
        ____________________________


Enter your move: 7
        ____________________________

        |        |    O   |    X   |       
        ____________________________

        |    O   |    X   |    O   |       
        ____________________________

        |    X   |        |        |       
        ____________________________

     ******** Smart Move! You Won. *********

Do you like to play this game again? YES or NO: Yes

Enter your move: 1
        ____________________________

        |    X   |        |        |       
        ____________________________

        |        |        |        |       
        ____________________________

        |        |        |        |       
        ____________________________


-> -> -> -> ...........

My turn, go to 8
        ____________________________

        |    X   |    O |    O   |       
        ____________________________

        |    O   | X   |    X |       
        ____________________________

        |    X   |    O   |    O |       
        ____________________________


        ****** You Played a Tough Game. It's a Cats Game *******


Do you like to play this game again? YES or NO: No

Sorry to see you go. Be sure to try our other games.
****************************************************

(50 points) After successfully creating the Tic Tac Toe game, integrates all games (Die Game, Guess Game and Tic Tac Toe Game) into your Menu class so that when a user selects one of the three games from the menu, the corresponding game will start. Since we only have three games at this time, remember to modify your Menu class to show only four options (three game choices and the Exit option to end the program).

Sample Output from a Test Run of the Entire Game System


    CSC200-02N Game System
    ========================

Welcome to CSC200-02N Game Zone. There are four games to choose from, just enter the game number to start
1: A Die Game
2: A Guess Game
3: A Tic Tac Toe Game play against the computer

4. Exit

Enter your game choice now: 3

        3 by 3 Tic Tac Toe Game
I am very happy to play this game with you. When we play, you will be the X and I will be the O
We'll take turns to pick moves; one at a time, and take turn to start. I'll let you start first in
the first game. To pick a move, simply enter an integer number between 1 and 9 inclusive
starting from 1 on top row, left to right, continued to the next row, from left to right again,
and so on. Natually, it's invalid to pick a spot already taken by any of the previous moves.
Whoever gets 3 in a row, either horizontally, dianglelly or vertically first, wins the game.
            May the best player wins.


            Game Board Position
        ____________________________

        |    1   |    2   |    3   |       
        ____________________________

        |    4   |    5   |    6   |       
        ____________________________

        |    7   |    8   |    9   |       
        ____________________________


Enter your move: 5
        ____________________________

        |        |        |        |       
        ____________________________

        |        |    X   |        |       
        ____________________________

        |        |        |        |       
        ____________________________


My turn, go to 7
        ____________________________

        |        |        |        |       
        ____________________________

        |        |    X   |        |       
        ____________________________

        |    O   |        |        |       
        ____________________________


Enter your move: 6
        ____________________________

        |        |        |        |       
        ____________________________

        |        |    X   |    X   |       
        ____________________________

        |    O   |        |        |       
        ____________________________


My turn, go to 4
        ____________________________

        |        |        |        |       
        ____________________________

        |    O   |    X   |    X   |       
        ____________________________

        |    O   |        |        |       
        ____________________________


Enter your move: 9
        ____________________________

        |        |        |        |       
        ____________________________

        |    O   |    X   |    X   |       
        ____________________________

        |    O   |        |    X   |       
        ____________________________


My turn, go to 2
        ____________________________

        |        |    O   |        |       
        ____________________________

        |    O   |    X   |    X   |       
        ____________________________

        |    O   |        |    X   |       
        ____________________________


Enter your move: 1
        ____________________________

        |    X   |    O   |        |       
        ____________________________

        |    O   |    X   |    X   |       
        ____________________________

        |    O   |        |    X   |       
        ____________________________


        ******** Smart Move! You Won. *********

Do you like to play this game again? YES or NO: no

Sorry to see you go. Be sure to try our other games.
****************************************************


1: A Die Game
2: A Guess Game
3: A Tic Tac Toe Game play against the computer

4. Exit

Enter your game choice now: 2

        Welcome to the Guessing Game

To play this game, you need to guess a five-digit secret number
After each attempt, if incorrect, there will be two tips given to assist you:
    1. the number of correct digits that are in the proper position, and
    2. the sum of the correct digits. If you like the game, you can
Use these tips to help you guess again, until you guessed the secret number correctly.
repeat the game again to you heart's content.

               Let's get started.

Enter your best guess (a five-digit number): 70000
|:-( Your guess is incorrect. Use the following tips to help you try again:
    Tip #1 - 1 digit in your number match that in the secret number
    Tip #2 - The sum of these correct digits is: 7

Enter your best guess (a five-digit number): 73000
|:-( Your guess is incorrect. Use the following tips to help you try again:
    Tip #1 - 2 digit in your number match that in the secret number
    Tip #2 - The sum of these correct digits is: 10

Enter your best guess (a five-digit number): 73800
|:-( Your guess is incorrect. Use the following tips to help you try again:
    Tip #1 - 3 digit in your number match that in the secret number
    Tip #2 - The sum of these correct digits is: 18

Enter your best guess (a five-digit number): 73843

|:-)You got it!! It took you 4 attempts to guess the secret number

Do you like to play this game again? YES or NO no

        Thanks for playing the guessing game with me. Come back soon.
        *************************************************************


1: A Die Game
2: A Guess Game
3: A Tic Tac Toe Game play against the computer

4. Exit

Enter your game choice now: 1
        Welcome to the Dice Throwing Game

This game will allow you to enter a die face number
followed by the number of time in a row you like to see the number
being thrown by the computer. In return, the game will tell you
how many times the dice had been thrown to get what you wanted.

Enter the die face number you want: 5

How many times you like to see this number in a row? 2

To get 5 2 times in a row, it took 99 throws.


Do you like to play this game again? YES or NO: no

        Thanks for playing the die game with me. Come back soon.
        ********************************************************


1: A Die Game
2: A Guess Game
3: A Tic Tac Toe Game play against the computer

4. Exit

Enter your game choice now: 4

Thanks for playing. Come again soon

Requirements

As with the other parts of this game homework, your code must meet all of the following criterion in order to receive any credit. You will definitely receive a ZERO this time even if you failed to meet just one of the following :

comment lines for your name, due date and description for each method

use meaningful identifiers and with proper indentation

produce correct result with the exact format and wording shown in Sample Outputs above, corresponding to user inputs with no syntax, logic or run-time error.

create and use no less than fifteen (15) methods in the TicTacToe class (or other variation of the class), not including the main method, each doing something meaningful and/or returns useful value. Check with your learning facilitator if you are not sure a method you designed is indeed doing something meaningful.

Submitted required deliverable(s) through the proper channel.

Deliverable and Submission

In ONE ATTEMPT by the deadline, attach and submit the following through the submission area of this assignment.

for 50 points - TicTacToe.java

for 70 points - TicTacToeExpendable.java

for 80 points - TicTacToeSmart.java

for 100 points -

TicTacToeSmartExpend.java, OR

DieGame.java, GuessNumberGame.java, TicTacToe.java and Menu.java

for 120 points - DieGame.java, GuessNumberGame.java, TicTacToeExpendable.java, and Menu.java

for 130 points - DieGame.java, GuessNumberGame.java, TicTacToeSmart.java and Menu.java

for 150 points - DieGame.java, GuessNumberGame.java, TicTacToeSmartExpend.java and Menu.java

Grading Note: If you received a zero from any of the previous game parts (part I, part II or part III), you will receive up to 25 points (50%) for each part, should all parts worked successfully together with this submission.

Explanation / Answer

import java.util.Scanner;

public class TTTConsoleNonOO2P {

   public static final int EMPTY = 0;

   public static final int CROSS = 1;

   public static final int NOUGHT = 2;

   public static final int PLAYING = 0;

   public static final int DRAW = 1;

   public static final int CROSS_WON = 2;

   public static final int NOUGHT_WON = 3;

   public static final int ROWS = 3, COLS = 3;

   public static int[][] board = new int[ROWS][COLS];

   public static int currentState;

   public static int currentPlayer;

   public static int currntRow, currentCol;

   public static Scanner in = new Scanner(System.in);

   public static void main(String[] args) {

      initGame();

      do {

         playerMove(currentPlayer);

         updateGame(currentPlayer, currntRow, currentCol);

         printBoard();

         if (currentState == CROSS_WON) {

            System.out.println("'X' won! Bye!");

         } else if (currentState == NOUGHT_WON) {

            System.out.println("'O' won! Bye!");

         } else if (currentState == DRAW) {

            System.out.println("It's a Draw! Bye!");

         }

         currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;

      } while (currentState == PLAYING);   }

      public static void initGame() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            board[row][col] = EMPTY;

         }

      }

      currentState = PLAYING;

      currentPlayer = CROSS;    }

  

   public static void playerMove(int theSeed) {

      boolean validInput = false;

      do {

         if (theSeed == CROSS) {

            System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");

         } else {

            System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");

         }

         int row = in.nextInt() - 1;

         int col = in.nextInt() - 1;

         if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {

            currntRow = row;

            currentCol = col;

            board[currntRow][currentCol] = theSeed;

            validInput = true;

         } else {

            System.out.println("This move at (" + (row + 1) + "," + (col + 1)

                  + ") is not valid. Try again...");

         }

      } while (!validInput);    }

  

   public static void updateGame(int theSeed, int currentRow, int currentCol) {

      if (hasWon(theSeed, currentRow, currentCol)) {

         currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;

      } else if (isDraw()) {

         currentState = DRAW;

      }

         }

  

   public static boolean isDraw() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            if (board[row][col] == EMPTY) {

               return false;             }

         }

      }

      return true;

   }

  

   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {

      return (board[currentRow][0] == theSeed        

                   && board[currentRow][1] == theSeed

                   && board[currentRow][2] == theSeed

              || board[0][currentCol] == theSeed     

                   && board[1][currentCol] == theSeed

                   && board[2][currentCol] == theSeed

              || currentRow == currentCol           

                   && board[0][0] == theSeed

                   && board[1][1] == theSeed

                   && board[2][2] == theSeed

              || currentRow + currentCol == 2

                   && board[0][2] == theSeed

                   && board[1][1] == theSeed

                   && board[2][0] == theSeed);

   }

   public static void printBoard() {

      for (int row = 0; row < ROWS; ++row) {

         for (int col = 0; col < COLS; ++col) {

            printCell(board[row][col]);

            if (col != COLS - 1) {

               System.out.print("|");  

            }

         }

         System.out.println();

         if (row != ROWS - 1) {

            System.out.println("-----------");

         }

      }

      System.out.println();

   }

   public static void printCell(int content) {

      switch (content) {

         case EMPTY: System.out.print("   "); break;

         case NOUGHT: System.out.print(" O "); break;

         case CROSS: System.out.print(" X "); break;

      }

   }

}