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 "...
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,...
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.
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and...
In C++ Create two functions called TheNumber. One version of TheNumber should accept a string and output the accepted string 10 times. The other version of TheNumber should accept a double and output the accepted double 10 times. This uses function overloading.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT