Question

In: Computer Science

USING JAVA Program a playable version of TicTacToe. It should be: •Playable by two players •Show...

USING JAVA

Program a playable version of TicTacToe.

It should be:

•Playable by two players

•Show the board before asking each player for input

•Instruct the player on how to play at the start of the game

•Implement the standard rules of TicTacToe

Please make sure it's not a picture because I might not understand your handwriting or I might get the capitalization wrong and Java is case sensitive.

Solutions

Expert Solution

//Create a class TicTacToe.java


import java.util.Scanner;

public class TicTacToe {

static Scanner in;

private static char[][] board = new char[3][3];
private static char turn = 'X';

public static void writeBoard() {
System.out.println("------------------");
System.out.println("|R\\C| 1 | 2 | 3 |");
System.out.println("|1| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | ");
System.out.println("------------------");
System.out.println("|2| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | ");
System.out.println("------------------");
System.out.println("|3| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | ");
}

public static void newGame() {
System.out.println("Enter X's or O's position e.g.(1 1) and position always starts from 1 not 0 ");
System.out.println("New Game: X goes first.");
System.out.println("");
board = new char[3][3];
board[0][0] = ' ';
board[0][1] = ' ';
board[0][2] = ' ';
board[1][0] = ' ';
board[1][1] = ' ';
board[1][2] = ' ';
board[2][0] = ' ';
board[2][1] = ' ';
board[2][2] = ' ';
turn = 'X';
}

public static void getMove() {
System.out.println(turn + "'turn");
System.out.println("Where do what your " + turn + " placed ?");

System.out.println("Please enter row number and column number separated by space");

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

if (input.contains(" ")) {
String data[] = input.split(" ");
int row = Integer.parseInt(data[0]);
int column = Integer.parseInt(data[1]);

System.out.println("You have entered row #" + data[0] + " and column #" + data[1]);

System.out.println("Thank you for your selection");

if (board[row - 1][column - 1] == 'X' || board[row - 1][column - 1] == 'O') {
System.out.println("That cell is already taken. ");
System.out.println("Please make another selection");
System.out.println("");
getMove();
} else {
board[row - 1][column - 1] = turn;
if (turn == 'X') {
turn = 'O';
} else {
turn = 'X';
}
}

} else {
System.out.println("Wrong input.. Try Again");
getMove();
}
}

public static boolean winner() {
boolean flag = false;

for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = "" + board[0][0] + board[0][1] + board[0][2];
break;
case 1:
line = "" + board[1][0] + board[1][1] + board[1][2];
break;
case 2:
line = "" + board[2][0] + board[2][1] + board[2][2];
break;
case 3:
line = "" + board[0][0] + board[1][0] + board[2][0];
break;
case 4:
line = "" + board[0][1] + board[1][1] + board[2][1];
break;
case 5:
line = "" + board[0][2] + board[1][2] + board[2][2];
break;
case 6:
line = "" + board[0][0] + board[1][1] + board[2][2];
break;
case 7:
line = "" + board[0][2] + board[1][1] + board[2][0];
break;
}
if (line.equals("XXX")) {
System.out.println("X IS THE WINNER !!!!");
flag = true;
return flag;

} else if (line.equals("OOO")) {
System.out.println("O IS THE WINNER !!!!");
flag = true;
return flag;
}

}
int a=0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a++;
if (board[i][j]==' ') {
break;
} else if (a == 9) {
System.out.println("DRAW WINNER's BOARD");
flag = true;
return flag;
}

}
}
return flag;
}

public static void main(String[] args) {
TicTacToe.newGame();
while(!TicTacToe.winner())
{
TicTacToe.writeBoard();
TicTacToe.getMove();
}
  
  
}
}

//create a driver class TicTacToeDriver.java

public class TicTacToeDriver{
public static void main(String[] args) {
TicTacToe.newGame();
while(!TicTacToe.winner())
{
TicTacToe.writeBoard();
TicTacToe.getMove();
}
  
  
}
}

//run the TicTacToeDriver.java program

_______________________________________OUTPUT_____________________________________________

Enter X's or O's position e.g.(1 1) and position always starts from 1 not 0
New Game: X goes first.

------------------
|R\C| 1 | 2 | 3 |
|1| | | |
------------------
|2| | | |
------------------
|3| | | |
X'turn
Where do what your X placed ?
Please enter row number and column number separated by space
1 1
You have entered row #1 and column #1
Thank you for your selection
------------------
|R\C| 1 | 2 | 3 |
|1| X | | |
------------------
|2| | | |
------------------
|3| | | |
O'turn
Where do what your O placed ?
Please enter row number and column number separated by space
1 2
You have entered row #1 and column #2
Thank you for your selection
------------------
|R\C| 1 | 2 | 3 |
|1| X | O | |
------------------
|2| | | |
------------------
|3| | | |
X'turn
Where do what your X placed ?
Please enter row number and column number separated by space
2 2
You have entered row #2 and column #2
Thank you for your selection
------------------
|R\C| 1 | 2 | 3 |
|1| X | O | |
------------------
|2| | X | |
------------------
|3| | | |
O'turn
Where do what your O placed ?
Please enter row number and column number separated by space
3 3
You have entered row #3 and column #3
Thank you for your selection
------------------
|R\C| 1 | 2 | 3 |
|1| X | O | |
------------------
|2| | X | |
------------------
|3| | | O |
X'turn
Where do what your X placed ?
Please enter row number and column number separated by space
2 1
You have entered row #2 and column #1
Thank you for your selection
------------------
|R\C| 1 | 2 | 3 |
|1| X | O | |
------------------
|2| X | X | |
------------------
|3| | | O |
O'turn
Where do what your O placed ?
Please enter row number and column number separated by space
3 2
You have entered row #3 and column #2
Thank you for your selection
------------------
|R\C| 1 | 2 | 3 |
|1| X | O | |
------------------
|2| X | X | |
------------------
|3| | O | O |
X'turn
Where do what your X placed ?
Please enter row number and column number separated by space
3 1
You have entered row #3 and column #1
Thank you for your selection
X IS THE WINNER !!!!


Related Solutions

USING JAVA Program a playable version of TicTacToe. It should be: •Playable by two players •Show...
USING JAVA Program a playable version of TicTacToe. It should be: •Playable by two players •Show the board before asking each player for input •Instruct the player on how to play at the start of the game •Implement the standard rules of TicTacToe
Program a playable version of TicTacToe. It should be: • Playable by two players
 • Show...
Program a playable version of TicTacToe. It should be: • Playable by two players
 • Show the board before asking each player for input
 • Instruct the player on how to play at the start of the game
 • Be entirely your own work
 • Implement the standard rules of TicTacToe
 You may: • Use any objects in the java standard library (java.whatever)
 • Go as far above and beyond the project requirements as you want
Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3...
Using ECLIPSE IDE Write a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program. Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number. In the main() method, declare an int named myPick3choice. Create a prompt for myPick3choice as "Enter your Pick 3...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
This program is a simple version of the linux shell using execvp by forking using C...
This program is a simple version of the linux shell using execvp by forking using C Currently this program of the shell assumes user enters no spaces before and no extra spaces between strings. Using exec, an executable binary file (eg: a.out) can be converted into a process. An example of using exec is implementing a shell program or a command interpreter. A shell program takes user commands and executes them. int execvp(const char *file, char *const argv[]); Same as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT