0
\$\begingroup\$

I can move within my 2d array. But I'm not sure if my right or left and up and down is messed up, or what but for some reason the movements don't correspond to anything I'm familiar with on the map(like if I move into a wall, and type the opposite direction, there is a wall on both sides of me, you may have to just try this little game out yourself and see).Also sometimes, when it should output that you are on an empty tile, or something, it just goes straight to my if else instead of saying one of my if statements(Which I'm sure is my blunder). Is it just my directions within the 2D array that are messed up or something more? Thank you again for your time I am just self learning java and having a great time. TLDR: I just want my movements (up, down,left,and right) to correspond to my 2d array map.

    package login;

import java.util.Scanner;


public class Form {
static String dir = "";
static int player;
static int chest;
static int monster;
static int key;
static int tile;
static int wall;
static Scanner scan = new Scanner(System.in);

static int[][] map = new int[][]{
    {1,1,1,1,1,1,1,1,1,1},
    {1,2,2,2,3,4,2,2,4,1},
    {1,3,2,2,2,2,2,2,2,1},
    {1,2,5,4,2,2,2,5,2,1},
    {1,2,3,3,0,5,5,3,2,1},
    {1,2,2,2,2,2,2,2,2,1},
    {1,3,3,5,2,2,3,2,2,1},
    {1,4,4,2,2,2,2,2,2,1},
    {1,3,5,2,2,2,3,4,5,1},
    {1,1,1,1,1,1,1,1,1,1}};

    static int x= map[5][0];
    static int y = map[0][5];
    static int playerPos = map[x][y];
    static int life = 3;

static boolean isInGame;

public static void main(String[] args){
int isPlaying;
    System.out.println("Press 1 to play..anything else to exit!");
    isPlaying = scan.nextInt();
    if(isPlaying == 1){
    isInGame = true;
    enterGame();

    }
    else{
        System.out.println("You missed out on a good game...");
        System.exit(1);
    }
}

public static void enterGame(){
    System.out.println("Type, up, down, left, or right, to move.");
    while(isInGame == true){
        move();


    }

}



public static void move(){
    dir = scan.nextLine();
    switch (dir){
    case "up":
        x = x+1;
        playerPos = map[x][y];
        map();
        break;
    case "down":
        x = x-1;

        playerPos = map[x][y];
        map();
        break;
    case "left":
        y = y-1;

        playerPos = map[x][y];
        map();
        break;
    case "right":
        y = y+1;

        map();
        break;

    }
}

public static void map(){
    wall = 1;
    tile = 2;
    monster = 3;
    chest = 4;
    key = 5;
    player = 0;



    if(playerPos == wall){
        System.out.println("Thats a wall. Can't move there.");
    }
    if(playerPos == tile){
        System.out.println("You move to a free tile");
    }
    if(playerPos == monster){
        monsterEncounter();
    }
    if(playerPos == chest){
        System.out.println("Woah a chest!");
    }
    if(playerPos == key){
        System.out.println("Woah a key!");
    }
    else if(playerPos != wall){
        System.out.println("Keep going.");
    }
}
public static void monsterEncounter(){
    if(life > 0){
    if( x == 3 || y == 3){
        System.out.println("WOAH! A MONSTER! HE BEATS YOUR ASS.. -1 LIFES");
        life =- 1;
        System.out.println("You have: "+ life +" life left! Don't die..");
    }}else{
        System.out.println("You have: "+ life +"You encountered too many monsters. GAME OVER.");
        System.exit(1);
    }

}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Do you know what a step-by-step debugger or a breakpoint is? \$\endgroup\$ – Vaillancourt Mar 15 '17 at 16:14
  • \$\begingroup\$ @AlexandreVaillancourt No i do not, but I will look them up. \$\endgroup\$ – Mike noah Mar 15 '17 at 16:18
  • 1
    \$\begingroup\$ Every decent IDE comes with one, I suggest you take a look at it first, this will help you a lot to understand what's going on in your program. It helps you step through the code, one line of code at a time, allowing you to see what code path the program takes, and showing you the current value of all of the variables. System.out.println is a great tool to debug, but you have to learn how to use a debugger too :) \$\endgroup\$ – Vaillancourt Mar 15 '17 at 16:22
1
\$\begingroup\$

Here it is working, I just messed with it until I got it. Hope this code helps someone. I also figured out how to change chests that had been opened, and keys that had been collected, into generic tile pieces. Cheers.

   package login;

import java.util.Scanner;


public class Form {
static String dir = "";
static int player;
static int chest;
static int monster;
static int key;
static int tile;
static int wall;
static int keysFound = 0;
static int chestsOpened = 0;
static int chestsLeft = 3;
static Scanner scan = new Scanner(System.in);

static int[][] map = new int[][]{
    {1,1,1,1,1,1,1,1,1,1},
    {1,2,2,2,2,4,2,2,5,1},
    {1,3,2,2,2,2,2,2,2,1},
    {1,2,5,4,2,2,2,5,2,1},
    {1,2,3,3,0,5,5,3,2,1},
    {1,2,2,2,2,2,2,2,2,1},
    {1,3,2,5,2,2,3,2,2,1},
    {1,4,4,2,2,2,2,2,2,1},
    {1,2,5,2,2,2,3,4,5,1},
    {1,1,1,1,1,1,1,1,1,1}};

    static int x= map[5][0];
    static int y = map[0][5];
    static int playerPos = map[x][y];
    static int life = 3;

static boolean isInGame;

public static void main(String[] args){
int isPlaying;
    System.out.println("Welcome to Michael's Mystical Maze.. You will need:\nOne peice of paper\nA pen\nYour wits\n");
    System.out.println("You must map the maze on paper, finding keys, avoiding monsters\nand open three chests to win!");
    System.out.println("Press 1 to play..anything else to exit!");
    isPlaying = scan.nextInt();
    if(isPlaying == 1){
    isInGame = true;
    enterGame();

    }
    else{
        System.out.println("You missed out on a good game...");
        System.exit(1);
    }
}

public static void enterGame(){
    System.out.println("Type, up, down, left, or right, to move.");
    while(isInGame == true){
        move();


    }

}



public static void move(){
    dir = scan.nextLine();
    switch (dir){
    case "up":
        x = x+1;
        playerPos = map[x][y];
        map();
        break;
    case "down":
        x = x-1;
        playerPos = map[x][y];
        map();
        break;
    case "left":
        y = y-1;
        playerPos = map[x][y];
        map();
        break;
    case "right":
        y = y+1;
        playerPos = map[x][y];
        map();
        break;

    }
}

public static void map(){
    wall = 1;
    tile = 2;
    monster = 3;
    chest = 4;
    key = 5;
    player = 0;


    System.out.println(playerPos);
    if(playerPos == wall){
        System.out.println("Thats a wall. Can't move there.");
    }
    if(playerPos == tile){
        System.out.println("You move to a free tile");
    }
    if(playerPos == monster){
        monsterEncounter();
    }
    if(playerPos == chest){
        openChest();
    }
    if(playerPos == key){
        findKey();
    }

}
public static void monsterEncounter(){
    if(life > 0){

        System.out.println("WOAH! A MONSTER! HE BEATS YOUR ASS.. -1 LIFES");
        life = life - 1;
        System.out.println("You have: "+ life +" life left! Don't die..");
    }else{
        System.out.println("You have:NO LIFE , You encountered too many monsters. GAME OVER.");
        System.exit(1);
    }

}
public static void findKey(){
    System.out.println("Woah a key!");
    keysFound += 1;
    map[x][y] = 2;
}
public static void openChest(){
    System.out.println("Woah a chest!");
    if(keysFound >= 1){
        chestsOpened += 1;
        int totalChestsFound = chestsLeft - chestsOpened;
        chestsLeft -= 1;
        System.out.println("You have opened a chest! You have "+ totalChestsFound + " chests to go!");
        map[x][y] = 2;
        keysFound -= 1;
    }
    if(chestsLeft == 0){
        System.out.println("YOU WON! GOOD GAME SIR, GOOD GAME.");
        System.exit(1);
    }
    else{
        System.out.println("No keys, no chests. Keep searching soldier.");
    }

}
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.