Home > database >  Is there an easier/generic way of implementing this switch statement?
Is there an easier/generic way of implementing this switch statement?

Time:11-22

I am making a small game in which a Player object is able to move around a 2x2 matrix of other objects (DreamObjects, in this context). The Player is able to move onto Space objects. I will later implement other objects; but all extend the parent DreamObject. Now, to implement movement of my Player, I have to always check if the object infront of it is an instance of a certain object. In the code shown below, for each direction the player intends to move in, I check if the object there is an instance of Space; if it is, I swap their locations and update the world. In the future I will add Monsters, stationary objects and etc. The problem I am encountering is the fact that I will repeat a lot of code with the only difference being how I apply arithmetic on the row and columns depending on direction of movement. I was thinking of applying a Generic class, but I am not seeing the implementation since the operation is different for each Direction enum. What do you think? Is there a better way I am not seeing?

//returns location as [row, col]
int[] tempLocation = player.getObjectLocation();

switch (direction){
                    case NORTH -> {
                        if (getObjectAt(tempLocation[0] - 1, tempLocation[1]) instanceof Space){
                            this.world[tempLocation[0]][tempLocation[1]] = new Space(player.getDreamLocation());
                            this.world[tempLocation[0]-1][tempLocation[1]] = player;
                            player.changeDreamLocation(-1,0);
                        }
                    }
                    case EAST -> {
                        if (getObjectAt(tempLocation[0], tempLocation[1]   1) instanceof Space){
                            this.world[tempLocation[0]][tempLocation[1]] = new Space(player.getDreamLocation());
                            this.world[tempLocation[0]][tempLocation[1]   1] = player;
                            player.changeDreamLocation(0,1);
                        }
                    }
                    case SOUTH -> {
                        if (getObjectAt(tempLocation[0]   1, tempLocation[1]) instanceof Space){
                            this.world[tempLocation[0]][tempLocation[1]] = new Space(player.getDreamLocation());
                            this.world[tempLocation[0]   1][tempLocation[1]] = player;
                            player.changeDreamLocation(1,0);
                        }
                    }
                    case WEST -> {
                        if (getObjectAt(tempLocation[0], tempLocation[1] - 1) instanceof Space){
                            this.world[tempLocation[0]][tempLocation[1]] = new Space(player.getDreamLocation());
                            this.world[tempLocation[0]][tempLocation[1] - 1] = player;
                            player.changeDreamLocation(0,-1);
                        }
                    }
                }

CodePudding user response:

I would extract just the indices in the switch and put everything else in a generic method. You have to define valid initial values for playerX/playerY and dreamLocationX/dreamLocationY:

//returns location as [row, col]
int[] tempLocation = player.getObjectLocation();

int playerX = -1;
int playerY = -1;
int dreamLocationX = -1;
int dreamLocationY = -1;

switch (direction) {
    case NORTH:
        playerX = tempLocation[0] - 1;
        playerY = tempLocation[1];
        dreamLocationX = -1;
        dreamLocationY = 0;
        break;
    case EAST:
        playerX = tempLocation[0];
        playerY = tempLocation[1]   1;
        dreamLocationX = 0;
        dreamLocationY = 1;
        break;
    case SOUTH:
        playerX = tempLocation[0]   1;
        playerY = tempLocation[1];
        dreamLocationX = 1;
        dreamLocationY = 0;
        break;
    case WEST:
        playerX = tempLocation[0];
        playerY = tempLocation[1] - 1;
        dreamLocationX = 0;
        dreamLocationY = -1;
        break;
}

if (getObjectAt(playerX, playerY) instanceof Space) {
    this.world[tempLocation[0]][tempLocation[1]] = new Space(player.getDreamLocation());
    this.world[playerX][playerY] = player;
    player.changeDreamLocation(dreamLocationX, dreamLocationY);
}
  • Related