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.
using java write a program As the title described, you should only use two stacks to...
using java write a program As the title described, you should only use two stacks to implement a queue's actions. DO NOT use any other data structure and push, pop and top should be O(1) by AVERAGE. The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue. Both pop and top methods should return the value of first element example push(1) pop() // return 1 push(2) push(3) top() // return 2...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will...
Create a Java program that allows two players to play Rock, Paper, Scissors. Player 1 will enter an integer to determine whether they use rock, paper or scissors. Player 2 will also enter an integer to determine whether they use rock, paper or scissors. Use named constants for rock, paper and scissors and set their values as shown below. Use if-else statements to determine the results of the game. Use the named constants to compare with the player 1 and...
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...
IN JAVA PLEASE Program 4: Is there a Prius version? Did you know that the average...
IN JAVA PLEASE Program 4: Is there a Prius version? Did you know that the average Boeing 747 airplane uses approximately 1 gallon of fuel per second? Given the speed of the airplane, that means it gets 5 gallons to the mile. No, not 5 miles to the gallon, 5 gallons to the mile. You may be questioning why such a horribly inefficient machine is allowed to exist, but you’ll be happy to find out that, because this airplane hold...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT