LeonH
LeonH

Reputation: 1037

Method not called, debug message not displayed

Basically, everything works in seclusion, but for some reason at the end of my generateBoard method, it doesn't actually call the method (printBoard) to print it. I believe that this should work but not even my debug message is popping up so I imagine I have an indentation error somewhere?

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

public class Zombies {
    private int Level = 1;
    private int MoveNo = 0;
    public static char[][] myGrid = new char[12][12];

        public static void levelInfection() {
            Scanner input = new Scanner (System.in);
            System.out.println("How many infected tiles would you like to start the game with?");
            if(input.hasNextInt()) {
                int nLevel = input.nextInt();
                if(nLevel > 6) {
                    System.out.println("You can't have that many infected tiles, you'd never survive!");
                    levelInfection();
                }
                else if (nLevel < 0){
                    System.out.println("You need to have some infected tiles, and don't try any negative numbers!");
                    levelInfection();
                }
                else if (nLevel < 6 && nLevel > 0) {
                    System.out.println("We will now create your gameboard with " + nLevel + " infected tiles, good luck!");
                    generateBoard(nLevel);
                }
            }
            else {
                System.out.println("I'm sorry, you didn't enter an integer number. Please try again.");
                levelInfection();
            }
        }

    public static void generateBoard(int nLevel) {
        Random rand = new Random();
        int i, j;
        int infTile = 0;

        for(i = 0; i < 12; i++) {
            for(j = 0; j < 12; j++) {
                if (i == 6 && j == 6) {
                    myGrid[i][j] = 'P';
                    System.out.println("I made a player tile");
                }
                else if(rand.nextInt(9) == 0) {
                    if(myGrid[i][j] == 'I'||infTile >= nLevel) {
                        System.out.println("I found infected tile");
                        return;
                    }
                    else {
                        myGrid[i][j] = 'I';
                        System.out.println("I made an infected tile");
                        infTile++;
                        System.out.println("I counted an infected tile (" + infTile +") at " + i + "," + j);
                    }
                }
                else {
                    myGrid[i][j] = 'x';
                }
            }
        }
        System.out.println("Print me mofo!");
        printBoard();
    }

    public static void printBoard() {
        int i, j;
        for(i = 0; i < 12; i++) {
            for(j = 0; j < 12; j++) {
                if(j == 0) {
                    System.out.print( "| " );
                }
                System.out.print( myGrid[i][j] + " " );
                if(j == 11) {
                    System.out.print( "|\n" );
                }
            }
        }
    }
}

Upvotes: 0

Views: 241

Answers (2)

Sameer Sawla
Sameer Sawla

Reputation: 729

When you are finding the infected tile, you have put up a return statement in the execution order. This returns the control to the caller function terminating the execution of generateBoard().

If you need the value of you array at that instant when you encounter the infected tile, you should go for a break; statement. This would break out of the current for loop but the rest of the function will still execute.

Hope this helps.

Upvotes: 1

Ross Drew
Ross Drew

Reputation: 8246

Perhaps it is hitting your return statement

  if(myGrid[i][j] == 'I'||infTile >= nLevel) {
    System.out.println("I found infected tile");
    return;
  }

...which would exit the method without ever reaching the call.

Upvotes: 3

Related Questions