-5

I am trying to create a text-based game, and I'm struggling to Initialize the game by populating the arrays in the game class. I keep getting this error:

Game4.java:50: error: cannot find symbol
        initGame();
        ^
  symbol:   method initGame()
  location: class Game
1 error

My code is below. If anyone can help with possible ways to initialize without getting that error it would be greatly appreciated.

class Game {
 
    // Declare and initialize global variables
    private static final int num_locations = 5;
    private static final int num_characters = 5;
    private static final int num_items = 12;
 
    private Location[] locations;
    private Character[] characters;
    private Item[] items;
 
    private int currentLocation;
 
    // Default constructor
    public Game() {
         
        // Create the array of locations
        locations = new Location[num_locations];//NUM_LOCATIONS];

        // Create the array of characters
        characters = new Character[num_characters];//NUM_CHARACTERS];
         
        // Create the array of items
        items = new Item[num_items];//NUM_ITEMS];
         
        // Initialize the game by populating the arrays
        // with objects
        initGame(); // find a new way to initialize the game by populating the game
  
        // Set the current location to the starting location
        currentLocation = 0;
         
    }
  • 6
    What is `init.Game()` supposed to be or do here? It's not clear to me what you're trying to do, or why you expect that line to do anything. – David Oct 18 '22 at 17:29
  • 2
    I'm curious, why are you expecting `init.Game()` to work? – MC Emperor Oct 18 '22 at 17:30
  • I made a slight error when writing down my code. Instead of init.Game(), it should be initGame(); – JAVAnew123 Oct 18 '22 at 18:51
  • Does that clarify anything? – JAVAnew123 Oct 18 '22 at 18:51
  • @JAVAnew123: (1) Copying/pasting code avoids typos. We can't do much with text that kind of looks like the code that causes the error, we'd need actual code. (2) With the updated code, it's the same issue as before. What is `initGame`? What are you expecting it to be and why? The compiler is telling you that there is no such method. And, if you look at the code, you will see that there is no such method. It's not clear what exactly you're asking. – David Oct 18 '22 at 19:24
  • Is there a way to initialize a class called game and populate the arrays with objects? Let me take a look at my code and see if there is anything I can resolve – JAVAnew123 Oct 18 '22 at 19:27
  • @JAVAnew123: The code shown is in the constructor for `Game`. So when other code create a new instance of `Game` then this code will execute to "initialize" that "game". Currently the code already initializes the arrays themselves. If you want to populate the arrays with new objects, you could loop over each one and initialize each element of the array to a new object of its type. – David Oct 18 '22 at 20:10

1 Answers1

0

When you're doing init.Game() you're trying to call a function "Game()" on an object "init". You don't have a variable called init. If you already have a function written called init, you might be trying to do Game.init(). This "init" function you are trying to do is not something that java does for you automatically, you have to figure out how to make it. You should ask for some clarification from your teacher.