Question: The code in java is a tik tac toe. Without changing the code already written, please add a portion to the code where if all

The code in java is a tik tac toe. Without changing the code already written, please add a portion to the code where if all the tik tac toe spots are filled in and no one has won, end the game with the prompt, "the game is over, tie". It all has to be inside the main method and can not use another method.The code provided is listed below.

package tttgame;

import java.util.Scanner;

public class TTTBoard { public static void main(String[] args) { Scanner input = new Scanner(System.in); char[] [] board = new char [3] [3]; for(int row = 0; row < board.length; row++) { for(int col = 0; col < board[row].length; col++) { board[row][col] = '-'; } } char player = 'X'; int rowNum = -1; int colNum = -1; do { System.out.println("Printing board info..."); for(int row = 0; row < board.length; row++) { for(int col = 0; col < board[row].length; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } //TODO: is the game over, if so break System.out.print("Player " + player + ", Please select a row number (1-3): "); rowNum = input.nextInt(); rowNum = rowNum -1; System.out.print("Player " + player + ", Please select a row number (1-3): "); colNum = input.nextInt(); colNum = colNum -1; if(board[rowNum][colNum] == '-' ) { board[rowNum][colNum] = player; //made the move //TODO: check to see if player has won //Check each row for(int i = 0; i < 3; i++) { if(board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-') { System.out.println(player + " has won the game!"); for(int row = 0; row < board.length; row++) { for(int col = 0; col < board[row].length; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } break; } }

//Check each column for(int j = 0; j < 3; j++) { if(board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != '-') { System.out.println(player + " has won the game!"); for(int row = 0; row < board.length; row++) { for(int col = 0; col < board[row].length; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } break; } }

//Check the diagonals if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') { System.out.println(player + " has won the game!"); for(int row = 0; row < board.length; row++) { for(int col = 0; col < board[row].length; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } break; } if(board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != '-') { System.out.println(player + " has won the game!"); for(int row = 0; row < board.length; row++) { for(int col = 0; col < board[row].length; col++) { System.out.print(board[row][col] + " "); } System.out.println(); } break; }

if(player == 'X') player = 'O'; else player = 'X'; }else { System.out.println("Incorrect move"); System.out.println("Please attempt again"); } } while(true); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!