-2

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;
}

}
josliber
  • 43,891
  • 12
  • 98
  • 133
  • Your edit is now asking an entirely different question. Please do not edit a question to make another question. Ask the new question on it own as a new question. If you think it relevant, add a link to the earlier question. – AdrianHHH Dec 16 '15 at 13:04

1 Answers1

1

The array is created in makeArray, but you call isGameOver first.

One way you can solve this error is to change the while loop to do-while :

    GradeSO gSO = new GradeSO();
    do {
        gSO.makeArray(); 
        gSO.displayArray();
    } while(!gSO.isGameOver());

EDIT : Your isGameOver method would return true if any element of the grid contains 0. After the first call to makeArray, only two elements of the array may be different from 0 (assuming you got that code right), which means the loop will end. I don't know what the isGameOver logic should be, but the current implementation is clearly wrong.

P.S. It might be a good idea to move the array initialization outside of makeArray, since you don't want to reset the array in each iteration of the game.

EDIT :

You are still initializing the array inside makeArray.

Remove grid = new int[rows][columns]; from makeArray and create a constructor :

public GradeSO ()
{
    grid = new int[rows][columns];
}

remove grid = new int[][]{}; from your main.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • While this did fix the error, it still doesn't loop. It will only allow two inputs and then ends the program. –  Dec 16 '15 at 09:11
  • @SearchingFor see edit – Eran Dec 16 '15 at 09:17
  • Okay thanks @Eran should I put the array initialization inside of main? Also how would I go about having at least the program loop correctly even if the logic isn't totally correct. –  Dec 16 '15 at 10:22
  • @SearchingFor You must fix `isGameOver`, or the loop would terminate after one iteration. The array initialization can be done in the constructor of `GradeSO`. – Eran Dec 16 '15 at 10:29
  • I've fixed the `isGamOver` method but now it's in a infinite loop and it won't save the inputted values in the table. –  Dec 16 '15 at 10:41
  • @SearchingFor did you move the array initialization. Based on your description is sounds like you didn't. – Eran Dec 16 '15 at 10:44
  • sorry again but I'm having issues with initializing the array in the constructor. I keep on getting compiler errors but I think it's because I'm doing it wrong. –  Dec 16 '15 at 12:01
  • @SearchingFor You should edit your question and add the new code that doesn't work. – Eran Dec 16 '15 at 12:05
  • Okay I've updated my code and edited the question. –  Dec 16 '15 at 12:52
  • @SearchingFor see edit. – Eran Dec 16 '15 at 13:40
  • Thanks so much for all the help @Eran you don't know how much of a life saver you are. :) –  Dec 16 '15 at 18:27