Change Java code below

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

************************************************************

  • Change Java code below so that it outputs a 9x9 board and not a 3x3 board.

// program to demonstrate
// Tic-Tac-Toe Game.
import java.util.*;

public class Tic_Tac_Toe{

static String[] board;
static String turn;
// CheckWinner method will decide the combination
//of three box given below. if all three is with same X then X will be winner
//and if all three O's is same O's will be winner otherwise draw .
static String checkWinner()
{
for (int a = 0; a < 8; a++) {
String line = null;

switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}

// For O winner
else if (line.equals("OOO")) {
return "O";
}
}

for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(
String.valueOf(a + 1))) {
break;
}
else if (a == 8) {
return "draw";
}
}

// To enter the X Or O at the exact place on board.
System.out.println(
turn + "'s turn; enter a slot number to place "
+ turn + " in:");
return null;
}

// To print out the board.
/* |---|---|---|
| 1 | 2 | 3 |
|-----------|
| 4 | 5 | 6 |
|-----------|
| 7 | 8 | 9 |
|---|---|---|*/
//printing board of 3X3
static void printBoard()
{
System.out.println("|---|---|---|");
System.out.println("| " + board[0] + " | "
+ board[1] + " | " + board[2]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[3] + " | "
+ board[4] + " | " + board[5]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[6] + " | "
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}
//main drive
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
board = new String[9];
turn = "X";
String winner = null;

for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a+1);
}

System.out.println("Welcome to 3x3 Tic Tac Toe game");
//print the 3x3 tic-tac-toe board
printBoard();

System.out.println(
"X will play first. Enter a slot number to place X in:");

while (winner == null) {
int numInput;

// Exception handling.
// numInput will take input from user like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error "Invalid input."
try {
numInput = in.nextInt();
if (!(numInput >0 && numInput <=9)) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
}
catch (InputMismatchException e) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}

// This game has two player x and O.
// Here is the logic to decide the turn.which one play next
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;

if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
}

printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
}
}

// If no one win or lose from both player x and O.
// then here is the logic to print "draw".
if (winner.equalsIgnoreCase("draw")) {
System.out.println(
"It's a draw! Thanks for playing.");
}

// For winner -to display which one is winner message.
else {
System.out.println(
"Congrats! " + winner
+ "'s have won! the game Thanks for playing.");
}
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education