Question: I have a Java program that it is meant to simulate a tic tac toe game. The program works fine, X goes first and you
I have a Java program that it is meant to simulate a tic tac toe game. The program works fine, X goes first and you type in the row and column , then O goes second, but I need it to read the input from a text file with input redirection. The text file is just a bunch of numbers, one number per line like this:
2
3
2
1
1
1
Right now the program is only reading the first two numbers and then throws an exception. I know it has something to do with the nextInt() not being consumed at the end of the line, can somebody point me in the right direction?
Also I would like to check if there's a way to loop throughout the winning conditions more effectively so that the isWinner() method is not so lenghty. Thank you in advance, here is the code:
import java.util.*;
public class TicTacToe { public static void main(String args[]) { Scanner input = new Scanner(System.in); //Initialize board char[][] grid = { {'-', '-', '-'}, {'-', '-', '-' }, {'-', '-', '-'} }; int counter = 0; //Continues the game until there is a winner while (isWinner(grid) == false) { printGrid(grid); setGridValue(grid,'x',0,0, counter); counter += 1; } printGrid(grid); } //Board formatting public static void printGrid(char[][] grid) { System.out.println(" " + grid[0][0] + " " + "|" + " " + grid[0][1] + " " + "|" + " " + grid[0][2]); System.out.println(" " + "--+--+---"); System.out.println(" " + grid[1][0] + " " + "|" + " " + grid[1][1] + " " + "|" + " " + grid[1][2]); System.out.println(" " + "--+--+---"); System.out.println(" " + grid[2][0] + " " + "|" + " " + grid[2][1] + " " + "|" + " " + grid[2][2]); } //Input validation public static void setGridValue(char[][] grid, char value, int row, int col, int counter) { Scanner input = new Scanner(System.in); if (counter % 2 == 0) { value = 'X'; } else { value = 'O'; } row = input.nextInt(); if ((row >= 1) && (row <=3)) { } else { System.out.println("Bad Data"); System.exit(0); } col = input.nextInt(); if ((col >= 1) && (col <=3)) { } else { System.out.println("Bad Data"); System.exit(0); } if (grid[row-1][col-1] != '-') { System.out.println("Position Taken. Try Again."); setGridValue(grid,'x',0,0, counter); } else { grid[row-1][col-1] = value; } } //Check for winner public static boolean isWinner(char[][] grid) { if ((grid[0][0] == 'X') && (grid[0][1] == 'X') && (grid[0][2] == 'X') || (grid[1][0] == 'X') && (grid[1][1] == 'X') && (grid[1][2] == 'X') || (grid[2][0] == 'X') && (grid[2][1] == 'X') && (grid[2][2] == 'X') || (grid[0][0] == 'X') && (grid[1][0] == 'X') && (grid[2][0] == 'X') || (grid[1][0] == 'X') && (grid[1][1] == 'X') && (grid[1][2] == 'X') || (grid[2][0] == 'X') && (grid[2][1] == 'X') && (grid[2][2] == 'X')) { System.out.println("X is the winner!"); return true; } else if ((grid[0][0] == 'O') && (grid[0][1] == 'O') && (grid[0][2] == 'O') || (grid[1][0] == 'O') && (grid[1][1] == 'O') && (grid[1][2] == 'O') || (grid[2][0] == 'O') && (grid[2][1] == 'O') && (grid[2][2] == 'O') || (grid[0][0] == 'O') && (grid[1][0] == 'O') && (grid[2][0] == 'O') || (grid[1][0] == 'O') && (grid[1][1] == 'O') && (grid[1][2] == 'O') || (grid[2][0] == 'O') && (grid[2][1] == 'O') && (grid[2][2] == 'O')) { System.out.println("O is the winner!"); return true; } else if ((grid[0][0] == 'X') && (grid[1][1] == 'X') && (grid[2][2] == 'X') || (grid[0][2] == 'X') && (grid[1][1] == 'X') && (grid[0][2] == 'X')) { System.out.println("X is the winner!"); return true; } else if ((grid[0][0] == 'O') && (grid[1][1] == 'O') && (grid[2][2] == 'O') || (grid[0][2] == 'O') && (grid[1][1] == 'O') && (grid[0][2] == 'O')) { System.out.println("O is the winner!"); return true; } else { return false; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
