Skip to content
Snippets Groups Projects
CmdUI.java 6.18 KiB
import java.util.Scanner;

public class CmdUI {
    Player player;
    CardParser parser;
    public static Game gameController;
    public static RuleController ruler;

    public CmdUI(Player p) {
        player = p;
        gameController = player.gameController;
        parser = (gameController != null) ? player.parser : null;

        ruler = (gameController != null) ? gameController.ruler : null;

    }

    /**
     * FOR CMD LINE DEBUGGING
     * When it's this player's turn, the player must pick one card to play
     * @return the ** cardID ** of the chosen card
     */
    public int promptTakeAction() {
        // instruct player to take a move
        Scanner inputScanner = new Scanner(System.in);
        boolean receivedValidInput = false;
        int action = -1;
        while (!receivedValidInput) {
            promptChooseAction();
            String input = inputScanner.nextLine();
            try {
                action = Integer.parseInt(input);
                if (action == 1 || action == 2) {
                    receivedValidInput = true;
                }

            } catch (Exception e) {
                System.out.println("Please choose action from 1 or 2");
            }
        }

        if (action == 1) {
            return actionOne();
        } else if (action == 2){
            return actionTwo();
        }

        return -1; // user did not play valid card
    }

    private int actionOne() {

        int cardID = -1;
        while (true) {
            player.printCardsInHand();
            player.printLegalCards();
            System.out.println("Which card would you like to play? Please input the corresponding card number...");

            int chosenCardIndex = -1;

            while (chosenCardIndex < 0) {
                chosenCardIndex = getInputCardIndex();
            }
            cardID = player.getCards().get(chosenCardIndex);
            if(gameController.ruler.isValidPlay(player, cardID, true)) {
                // maintain list-cards and discardPile
                player.getCards().remove(chosenCardIndex);
                gameController.gameCardManager.insertOneCardToDiscardPile(cardID);
                break;
            }
        }

        if (parser.isWildCard(cardID)) {
            // to implement
            int colorChosen = -1;
            while (colorChosen < 0) {
                promptChooseColor();
                colorChosen = getInputColor();
            }
            ruler.setAllowedColor(parser.colorDict.get(colorChosen));
        }
        return cardID;
    }

    private int actionTwo () {
        player.drawCards( 1); // draw new card and add it to cards in hand
        int chosenCardIndex = player.getCards().size() - 1;
        int cardID = player.getCards().get(chosenCardIndex);
        if(ruler.isValidPlay(player, cardID, true)) {
            player.getCards().remove(chosenCardIndex);
            gameController.gameCardManager.insertOneCardToDiscardPile(cardID);

            if (parser.isWildCard(cardID)) {
                // to implement
                int colorChosen = -1;
                while (colorChosen < 0) {
                    promptChooseColor();
                    colorChosen = getInputColor();
                }
                gameController.ruler.setAllowedColor(parser.colorDict.get(colorChosen));
            }

            return cardID;
        } else {
            System.out.println("Sorry, the newly drawn card is not playable :(");
            return -1;
        }
    }


    /**
     * helper function for playCardWithCommandLine
     * parse the integer inputted by the player
     * @return the ** INDEX ** of card in list cards that user chooses
     */
    private int getInputCardIndex() {
        Scanner inputScanner = new Scanner(System.in);
        String input = inputScanner.nextLine();
        int decision;
        try {
            decision = Integer.parseInt(input) - 1; // -1 because we want the index in cards list
        } catch (Exception e) {
            System.out.println("Please input the number corresponding the card you choose!");
            return -1;
        }

        if (decision < 0 || decision >= player.getCards().size()) {
            System.out.println("The card you chose does not exist!");
            return -1;
        }
        return decision;
    }

    /**
     * helper function for playCardWithCommandLine
     * parse the integer inputted by the player
     * @return the ** INDEX ** of card in list cards that user chooses
     */
    private int getInputColor() {
        Scanner inputScanner = new Scanner(System.in);
        String input = inputScanner.nextLine();
        int decision;
        try {
            decision = Integer.parseInt(input);
        } catch (Exception e) {
            System.out.println("Please input the number corresponding the card you choose!");
            return -1;
        }

        if (decision != 1 && decision != 2 && decision != 3 && decision != 4) {
            System.out.println("Please choose a valid color (represented by number)!");
            return -1;
        }
        return decision;
    }

    /**
     * prompt player to take action current round
     */
    private void promptChooseAction() {
        player.printCardsInHand();
        System.out.println("The following are playable:");
        player.printLegalCards();
        System.out.println("Please choose you action this round:");
        System.out.println("[1] Play a owned card");
        System.out.println("[2] Draw a card and play it if possible");
        System.out.println("Please input 1 or 2 : ");
    }

    private void promptChooseColor() {
        player.printCardsInHand();
        System.out.println("Please pick a color for the next rounds:");
        System.out.println("[1] Red");
        System.out.println("[2] Green");
        System.out.println("[3] Blue");
        System.out.println("[4] Yellow");
        System.out.println("Please choose one from above: ");
    }

    public void printForcedSkip() {
        System.out.println("Sorry, you lost this turn and was forcefully skipped.");
    }



}