Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate player rotation movement and forward movement actions #1

Merged
merged 10 commits into from
Mar 25, 2024
252 changes: 69 additions & 183 deletions DarkerDungeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void main(String[] args){
* @brief Menu class that holds game state information about the program
*/
class Menu{
public enum MenuOptions { ZERO, DONOTHING, FORWARD, LEFT, RIGHT, BACKWARD, OPENDOOR, END}
public enum MenuOptions { ZERO, DONOTHING, FORWARD, TURNLEFT, TURNRIGHT, OPENDOOR, END}
ArrayList<Boolean> unlockedOptions;
private Map myMap;

Expand All @@ -65,22 +65,19 @@ public void printMenu(){
MenuOptions index = MenuOptions.values()[i];
if(unlockedOptions.get(i)){
switch(index){
case MenuOptions.DONOTHING:
case DONOTHING:
System.out.println(MenuOptions.DONOTHING.ordinal() + ": Do Nothing");
break;
case MenuOptions.FORWARD:
case FORWARD:
System.out.println(MenuOptions.FORWARD.ordinal() + ": Walk Forward");
break;
case MenuOptions.LEFT:
System.out.println(MenuOptions.LEFT.ordinal() + ": Walk Left");
case TURNLEFT:
System.out.println(MenuOptions.TURNLEFT.ordinal() + ": Turn Left");
break;
case MenuOptions.RIGHT:
System.out.println(MenuOptions.RIGHT.ordinal() + ": Walk Right");
case TURNRIGHT:
System.out.println(MenuOptions.TURNRIGHT.ordinal() + ": Turn Right");
break;
case MenuOptions.BACKWARD:
System.out.println(MenuOptions.BACKWARD.ordinal() + ": Backtrack");
break;
case MenuOptions.OPENDOOR:
case OPENDOOR:
System.out.println(MenuOptions.OPENDOOR.ordinal() + ": Open Door");
break;
default:
Expand Down Expand Up @@ -127,26 +124,22 @@ public MenuOptions parseUserInput(String userRawInput){
*/
public Boolean printAndDoResponse(MenuOptions userOption){
switch(userOption){
case MenuOptions.FORWARD:
case FORWARD:
System.out.println("\nYou decide to walk for a bit\n");
myMap.movePlayer(Map.PlayerMovements.FORWARD);
break;
case MenuOptions.LEFT:
System.out.println("\nYou decide to turn left and walk for a bit\n");
myMap.movePlayer(Map.PlayerMovements.LEFT);
myMap.movePlayer();
break;
case MenuOptions.RIGHT:
System.out.println("\nYou decide to turn right and walk for a bit\n");
myMap.movePlayer(Map.PlayerMovements.RIGHT);
case TURNLEFT:
System.out.println("\nYou decide to turn left\n");
myMap.turnPlayerLeft();
break;
case MenuOptions.BACKWARD:
System.out.println("\nYou decide to turn around and walk for a bit\n");
myMap.movePlayer(Map.PlayerMovements.BACKWARD);
case TURNRIGHT:
System.out.println("\nYou decide to turn right\n");
myMap.turnPlayerRight();
break;
case MenuOptions.DONOTHING:
case DONOTHING:
System.out.println("\nYou decide to do nothing for a while\n");
break;
case MenuOptions.OPENDOOR:
case OPENDOOR:
System.out.println(" \\ /");
System.out.println(" \\_ _ _/");
System.out.println(" / _ \\ ");
Expand All @@ -173,9 +166,6 @@ public Boolean printAndDoResponse(MenuOptions userOption){
public void updateMenu(){
unlockedOptions.set(MenuOptions.OPENDOOR.ordinal(), myMap.checkExit());
unlockedOptions.set(MenuOptions.FORWARD.ordinal(), myMap.checkPlayerFORWARD());
unlockedOptions.set(MenuOptions.LEFT.ordinal(), myMap.checkPlayerLEFT());
unlockedOptions.set(MenuOptions.BACKWARD.ordinal(), myMap.checkPlayerBACKWARD());
unlockedOptions.set(MenuOptions.RIGHT.ordinal(), myMap.checkPlayerRIGHT());
}

public void clearScreen() {
Expand All @@ -192,7 +182,6 @@ public void clearScreen() {
*/
class Map{
private enum Directions { NORTH, EAST, SOUTH, WEST }
public enum PlayerMovements {FORWARD, LEFT, BACKWARD, RIGHT}
private int width;
private int height;
private Directions playerFront;
Expand Down Expand Up @@ -220,207 +209,104 @@ public Map(int width, int height){
}

/**
* @brief Function to update player information based on a requested move action
* @brief Function to update player location to be 1 square forward
* @apiNote Warning this function does not do bounds checking for you
*/
public void movePlayer(PlayerMovements myMove){
public void movePlayer(){
switch(playerFront){
case Directions.NORTH:
switch(myMove){
case PlayerMovements.FORWARD:
currentLocationY--;
break;
case PlayerMovements.LEFT:
currentLocationX--;
playerFront = Directions.WEST;
break;
case PlayerMovements.BACKWARD:
currentLocationY++;
playerFront = Directions.SOUTH;
break;
case PlayerMovements.RIGHT:
currentLocationX++;
playerFront = Directions.EAST;
break;
}
case NORTH:
currentLocationY--;
break;
case Directions.EAST:
switch(myMove){
case PlayerMovements.FORWARD:
currentLocationX++;
break;
case PlayerMovements.LEFT:
currentLocationY--;
playerFront = Directions.NORTH;
break;
case PlayerMovements.BACKWARD:
currentLocationX--;
playerFront = Directions.WEST;
break;
case PlayerMovements.RIGHT:
currentLocationY++;
playerFront = Directions.SOUTH;
break;
}
case EAST:
currentLocationX++;
break;
case Directions.SOUTH:
switch(myMove){
case PlayerMovements.FORWARD:
currentLocationY++;
break;
case PlayerMovements.LEFT:
currentLocationX++;
playerFront = Directions.EAST;
break;
case PlayerMovements.BACKWARD:
currentLocationY--;
playerFront = Directions.NORTH;
break;
case PlayerMovements.RIGHT:
currentLocationX--;
playerFront = Directions.WEST;
break;
}
case SOUTH:
currentLocationY++;
break;
case Directions.WEST:
switch(myMove){
case PlayerMovements.FORWARD:
currentLocationX--;
break;
case PlayerMovements.LEFT:
currentLocationY++;
playerFront = Directions.SOUTH;
break;
case PlayerMovements.BACKWARD:
currentLocationX++;
playerFront = Directions.EAST;
break;
case PlayerMovements.RIGHT:
currentLocationY--;
playerFront = Directions.NORTH;
break;
}
case WEST:
currentLocationX--;
break;
}
}

/**
* @brief Function to check if the player is at the exit door
* @return boolean based on if the player is or is not at the exit door
*/
public Boolean checkExit(){
if(this.currentLocationX == this.exitLocationX &&
this.currentLocationY == this.exitLocationY){
return true;
}
return false;
}

/**
* @brief Function to check if the player can move forward
* @return boolean based on if the player can move forward or not
* @brief Function to update the players direction for a 90 degree
* left turn
*/
public Boolean checkPlayerFORWARD(){
switch (playerFront){
case Directions.NORTH:
if(currentLocationY > 0)
return true;
public void turnPlayerLeft(){
switch(playerFront){
case NORTH:
playerFront = Directions.WEST;
break;
case Directions.WEST:
if(currentLocationX > 0)
return true;
case EAST:
playerFront = Directions.NORTH;
break;
case Directions.SOUTH:
if(currentLocationY < height)
return true;
case SOUTH:
playerFront = Directions.EAST;
break;
case Directions.EAST:
if(currentLocationX < width)
return true;
case WEST:
playerFront = Directions.SOUTH;
break;
}

return false;
}

/**
* @brief Function to check if the player can move left
* @return boolean based on if the player can move left or not
* @brief Function to update the players direction for a 90 degree
* right turn
*/
public Boolean checkPlayerLEFT(){
switch (playerFront){
case Directions.NORTH:
if(currentLocationX > 0)
return true;
public void turnPlayerRight(){
switch(playerFront){
case NORTH:
playerFront = Directions.EAST;
break;
case Directions.WEST:
if(currentLocationY < height)
return true;
case EAST:
playerFront = Directions.SOUTH;
break;
case Directions.SOUTH:
if(currentLocationX < width)
return true;
case SOUTH:
playerFront = Directions.WEST;
break;
case Directions.EAST:
if(currentLocationY > 0)
return true;
case WEST:
playerFront = Directions.NORTH;
break;
}

return false;
}

/**
* @brief Function to check if the player can move backward
* @return boolean based on if the player can move backward or not
* @brief Function to check if the player is at the exit door
* @return boolean based on if the player is or is not at the exit door
*/
public Boolean checkPlayerBACKWARD(){
switch (playerFront){
case Directions.NORTH:
if(currentLocationY < height)
return true;
break;
case Directions.WEST:
if(currentLocationX < width)
return true;
break;
case Directions.SOUTH:
if(currentLocationY > 0)
return true;
break;
case Directions.EAST:
if(currentLocationX > 0)
return true;
break;
public Boolean checkExit(){
if(this.currentLocationX == this.exitLocationX &&
this.currentLocationY == this.exitLocationY){
return true;
}

return false;
}

/**
* @brief Function to check if the player can move right
* @return boolean based on if the player can move tight or not
* @brief Function to check if the player can move forward
* @return boolean based on if the player can move forward or not
*/
public Boolean checkPlayerRIGHT(){
public Boolean checkPlayerFORWARD(){
switch (playerFront){
case Directions.NORTH:
if(currentLocationX < width)
return true;
break;
case Directions.WEST:
case NORTH:
if(currentLocationY > 0)
return true;
break;
case Directions.SOUTH:
case WEST:
if(currentLocationX > 0)
return true;
break;
case Directions.EAST:
case SOUTH:
if(currentLocationY < height)
return true;
break;
case EAST:
if(currentLocationX < width)
return true;
break;
}

return false;
return false;
}
}