Untitled

mail@pastecode.io avatar
unknown
java
a year ago
6.8 kB
2
Indexable
Never
    
public void takeTurns()
    {
        currentPlayerIndex = 0;

        while (playerList.size() > 1) {
            Character currentPlayer = playerList.get(currentPlayerIndex);

            // Print the board
            System.out.println("Player " + currentPlayer.getName() + "'s turn: ( icon : " + currentPlayer.getIcon() + " )");
            printBoard();

            // Ask the current player to make a move
            System.out.print("Enter your move (U/D/L/R): ");
            char move = scanner.next().charAt(0);
            scanner.nextLine(); // Consume newline

            // Perform the move without validation
            attemptMove(this, currentPlayer, move);
            currentPlayerIndex = (currentPlayerIndex + 1) % playerList.size();
        }

        // Print the winner
        System.out.println("Player " + playerList.get(0).getName() + " wins! ( icon : " + playerList.get(0).getIcon() + " )");
    }    





public void attemptMove(Board board, Character currentPlayer, char move) {
        int x = currentPlayer.getPoint().getX();
        int y = currentPlayer.getPoint().getY();

        switch (java.lang.Character.toUpperCase(move)) {
            case 'U':
                x--;
                break;
            case 'D':
                x++;
                break;
            case 'L':
                y--;
                break;
            case 'R':
                y++;
                break;
            default:
                System.out.println("Invalid input pls try again ");
                System.out.println("Enter you new move please ");
                move = scanner.next().charAt(0);
                attemptMove(this, currentPlayer, move);
                return;
        }

        Point newPosition = new Point(x, y);

        if (isValidMove(currentPlayer, newPosition)) {
            // Check if there's a weapon at the new position
            Weapon weaponAtNewPosition = board.getWeaponAt(newPosition);
            System.out.println(weaponAtNewPosition);
            if (weaponAtNewPosition != null) {
                // Add the weapon to the current player's queue
                currentPlayer.addWeapon(weaponAtNewPosition);
                System.out.println("This is the player weapon " + currentPlayer.getWeapon());

                // Remove the weapon from the board
                board.setBoardPosition(newPosition, null);
            }

            // Check if there's an opponent character at the new position
            Character opponent = board.getCharacterAt(newPosition);

            if (opponent != null) {
                // Resolve the encounter between the current player and the opponent
                boolean doMove = resolveEncounter(currentPlayer, opponent); // Pass the current player and the opponent
                if(doMove){
                    // Remove the current player from their current position on the board
                    board.setBoardPosition(currentPlayer.getPoint(), null);

                    // Update the current player's position
                    currentPlayer.setPoint(newPosition);

                    // Place the current player in their new position on the board
                    board.setBoardPosition(newPosition, currentPlayer);

                }
            }else{
                // Remove the current player from their current position on the board
                board.setBoardPosition(currentPlayer.getPoint(), null);

                // Update the current player's position
                currentPlayer.setPoint(newPosition);

                // Place the current player in their new position on the board
                board.setBoardPosition(newPosition, currentPlayer);

            }

        }
        else
        {
            System.out.println("Invalid move. Please try again.");
            System.out.println("Enter you new move please ");
            move = scanner.next().charAt(0);
            attemptMove(this, currentPlayer, move);
        }

    }



    
private boolean resolveEncounter(Character player, Character opponent) {
        // Check if both players have no weapons
        Point position = opponent.getPoint();
        if (player.getWeapon() == null && opponent.getWeapon() == null) {
            return false;// No winner, nothing happens
        }

        // Check if one player has no weapon
        if (player.getWeapon() == null || opponent.getWeapon() == null) {
            // Remove the player without a weapon from the board and the game
            Weapon weapon = (player.getWeapon() != null) ? player.getWeaponQueue().remove() : opponent.getWeaponQueue().remove();

            removeCharacter(player.getWeapon() == null ? player : opponent);
            return true;
        }

        boolean playerWins = player.getWeapon().interact(opponent.getWeapon());

        if (playerWins) {
            System.out.println("3");
            // Player wins the encounter:
            // - Collect one weapon from the opponent and add it to the player's queue
            Weapon opponentWeapon = opponent.getWeapon(); // Assuming you have a removeTopWeapon method
            if (opponentWeapon != null) {
                player.getWeaponQueue().remove();
                player.addWeapon(opponentWeapon);
                System.out.println("Now Player " +player.getName()+ " has " +player.getWeapon());
            }
            removeCharacter(opponent);
            setBoardPosition(position,player);
        } else {
            Weapon playerWeapon = player.getWeapon(); // Assuming you have a removeTopWeapon method
            if (playerWeapon != null) {
                opponent.getWeaponQueue().remove();
                opponent.addWeapon(playerWeapon);
                System.out.println("Now Player " +opponent.getName()+ " has " +opponent.getWeapon());
            }
            removeCharacter(player);
            this.board[opponent.getPoint().getX()][opponent.getPoint().getY()] = opponent;
        }
        return true;
    }
    
    
        public void removeCharacter(Character character) {
        // Debug statement to check if the function is called
        System.out.println("Removing player: " + character.getName());

        playerList.remove(character);
        if(currentPlayerIndex > 0) {
            currentPlayerIndex--;
        }
        else if (currentPlayerIndex ==0)
        {
            currentPlayerIndex++;
        }

        // Print a message to indicate that the player has been removed
        System.out.println("Player " + character.getName() + " has been removed from the game. ( icon : " + character.getIcon() + " )");
    }