Question
Help me to do this phase and run it
Phase 3: The task of phase 3 of this project is to extend your tic-tac-toe game such that it uses (a) 1-0 arrays to represent the board, and (b) by implementing the following tasks as separate functions 1) Display Board: The job of this function is to display the current board while taking into consideration the current state of the board. Must reflect the currently taken positions (fany) Use this after each move is done. 2) Initialize Board and no positions are taken. Use this at the beginning of every new game. Wll reset the board to a clear state where only numbers 1-9 in the available positions Validate Input for board This function takes a position from the user and makes sure the position is within the range (0-9), and at the same time the selected postion is not previously taken 4) Check Win: checking the ourrent state of the board 5) Check Tie: This function should return true ether the user, or the comp has won the game by This function should return true only if a te condition exists in the current board state. 6) Get Next Move: The job of this function is to either get the next move from the user, or from the computer. The function should use a flag to determine if it is the users turn or the computers turm, and call the comresponding functions accordingly 7) Generate Smart Move: against the user (human player). In order to achieve thait it does the following This function will strategically select the next move for the computer so that it can win e is a poss biRy forthe computer townin next game. and Determine whether make the right move to win. For example、ifany Ine(horwortal.vetCal, or dagonal) is almost complete, place the next selection to complete it (f that position is available). a b. Determine whether the user is about to win, and block the user from winning So, if the user is about to complete a line, place the computer symbol in the missing space from the line to block the user rom winning 8) Save status: save the same information on a file called gamestats. ties. For example: This function will display the game statistics on the screen (as in Phase 2), and then Just as a reminder, the program should display the number of games, wins, losses, and Number of Games: Number of Wins: 4 Number of Losses: 1 Number of Ties: 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM::

/**

                First Run it and then Read the Following.

               

                Give inputs as 11,22,33, or 12,13

                it takes input as matrices indexes

                if you want to place at 1st position then it is 11

                if you want to place at 5th then it is 22

                Player-1 will place '@' at where he claims , where Player-2 do '#'

*/

import java.util.*;

public class Tick

{

                static

                {

                                System.out.println("Do You Want to play the game (y/n)?");

                                String decision = new Scanner(System.in).nextLine();

                                if (decision.equals("n") || !decision.equals("y"))

                                {

                                                System.out.println("Than Q!");

                                                System.exit(0);

                                }

                                System.out.println("\t*******Game Rules*******");

                                System.out.println("\t1.Player has to Claim a Position.");

                                System.out.println("\t2. Who claims First Horizontally,\n\t   Vertically or Diagonally, will be the Winner");

                }

                public static void printScreen()

                {

                                System.out.println("       1   2   3");

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

                                System.out.println(" 1 | "+arr[0][0]+" | "+arr[0][1]+" | "+arr[0][2]+" |");

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

                                System.out.println(" 2 | "+arr[1][0]+" | "+arr[1][1]+" | "+arr[1][2]+" |");

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

                                System.out.println(" 3 | "+arr[2][0]+" | "+arr[2][1]+" | "+arr[2][2]+" |");

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

                }

                public static int checkWinner()

                {

                                if (arr[0][0]!="." && ((arr[0][0]==arr[0][1]) && (arr[0][0]==arr[0][2])))

                                {

                                                if (arr[0][0]=="#")

                                                {

                                                                return 2;

                                                }

                                                else return 1;

                                }

                                else if (arr[1][0]!="." && ((arr[1][0]==arr[1][1]) && (arr[1][0]==arr[1][2])))

                                {

                                                if (arr[1][0]=="#")

                                                {

                                                                return 2;

                                                }

                                                else return 1;

                                }

                                else if (arr[2][0]!="." && (arr[2][0]==arr[2][1]) && (arr[2][0]==arr[2][2]))

                                {

                                                if (arr[2][0]=="#")

                                                {

                                                                return 2;

                                                }return 1;

                                }

                                else if (arr[0][0]!="." && (arr[0][0]==arr[1][0]) && (arr[0][0]==arr[2][0]))

                                {

                                                if (arr[0][0]=="#")

                                                {

                                                                return 2;

                                                }return 1;

                                }

                                else if (arr[0][1]!="." && ((arr[0][1]==arr[1][1]) && (arr[0][1]==arr[2][1])))

                                {

                                                if (arr[0][1]=="#")

                                                {

                                                                return 2;

                                                }return 1;

                                }

                                else if (arr[0][2]!="." && ((arr[0][2]==arr[1][2]) && (arr[0][2]==arr[2][2])))

                                {

                                                if (arr[0][2]=="#")

                                                {

                                                                return 2;

                                                }return 1;

                                }

                                // Diag

                                else if (arr[0][0]!="." && (arr[0][0]==arr[1][1] && arr[0][0]==arr[2][2]))

                                {

                                                if (arr[0][0]=="#")

                                                {

                                                                return 2;

                                                }return 1;

                                }

                                else if (arr[0][2]!="." && (arr[0][2]==arr[1][1] && arr[0][2]==arr[2][0]))

                                {

                                                if (arr[0][0]=="#")

                                                {

                                                                return 2;

                                                }return 1;

                                }

                                return -1;

                }

                static String[][] arr=new String[3][3];

                public static void initialize()

                {

                                for (int i=0;i<3;i++)

                                {

                                                for (int j=0;j<3;j++)

                                                {

                                                                arr[i][j]=".";

                                                }

                                }

                }

                public static void main(String[] args)

                {

                                final int totalPossibleAttempts=9;

                                initialize();

                                int i=0;

                                while (i<totalPossibleAttempts)

                                {

                                                printScreen();

                                                if (i%2==0)

                                                {

                                                                System.out.print("Player-1 turn (@) : ");

                                                                int position = getPosition();

                                                                if (!isOccupied(position))

                                                                {

                                                                                fillAt(position,"@");

                                                                }

                                                                else

                                                                {

                                                                                System.out.println("This Position is already Occupied!");

                                                                                continue;

                                                                }

                                                }

                                                else

                                                {

                                                                System.out.print("Player-2 turn (#) : ");

                                                                int position = getPosition();

                                                                if (!isOccupied(position))

                                                                {

                                                                                fillAt(position,"#");

                                                                }

                                                                else

                                                                {

                                                                                System.out.println("This Position is already Occupied!");

                                                                                continue;

                                                                }

                                                }

                                                if (checkWinner()==2)

                                                {

                                                                printScreen();

                                                                System.out.println("Player-2 is Winner!");

                                                                return;

                                                }

                                                else if (checkWinner()==1)

                                                {

                                                                printScreen();

                                                                System.out.println("Player-1 is Winner!");

                                                                return;

                                                }

                                                i++;

                                }

                                printScreen();

                                int winner = checkWinner();

                                if (winner==1)

                                {

                                                System.out.println("Player-1 is winner ");

                                }

                                else if (winner==2)

                                {

                                                System.out.println("Player-2 is winner");

                                }

                                else if (winner==-1)

                                {

                                                System.out.println("No one win");

                                }

                }

                public static int getPosition()

                {

                                int n=0;

                                try

                                {

                                                n = (new Scanner(System.in)).nextInt();

                                }catch(Exception e){

                                                System.out.print("Invalid Position select another : ");

                                                return getPosition();

                                }

                                if ((n>10 && n<34) && (n%10==1 || n%10==2 || n%10==3))

                                {

                                                return n;

                                }

                                System.out.print("Invalid Position select another : ");

                                return getPosition();

                }

                public static boolean isOccupied(int row,int col)

                {

                                if (arr[r][c]==" ")

                                                {

                                                                return false;

                                                }

                                                return true;

                               

                }

                public static void fillAt(int position,String by)

                {

                                if (position==11)

                                {

                                                arr[0][0]=by;

                                }else if (position==12)

                                {

                                                arr[0][1]=by;

                                }else if (position==13)

                                {

                                                arr[0][2]=by;

                                }else if (position==21)

                                {

                                                arr[1][0]=by;

                                }else if (position==22)

                                {

                                                arr[1][1]=by;

                                }else if (position==23)

                                {

                                                arr[1][2]=by;

                                }else if (position==31)

                                {

                                                arr[2][0]=by;

                                }else if (position==32)

                                {

                                                arr[2][1]=by;

                                }

                                else if (position==33)

                                {

                                                arr[2][2]=by;

                                }

                }

}

OUTPUT::

CProgram Files (x86)\Dev-CpplConsolePauser.exe Do You Want to play the game (y/n)?Y *i 1.Player has to Claim a Position. 2. W

Add a comment
Know the answer?
Add Answer to:
Help me to do this phase and run it Phase 3: The task of phase 3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java language here is my code so far! i only need help with the extra...

    In java language here is my code so far! i only need help with the extra credit part For this project, you will create a Rock, Paper, Scissors game. Write a GUI program that allows a user to play Rock, Paper, Scissors against the computer. If you’re not familiar with Rock, Paper, Scissors, check out the Wikipedia page: http://en.wikipedia.org/wiki/Rock-paper-scissors This is how the program works: The user clicks a button to make their move (rock, paper, or scissors). The program...

  • A subtraction game Subtraction games are two-player games in which there is a pile of objects,...

    A subtraction game Subtraction games are two-player games in which there is a pile of objects, say coins. There are two players, Alice and Bob, who alternate turns subtracting 4.9. A SUBTRACTION GAME 19 from the pile some number of coins belonging to a set S (the subtraction set). Alice goes first. The first player who is unable to make a legal move loses. For example, suppose the initial pile contains 5 coins, and each player can, on his turn,...

  • Introduction Write a MIPS program to allow a user to play a simple variation of the...

    Introduction Write a MIPS program to allow a user to play a simple variation of the game BINGO. Your program should display the following game board and the player wants to choose a location to mark BINGO A position can be marked by entering its column letter (eit B', 'I', 'N' 'G', or CO and row number (either 1, 2, 3, 4, or 5). An 'X' should mark the positions already marked. An underscore, e C 1 should represent unmarked...

  • Question 3 [10 points] Bob and Doug play a lot of Ping-Pong, but Doug is a much better player, and wins 90% of their games. To make up for this, if Doug wins a game he will spot Bob tive points in th...

    Question 3 [10 points] Bob and Doug play a lot of Ping-Pong, but Doug is a much better player, and wins 90% of their games. To make up for this, if Doug wins a game he will spot Bob tive points in their next game. if Doug wins again he will spot Bob ten points the next game, and if he still wins the next game he will spot him ifteen points, and continue to spot hinm ifteen points as...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Problem 3. In the game of tennis, the first player to win four points wins the...

    Problem 3. In the game of tennis, the first player to win four points wins the game as long as the winner's total is at least two points more than the opponent. Thus if the game is tied at 3-3(Deuce"), then the game is not decided by the next point, but must go on until one player has two points more than the opponent's score. Assume that the server has a constant probability p of winning each point, independently of...

  • Question 9 [10 points] Bob and Doug play a lot of Ping-Pong, but Doug is a much better player, an...

    Question 9 [10 points] Bob and Doug play a lot of Ping-Pong, but Doug is a much better player, and wins 90% of their games. To make up for this, if Doug wins a game he will spot Bob five points in their next game. If Doug wins again he will spot Bob ten points the next game, and if he still wins the next game he will spot him fifteen points, and continue to spot him fifteen points as...

  • Question 9 [10 points] Bob and Doug play a lot of Ping-Pong, but Doug is a much better player, an...

    Question 9 [10 points] Bob and Doug play a lot of Ping-Pong, but Doug is a much better player, and wins 70% of their games To make up for this, if Doug wins a game he will spot Bob five points in their next game. If Doug wins again he will spot Bob ten points the next game, and if he still wins the next game he will spot him fifteen points, and continue to spot him fifteen points as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT