Domanda

I am creating a Java Dungeon game and so far it imports a ASCII map into a 2d array. A player is then added and can roam around the dungeon using CLI. The player has to pick up all the gold before he can exit and win the game.

The problem I have is you can currently see the whole dungeon. For example..

###################
#.................#
#......G........E.#
#...........G.....#
#..E..............#
#..........G......#
#.............P...#
#.................#
###################

I would like it so you can use a LOOK command and see a 2 tile radios around the player. For example..

X...X
.....
..P..
.....
X###X

So far I have come up with this.

public void look(){
    try{
        System.out.print("X");
        System.out.print(myWorld[posX-2][posY-1]);
        System.out.print(myWorld[posX-2][posY]);
        System.out.print(myWorld[posX-2][posY+1]);
        System.out.print("X");
        System.out.print("\n");
        System.out.print(myWorld[posX-1][posY-2]);
        System.out.print(myWorld[posX-1][posY-1]);
        System.out.print(myWorld[posX-1][posY]);
        System.out.print(myWorld[posX-1][posY+1]);
        System.out.print(myWorld[posX-1][posY+2]);
        System.out.print("\n");
        System.out.print(myWorld[posX][posY-2]);
        System.out.print(myWorld[posX][posY-1]);
        System.out.print(myWorld[posX][posY]);
        System.out.print(myWorld[posX][posY+1]);
        System.out.print(myWorld[posX][posY+2]);
        System.out.print("\n");
        System.out.print(myWorld[posX+1][posY-2]);
        System.out.print(myWorld[posX+1][posY-1]);
        System.out.print(myWorld[posX+1][posY]);
        System.out.print(myWorld[posX+1][posY+1]);
        System.out.print(myWorld[posX+1][posY+2]);
        System.out.print("\n");
        System.out.print("X");
        System.out.print(myWorld[posX+2][posY-1]);
        System.out.print(myWorld[posX+2][posY]);
        System.out.print(myWorld[posX+2][posY+1]);
        System.out.println("X");
    }catch(IndexOutOfBoundsException e){
        System.out.println("AGAINST WALL....");
    }
}

I am sure there is a better way to do this but I can't figure it out. This method also only works unless the player is against a wall. Is there a better way to do this and how would I be able to create a buffer around my map of '?' for the tiles that are not in the map.

È stato utile?

Soluzione

as others suggested you should use 2 for loops. I've added the required logic get the result you're looking for

public void look()
    {
        StringBuffer sb = new StringBuffer(25);

        for(int y= posY-2; y <= posY+2; y++){
            for(int x = posX-2; x <= posX+2; x++){
                try
                {
                    if(x<0 || y<0 || x > myWorld.length || y > myWorld[x].length)
                        sb.append("#");//Wall or outside map
                    else
                        if(Math.abs(posX-x)+Math.abs(posY-y) < 4)
                            sb.append(myWorld[x][y]); //Valid part of map
                        else
                            sb.append("X"); //Unreachable corner
                }
                catch(IndexOutOfBoundsException e){} //This shouldn't happen :)
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }

Using StringBuffer will keep the execution time down with bigger amounts of data

Altri suggerimenti

I would suggest to add 2 variables, which would contain size of array (for example sizeX and sizeY) and use two cycles:

for (i = posX-2; i<posX+2; i++){
  for (j = posY-2; j<posY+2; j++){
    if (i>=0 && i<sizeX && j>=0 && j<sizeY){
     System.out.print(myWorld[i][j]);
    }
    System.out.print("\n");
}

I hope this helps.

void getSurrounding(myWorld, posX, poxY, width, height, radius)
{
  for(i=-(radius-1);i==(radius-1);i++)
  {
    if(i==0 || i==radius)
    {
      System.out.print('X');
    }
    if(posX+i>width)
    {
       break;
    }
    for(j=-(radius-1);j==(radius-1);j++)
    {
        if(posY+j>height)
        {
           break;
        }
        System.out.print(myWorld[posX+i][posY+j]);
    }
    System.out.println('\n');
}

}

//Call in your look function
void look()
{
    getSurroundings(myWorld,posX,posY,width,height,2);
}`
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top