0

I am new in Java and I am trying to write a simple case of the Whack-A-Mole game. But, the problem is I am getting trouble when tring to call one of my method in the main method.

Here is the code :

import java.util.Scanner;
import java.util.Random;

public class WhackAMole {
    //specifying instance variables
    int score;
    int molesLeft;
    int attemptsLeft;
    int Dimension;
    char[][] moleGrid = new char[Dimension][Dimension];

    WhackAMole(int numAttempts, int gridDimension) {
    this.molesLeft = 0;
    this.Dimension = gridDimension;
    this.attemptsLeft = numAttempts;
    for (int i = 0; i < moleGrid.length; i++) {
        for (int j = 0; j < moleGrid[i].length; j++) {
        this.moleGrid[i][j] = '*';
        }
    }
    }

    boolean place(int x, int y) {
    if (moleGrid[x][y] == '*') {
        moleGrid[x][y] = 'M';
        molesLeft = molesLeft + 1;
        return true;

    }
    else {
        return false;
    }
    }

    void whack(int x, int y) {
    if (moleGrid[x][y] == 'M') {
        score = score + 100;
        attemptsLeft = attemptsLeft - 1;
        molesLeft = molesLeft-1;
        moleGrid[x][y] = 'W';
    }
    else {
        attemptsLeft = attemptsLeft - 1;
    }
    }

    void printGridToUser() {
    for (int i = 0; i < Dimension ; i++) {
        for (int j = 0; j < Dimension; j++) {
        if (moleGrid[i][j] == 'W') {
            System.out.print("W" + " ");
        }
        else {
            System.out.print("*" + " ");
        }
        }
    }
    }

    void printGrid() {
    for (int i = 0; i < Dimension ; i++) {
        for (int j = 0; j < Dimension; j++) {

            System.out.print(moleGrid[i][j] + " ");
        }
    }
    }

    public static void main(String[] args) {

    WhackAMole NewGame = new WhackAMole (50, 10);
    Random rand = new Random();

    int i = 0;
    while (i < 10){
    int x = 0 + rand.nextInt((9 - 0) + 1);
    int y = 0 + rand.nextInt((9 - 0) + 1);
    NewGame.place(x, y);
    if (NewGame.place(x, y) == true) {
       i = i + 1;
    }
    else {
       i = i;
    }
    }

    Scanner input = new Scanner(System.in);
    System.out.println("You have a maximum of 50 attempts to get all the moles");
    for (int j = 0; j <= NewGame.attemptsLeft; j++) {
        System.out.println("Enter the first coordinates between 1 and 10");
        int x = input.nextInt() - 1;
        System.out.println("Enter the second coordinates between 1 and 10");
        int y = input.nextInt() - 1;
        if (x == -2) {
        if (y == -2) {
            j = NewGame.attemptsLeft + 1;
            NewGame.printGrid();
            System.out.println("The game is over");
        }
        }
        else {
        NewGame.whack(x,y);
        if (NewGame.molesLeft == 0) {
            j = NewGame.attemptsLeft +1;
        }
        }

    }
    }

}

And here is the problem

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at WhackAMole.place(WhackAMole.java:25)
    at WhackAMole.main(WhackAMole.java:79)

Why is that happening?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • @OP that is actually good thing :) [Should 'Hi', 'thanks', taglines, and salutations be removed from posts?](https://meta.stackexchange.com/q/2950) – Pshemo Sep 28 '17 at 18:13
  • Class fields have default values. If you write `int Dimension;` then it is automatically initialized with 0, so it is like you would write `int Dimension = 0;`. Then at `char[][] moleGrid = new char[Dimension][Dimension];` it is same as `char[][] moleGrid = new char[0][0];`. Do you see the problem here? – Pshemo Sep 28 '17 at 18:16
  • Dear @Pshemo, Thanks a lot for your comment. I thought the problem you pointed out is resolved with the constructor. Indeed, I initialized the **Dimension** in the constructor by closing it to the **gridDimension** which is a variable of the constructor. What do you think about that ? – Mohamed Coulibaly Sep 28 '17 at 18:26
  • Initialization of class fields is done before any constructors code. So even if you change dimension via `this.Dimension = gridDimension;` in constructor, execution of `char[][] moleGrid = new char[Dimension][Dimension];` will still happen before that change, so for `Dimension = 0`. You should move initialization of that array also to constructor if you want to initialize it to size passed by its parameter. – Pshemo Sep 28 '17 at 18:30
  • Dear @Pshemo, after having tried your suggestion, I have been able to solve the issue. Thanks a lot for your help. – Mohamed Coulibaly Sep 28 '17 at 21:58

0 Answers0