In: Computer Science
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 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 !!!!