Untitled

 avatar
unknown
java
5 months ago
12 kB
3
Indexable
/**
 * @author Simão Gonçalves
 */



import java.util.Scanner;

final char TREASURE_ROOM_SYMBOL = 'T';
final char LOOPER_ROOM_SYMBOL = 'L';
final char STARTING_POSITION_SYMBOL = 'P';
final char ESCAPE_ROOM_SYMBOL = 'E';
final char STAIR_ROOM_SYMBOL = 'S';
final char ZIGZAGGER_ROOM_SYMBOL = 'Z';
final char EMPTY_ROOM_SYMBOL = '.';
final int LEVEL1 = 0;
final int LEVEL2 = 1;
final int NUMBER_OF_LEVELS = 2;
final int looperSteps = 1;
final String MOVE_RIGHT = "right";
final String MOVE_LEFT = "left";
final String QUIT = "quit";

final String LOOPER = "looper";
final String ZIGZAGGER = "zigzagger";



char[][] dungeon = new char[NUMBER_OF_LEVELS][];
char[][] enemyPositions = new char[NUMBER_OF_LEVELS][];
int zigzaggerSteps = 1;
int totalTreasures = 0;
int treasuresCollected = 0;
int currentRoom = 0;
int currentLevel = 0;
int[] escapePosition = new int[2];
boolean gameOver;
boolean quitGame;
boolean winGame;
boolean lostTheGameAnnounced = false;
boolean winTheGameAnnounced = false;


//Este método tem como função reiniciar/inicializar as variáveis globais do jogo, de forma a poder iniciá-lo
void inItState() {
    totalTreasures = 0;
    treasuresCollected = 0;
    gameOver = false;
    winGame = false;
    currentRoom = 0;
    currentLevel = 0;
    quitGame = false;
    winTheGameAnnounced = false;
    lostTheGameAnnounced = false;
    zigzaggerSteps = 1;

}

/*Este método tem como função encontrar as posição inicial do jogador e posição da saída recorrendo a do while loops que
percorrem os dois níveis e todas as salas à procura de cada uma dessas posições, acabando de iterar quando isso acontece,
guardando a posção inicial em duas variáveisdistintas que correspondem ao nível atual e à sala atual e
a posição de saída num vetor unidimensional com duas posições
 */
void findPositions() {
    boolean foundStartingPosition = false;
    boolean foundEscapePosition = false;
    int i = 0;
    do {
        int j = 0;
        do {
            char symbol = dungeon[i][j];
            if (symbol == STARTING_POSITION_SYMBOL) {
                currentLevel = i;
                currentRoom = j;
                foundStartingPosition = true;}
            if (symbol == ESCAPE_ROOM_SYMBOL) {
                escapePosition[0] = i;
                escapePosition[1] = j;
                foundEscapePosition = true;
            }
            if (foundStartingPosition && foundEscapePosition) {
                break;
            }
            j++;
        } while (j < dungeon[i].length);
        if (foundStartingPosition && foundEscapePosition) {
            break;
        }
        i++;
    } while (i < dungeon.length);
}

/*Este método conta todos os tesouros do mapa iterando com um for loop todas as salas de cada nível do mapa e adicionando
um valor à contagem total dos tesouros do mapa cada vez que encontra um
 */
int countTotalTreasures() {
    int count = 0;
    for (int i = 0; i < dungeon.length; i++) {
        for (int j = 0; j < dungeon[i].length; j++) {
            if (dungeon[i][j] == TREASURE_ROOM_SYMBOL) {
                count++;
            }
        }
    }

    return count;
}

void playGame(Scanner scanner) {

    while (!quitGame) {

        if (gameOver) {
            if (!lostTheGameAnnounced) {
                System.out.println("You lost the game!");
                lostTheGameAnnounced = true;
            }

            handlePostGameInput(scanner);
            continue;
        }

        if (winGame) {
            if (!winTheGameAnnounced) {
                System.out.println("You won the game!");
                winTheGameAnnounced = true;
            }

            handlePostGameInput(scanner);
            continue;
        }

        String direction = getInputDirection(scanner);
        if (direction.equals(QUIT)) {
            quitGame = true;
            break;
        }

        int steps = getInputSteps(scanner);
        processGameTurn(direction, steps);
    }

    // Mensagem de encerramento do jogo
    if (winGame) {
        System.out.println("Goodbye: You won the game!");
    } else if (gameOver) {
        System.out.println("Goodbye: You lost the game!");
    } else {
        System.out.println("The game was not over yet!");
    }
}

void processGameTurn(String direction, int steps) {

    movePlayer(direction, steps);

    checkForTreasure();

    checkForStairs();

    moveEnemies();

    checkForEnemyCollision();


    if (checkWin()) {
        winGame = true;
    }
    if(!gameOver && !quitGame && !winGame)
        displayGameInformation();
}

void movePlayer(String direction, int steps) {
    if (direction.equals(MOVE_RIGHT)) {
        currentRoom = Math.min(currentRoom + steps, dungeon[currentLevel].length - 1);
    } else if (direction.equals(MOVE_LEFT)) {
        currentRoom = Math.max(currentRoom - steps, 0);
    }
}

void checkForTreasure() {
    if (dungeon[currentLevel][currentRoom] == TREASURE_ROOM_SYMBOL) {
        treasuresCollected++;
        dungeon[currentLevel][currentRoom] = EMPTY_ROOM_SYMBOL;
    }
}

void moveEnemies() {

    char[][] enemyPositionsBuffer = cloneEnemyPositions(enemyPositions);


    moveLoopers(enemyPositionsBuffer);


    moveZigzaggers(enemyPositionsBuffer);
    zigzaggerSteps++;

    for (int i = 0; i < enemyPositions.length; i++) {
        enemyPositions[i] = enemyPositionsBuffer[i].clone();
    }

}

char[][] cloneEnemyPositions(char[][] enemyPositions) {
    char[][] buffer = new char[enemyPositions.length][];
    for (int i = 0; i < enemyPositions.length; i++) {
        buffer[i] = enemyPositions[i].clone();
    }
    return buffer;
}

void moveLoopers(char[][] enemyPositionsBuffer) {
    int i = 0;
    int j = 0;


    do {

        while (j < dungeon[i].length) {
            if (enemyPositions[i][j] == LOOPER_ROOM_SYMBOL) {
                enemyPositionsBuffer[i][j] = EMPTY_ROOM_SYMBOL;


                if (j + 1 >= dungeon[i].length) {
                    enemyPositionsBuffer[i][0] = LOOPER_ROOM_SYMBOL;
                } else {
                    enemyPositionsBuffer[i][j + 1] = LOOPER_ROOM_SYMBOL;
                }
            }
            j++;
        }

        j = 0;
        i++;
    } while (i < dungeon.length);
}

void moveZigzaggers(char[][] enemyPositionsBuffer) {
    int i = 0;
    int j = 0;

    do {
        while (j < dungeon[i].length) {
            if (zigzaggerSteps > 5) {
                zigzaggerSteps = 1;
            }
            if (enemyPositions[i][j] == ZIGZAGGER_ROOM_SYMBOL) {
                enemyPositionsBuffer[i][j] = EMPTY_ROOM_SYMBOL;


                int newPosition = j + zigzaggerSteps;


                while (newPosition >= dungeon[i].length) {
                    newPosition = newPosition % dungeon[i].length;
                }


                enemyPositionsBuffer[i][newPosition] = ZIGZAGGER_ROOM_SYMBOL;


            }
            j++;
        }
        j = 0; //
        i++; //
    } while (i < dungeon.length);
}

void checkForStairs() {
    if(dungeon[currentLevel][currentRoom] == STAIR_ROOM_SYMBOL) {
        if(currentLevel == LEVEL1)
            currentLevel = LEVEL2;
        else currentLevel = LEVEL1;

        currentRoom = findStairPosition(currentLevel);
    }
}

int findStairPosition(int level) {
    int i = 0;
    do {
        if (dungeon[level][i] == STAIR_ROOM_SYMBOL) {
            return i;
        }
        i++;
    } while (i < dungeon[level].length);

    return -1;
}

void checkForEnemyCollision() {

    if (currentLevel >= 0 && currentLevel < enemyPositions.length &&
            currentRoom >= 0 && currentRoom < enemyPositions[currentLevel].length) {

        char symbol = enemyPositions[currentLevel][currentRoom];
        if (symbol == LOOPER_ROOM_SYMBOL || symbol == ZIGZAGGER_ROOM_SYMBOL) {
            gameOver = true;
        }
    }
}

boolean checkWin() {
    return treasuresCollected == totalTreasures && dungeon[currentLevel][currentRoom] == ESCAPE_ROOM_SYMBOL
            && !gameOver;
}

void shouldContinue(String direction) {
    if (direction.equals(QUIT)) {
        quitGame = true;
    }
}

int findLevelEnemy(int level){

    int i = 0;
    do{

        char symbol = enemyPositions[level][i];
        if(symbol == LOOPER_ROOM_SYMBOL || symbol == ZIGZAGGER_ROOM_SYMBOL) {

            return i;
        }

        i++;
    }while (i < enemyPositions[level].length);
    return -1;
}

String getEnemyFromType(char type) {
    if(type == LOOPER_ROOM_SYMBOL) return LOOPER;
    else return ZIGZAGGER;
}

void displayGameInformation() {
    System.out.println("Player: level " + (currentLevel + 1) + ", room " + (currentRoom + 1) + ", treasures " + treasuresCollected);
    displayLevelEnemy(LEVEL1);
    displayLevelEnemy(LEVEL2);
}

void displayLevelEnemy(int level) {
    int enemyPosition = findLevelEnemy(level);
    char enemySymbol = enemyPositions[level][enemyPosition];
    System.out.println("Level " + (level+1) + " enemy: " + getEnemyFromType(enemySymbol) + ", room " + (enemyPosition+1));
}

int getInputSteps(Scanner scanner) {
    return scanner.nextInt();
}

char[] readLevel(Scanner scanner) {
    String input = scanner.nextLine();
    return levelsInChar(input);
}

char[] levelsInChar(String sLevel) {
    char[] level;
    level = sLevel.toCharArray();
    return level;
}

String getInputDirection(Scanner scanner) {
    String direction = scanner.next();

    if (direction.equals(QUIT)) {
        shouldContinue(direction);
        return direction;
    }

    // Verifica se o comando é inválido enquanto o jogo ainda está ativo
    if (!gameOver && !winGame && !direction.equals(MOVE_RIGHT) && !direction.equals(MOVE_LEFT)) {
        scanner.nextLine();
        System.out.println("Invalid command");
    }

    while (!gameOver && !winGame && !direction.equals(MOVE_RIGHT) && !direction.equals(MOVE_LEFT)) {
        direction = scanner.next();

        if (direction.equals(QUIT)) {
            shouldContinue(direction);
            return direction;
        } else if (!direction.equals(MOVE_RIGHT) && !direction.equals(MOVE_LEFT)) {
            scanner.nextLine();
            System.out.println("Invalid command");
        }
    }

    return direction;
}


void handlePostGameInput(Scanner scanner) {
    // Pega o input do jogador após o término do jogo
    String direction = getInputDirection(scanner);

    // Se o comando for 'quit', finaliza o jogo
    if (direction.equals(QUIT)) {
        quitGame = true;
    }
    // Se o comando for inválido, exibe 'Invalid command'
    else if (!direction.equals(MOVE_RIGHT) && !direction.equals(MOVE_LEFT)) {
        scanner.nextLine();
        System.out.println("Invalid command");
    }
    // Se o comando for válido, exibe 'The game is Over!'
    else {
        scanner.nextLine();
        System.out.println("The game is Over!");
    }
}


void main() {
    Scanner scanner = new Scanner(System.in);
    inItState();

    dungeon[1] = readLevel(scanner);
    dungeon[0] = readLevel(scanner);

    enemyPositions = new char[NUMBER_OF_LEVELS][]; // Inicializa as posições dos inimigos
    for (int i = 0; i < dungeon.length; i++) {
        enemyPositions[i] = new char[dungeon[i].length]; // Inicializa as linhas corretamente
        for (int j = 0; j < dungeon[i].length; j++) {
            enemyPositions[i][j] = dungeon[i][j]; // Copia as posições do nível
        }
    }

    findPositions();
    totalTreasures = countTotalTreasures();

    playGame(scanner);

    scanner.close();
}


/**
 * Guardar posições dos inimigos numa array como fiz com o movePlayer e não ter de procurar todas as vezes
 *
 *
 *
 *
 *
 *
 *
 */

//EXEMPLO 27 ESTÁ A DIZER INVALID COMMAND
Editor is loading...
Leave a Comment