-2

First post here, probably wont be my last.

I have a major assignment due and we were supplied with a task and code to go with it. The assignment is basically to recreate pacman. I am stuck with it, as when all the markers are removed it is meant to automatically load up a new "map/level". It is written in java and we are using Greenfoot as the application/compiler if that helps.

public final char[][] LEVEL_1 = {
        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
        {'#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#'},
        {'#','$','#','#','.','#','#','#','.','#','.','#','#','#','.','#','#','$','#'},
        {'#','.','#','#','.','#','#','#','.','#','.','#','#','#','.','#','#','.','#'},
        {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
        {'#','.','#','#','.','#','.','#','#','#','#','#','.','#','.','#','#','.','#'},
        {'#','.','.','.','.','#','.','.','.','#','.','.','.','#','.','.','.','.','#'},
        {'#','#','#','#','.','#','#','#',' ','#',' ','#','#','#','.','#','#','#','#'},
        {' ',' ',' ','#','.','#',' ',' ',' ',' ',' ',' ',' ','#','.','#',' ',' ',' '},
        {'#','#','#','#','.','#',' ','#','#','|','#','#',' ','#','.','#','#','#','#'},
        {' ',' ',' ',' ','.',' ',' ','#','%','?','%','#',' ',' ','.',' ',' ',' ',' '},
        {'#','#','#','#','.','#',' ','#','#','#','#','#',' ','#','.','#','#','#','#'},
        {' ',' ',' ','#','.','#',' ',' ',' ',' ',' ',' ',' ','#','.','#',' ',' ',' '},
        {'#','#','#','#','.','#',' ','#','#','#','#','#',' ','#','.','#','#','#','#'},
        {'#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#'},
        {'#','.','#','#','.','#','#','#','.','#','.','#','#','#','.','#','#','.','#'},
        {'#','$','.','#','.','.','.','.','.','@','.','.','.','.','.','#','.','$','#'},
        {'#','#','.','#','.','#','.','#','#','#','#','#','.','#','.','#','.','#','#'},
        {'#','.','.','.','.','#','.','.','.','#','.','.','.','#','.','.','.','.','#'},
        {'#','.','#','#','#','#','#','#','.','#','.','#','#','#','#','#','#','.','#'},
        {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
    },

        LEVEL_2 = {
        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
        {'#','#','#','#','#','#','.','.','.','#','.','.','.','.','.','.','.','.','#'},
        {'#','$','#','#','.','#','#','#','.','#','.','#','#','#','.','#','#','$','#'},
        {'#','.','#','#','.','#','#','#','.','#','.','#','#','#','.','#','#','.','#'},
        {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#'},
        {'#','.','#','#','.','#','.','#','#','#','#','#','.','#','.','#','#','.','#'},
        {'#','.','.','.','.','#','.','.','.','#','.','.','.','#','.','.','.','.','#'},
        {'#','#','#','#','.','#','#','#',' ','#',' ','#','#','#','.','#','#','#','#'},
        {' ',' ',' ','#','.','#',' ',' ',' ',' ',' ',' ',' ','#','.','#',' ',' ',' '},
        {'#','#','#','#','.','#',' ','#','#','|','#','#',' ','#','.','#','#','#','#'},
        {' ',' ',' ',' ','.',' ',' ','#','%','?','%','#',' ',' ','.',' ',' ',' ',' '},
        {'#','#','#','#','.','#',' ','#','#','#','#','#',' ','#','.','#','#','#','#'},
        {' ',' ',' ','#','.','#',' ',' ',' ',' ',' ',' ',' ','#','.','#',' ',' ',' '},
        {'#','#','#','#','.','#',' ','#','#','#','#','#',' ','#','.','#','#','#','#'},
        {'#','.','.','.','.','.','.','.','.','#','.','.','.','.','.','.','.','.','#'},
        {'#','.','#','#','.','#','#','#','.','#','.','#','#','#','.','#','#','.','#'},
        {'#','$','.','#','.','.','.','.','.','@','.','.','.','.','.','#','.','$','#'},
        {'#','#','.','#','.','#','.','#','#','#','#','#','.','#','.','#','.','#','#'},
        {'#','.','.','.','.','#','.','.','.','#','.','.','.','#','.','.','.','.','#'},
        {'#','.','#','#','#','#','#','#','.','#','.','#','#','#','#','#','#','.','#'},
        {'#','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','#','#','#'},
        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
    };

That is how the levels are designed (LEVEL_2 wasn't changed as much as I just wanted to test and get switching done before putting effort in), as far as I know it is a two dimensional char array.

There is a pre-written method advanceToLevel(char[][]) which requires the input of the name of an array, in this case when I trigger the map change I want LEVEL_2 to be selected, but I am unsure how to have it select LEVEL_2 or anything else that I choose to create.

Anything I try keeps throwing up that it can't convert a string to char[][].

Any help would be appreciated.

  • why have string names for the levels? why not just `1`, `2`, etc... ? integers are dead simple to use as array keys... – Marc B May 27 '15 at 14:25
  • 2
    SO is not a home work machine. – bhspencer May 27 '15 at 14:27
  • That's just what they used, I am not sure how to select them in the method call. I was not using this for homework, it's a proper question. I'd like to know how to be able to select another array in a method call. – Joshua Lundy-McDonald May 27 '15 at 14:31

1 Answers1

0

The error makes it sound like you're writing

advanceToLevel("LEVEL_2")

This isn't a reference to the variable LEVEL_2, but rather a String filled with the content "LEVEL_2".

It seems you might be confused on some of the basics of Java syntax and OOP principles; a good starter would be this refresher on variable scope - https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

I'd take a look at that page and any others in the java tutorial that seem confusing. If you don't get the basics down, it will be very difficult to get to doing interesting things, such as programming Pacman.

Alex Pruss
  • 533
  • 5
  • 15
  • I know that I've been doing it wrong there, but I am unsure on how to make it go from level to level. Is there a way in java that you can scan for the different variables of a type? Then from that have it select one based on a number I give it? – Joshua Lundy-McDonald May 27 '15 at 15:05
  • Technically you can do that programatically with reflection, but using reflection for your problem would be a very bad idea. – Alex Pruss May 27 '15 at 15:16
  • Yeah could be a bit advanced for what I need. Always something to look into. Will keep trying, if you have any other suggestions on how I could have a selector I'd love hear it, got everything but this done. – Joshua Lundy-McDonald May 27 '15 at 15:23
  • If the code you received hasn't already ordered the levels, nothing is stopping you from doing that with your own code. – Alex Pruss May 27 '15 at 16:05