I'm trying to use a while loop to loop a method until a certain requirement is filled. That requirement is until all the spaces in the array table have been filled.
I was getting a run time error before however @Eran helped me fix it, now I'm having trouble initializing the array in the constructor. I'm getting compiler errors but I know that's because I've initialized the array wrong.
import java.util.Scanner;
public class GradeSO{
Scanner input = new Scanner(System. in);
int remainder;
int down;
int column;
int row;
int rows = 5;
int columns = 5;
int playerOne;
int playerTwo;
int[][] grid;
public void makeLocation(int x, int r, int c)
{
remainder = x % c;
down = (int) x / r;
if(x % c!=0)
{
column = remainder-1;
}
else
{
column = remainder;
}
if(x % c==0)
{
row=down-1;
column = remainder+4;
}
else
{
row=down;
}
}
public void makeArray()
{
grid = new int[rows][columns];
System.out.println("Player one choose your position");
playerOne = input.nextInt();
while (playerOne > 25 || playerOne < 1) {
System.out.println("The inputted value is outside the range of the grid");
while(!input.hasNextInt())
{
input.next() ;
}
playerOne = input.nextInt();
}
makeLocation(playerOne,columns,rows);
grid[row][column]= 1;
System.out.println("Player two choose your position");
playerTwo = input.nextInt();
while (playerTwo > 25 || playerTwo < 1) {
System.out.println("The inputted value is outside the range of the grid");
while(!input.hasNextInt())
{
input.next() ;
}
playerTwo = input.nextInt();
}
makeLocation(playerTwo,columns,rows);
grid[row][column]= 2;
}
public void displayArray()
{
for (int[] bigGrid: grid)
{
for (int elem: bigGrid)
{
System.out.print(" ");
System.out.print(elem);
}
System.out.println( "" );
}
}
public static void main(String[] args)
{
GradeSO gSO = new GradeSO();
grid = new int[][]{};
do {
gSO.makeArray();
gSO.displayArray();
} while(!gSO.isGameOver());
}
public boolean isGameOver(){
for (int i=0; i < rows; i++){
for (int j=0; j < columns; j++){
if(grid[i][j] == 0) return false;
}
}
return true;
}
}