Question

In: Computer Science

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

Solutions

Expert Solution

//create a Java class with name TicTacToe.java nad paste the code given below

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();
}
  
  
}
}

//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 1
You have entered row #2 and column #1
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
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 | O | |
------------------
|3| | | |
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 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.
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
MATLAB Program a Matlab version of Guess the Word. The word should be determined by user...
MATLAB Program a Matlab version of Guess the Word. The word should be determined by user input. Once the word is determined, the game should begin by displaying blanks (-) where all the letters of the word are. The game should then continue to prompt the player to guess a letter in the word. If the player guesses a letter that is in the word and not already displayed, that letter should be displayed everywhere it appears in the word....
Suppose two players take turns playing another version of the parlor game discussed in class on...
Suppose two players take turns playing another version of the parlor game discussed in class on Tuesday. In this version, players can say any whole number between 1 and 10. The first person to get the running total to 25 wins. Do you want to go first or second? Figure out the optimal strategy for this game. Show your work. (b) (5 points) ECN-322 only for this part: Suppose two players take turns playing another version of the parlor game...
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
Design and implement a Python program which will allow two players to play the game of...
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid! X | O | X | O -------------- O | O | X | O -------------- X | X | O | X -------------- X | X | O | X The rules for this game is the same as the classic, 3x3, game – Each cell can hold one of the following three strings: "X", "O", or "...
List 3 reasons management should show more interest in the B players. In your own words...
List 3 reasons management should show more interest in the B players. In your own words explain the thought process behind those reasons. Please write detailed answer and cite your sources in APA format. at least 300 words.
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
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 to enter the row...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
C program with functions Make a dice game with the following characteristics: must be two players...
C program with functions Make a dice game with the following characteristics: must be two players with two dice. when player 1 rolls the marker is annotated when player 2 rolls the marker is annotated in other words, for each roll the added scores must be shown. Example: Round 1: player 1 score = 6 player 2 score = 8 Round 2: player 1 score = 14 player 2 score = 20 Whoever reaches 35 points first wins. if a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT