简体   繁体   中英

How do i make a card game with Print statements that only allows each card to be used a specific number of times?

I'm working on a card game for a personal project and I was wondering how I could make it so that each specific card can only be used a set number of times. I'm thinking about making an loop that adds to a specific number, and if the number reaches that specific value, the card can no longer be played. My issue is that I am having trouble with syntax and have just been confusing myself. cardLimiter is the variable i want to use to add to a specific value, though I just need a few pointers. Thanks!

public static void emperorsTurn() {

        Random cards = new Random();
        int computerinput = 0;
        int numberx = 5;

        for (int counter = 1; counter <= 3; counter++) {
            computerinput = 1 + cards.nextInt(2);
        }


        Scanner sc = new Scanner(System.in);
        System.out.println("Please pick the card you are playing. \n if you are playing the Emperor press 1, if you are playing the citizen press 2 ");
        int userinput = sc.nextInt();

        if (userinput == 1 && computerinput == 1 && cardLimiter == 0) {
            System.out.println("you have played the emperor! \n the emperor is defeated by the slave");
                cardLimiter++;
                if ((userinput == 1 && computerinput == 1 && cardLimiter == 1)) {
                    System.out.println("you cannot play the emperor this turn \n you have played the citizen instead");
            }

        } else if (userinput == 1 && computerinput == 2) {
            System.out.println("you have played the emperor the emperor defeats the citizen");

            winOrLose();
            wincounter();
            numberx--;
        } else if (userinput == 2) { //when the user input is 2
            if (computerinput == 1) {
                System.out.println("you have played the citizen, this defeats the slave");
                wincounter();
            } else if (computerinput == 2) {
                System.out.println("you have played the citizen, this ties with the citizen");
            }
            //print out something else if number is not 1,2 or 3
        }
    }

In this part, you've got the second if statement nested inside the first:

 if (userinput == 1 && computerinput == 1 && cardLimiter == 0) {
        System.out.println("you have played the emperor! \n the emperor is defeated by the slave");
            cardLimiter++;
            if ((userinput == 1 && computerinput == 1 && cardLimiter == 1)) {
                System.out.println("you cannot play the emperor this turn \n you have played the citizen instead");
        }

    }

If userinput == 1 && computerinput == 1 && cardLimiter == 0 it will print out "you have played the emperor" etc. But then you immediately increment cardLimiter , so it will also print out "you cannot play the emperor this turn".

I imagine that's not the desired behavior, so I would recommend moving that second if statement out of the first one. Perhaps something like this:

if (userinput == 1 && computerinput == 1 && cardLimiter == 0) {
        System.out.println("you have played the emperor! \n the emperor is defeated by the slave");
            cardLimiter++;
    } else if ((userinput == 1 && computerinput == 1 && cardLimiter == 1)) {
                System.out.println("you cannot play the emperor this turn \n you have played the citizen instead");
        }

That way, only one of the System.out.println statements will execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM