我正在为分配的任务构建战争纸牌游戏,并且已经正常运行,但是我遇到了问题,例如卡片10的黑桃价值较低,例如卡片23的黑心。寻找有关最好的方式的建议,以便能够比较卡是否相等。

下面是我到目前为止的代码:

import java.util.Random;
import java.util.Scanner;

public class WarGame {

    public static void main(String a[]) {
        DeckOfCards d = new DeckOfCards();
        int input = 0;
        int computer = 0;
        int you = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("\n1.To play\n2.Exit\nEnter the choice:");
        input = sc.nextInt();
        while (input != 2) {
            if (input == 1) {
                System.out.print("\n\nEnter the value of the card:");
                int card = sc.nextInt();
                if (card >= 0 && card <= 51) {
                    int systemCard = d.computerTurn(card);
                    System.out.println("Your card - " + d.inputCard(card));
                    System.out.println("Computer card - " + d.inputCard(systemCard));
                    if(systemCard > card)
                        ++computer;
                    else
                        ++you;
                    System.out.println("The winner is " + (systemCard > card ? "Computer" : "You"));
                } else {
                    System.out.println("Invalid card");
                }
            }
            else {
            System.out.println("That is an invalid selection please choose 1 or 2.");
            }
            System.out.print("\n1.To play\n2.Exit\nEnter the choice:");
            input = sc.nextInt();
        }
        System.out.println("Total Wins by Computer: "+ computer);
        System.out.println("Total Wins by You: "+ you);
        if (computer > you)
            System.out.println("Computer is the champion");
        else if (computer == you)
            System.out.println("Its a Tie");
        else
            System.out.println("You are the champion");
    }
    }

    class DeckOfCards {
        String suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
        Random ran = new Random();
        int systemWin = 0;
        int playerWin = 0;
        String inputCard(int card) {
            int suit = card / 13; //assigning suit to your card
            int rank = card % 13; //Assigning rank to your card
            String out = "";
            switch (rank) { //Setting up face cards for the cases
            case 0:
                out = "Ace of " + suits[suit];
                break;
            case 10:
                out = "Jack of " + suits[suit];
                break;
            case 11:
                out = "Queen of " + suits[suit];
                break;
            case 12:
                out = "King of " + suits[suit];
                break;
            default:
                out = rank + 1 + " of " + suits[suit]; //Adding one to remainder so it will go from 2-10 instead of 1-9
                break;
            }
            return out;
        }

        int computerTurn(int playerRank) { //Keeping track of the wins for computer and player
            int systemRank = this.ran.nextInt(51);
            if (systemRank > playerRank)
                systemWin++;
            else
                playerWin++;
            return systemRank;
        }
    }

最佳答案

我认为您是在将索引与卡组而不是卡值本身进行比较。据我了解,您想将d.inputCard(card)与d.inputCard(systemCard)进行比较,而不是将card。与systemCard进行比较。但是,当然,这是一个字符串。很难遵循代码:-)。

关于java - 建筑纸牌游戏需要相同的纸牌才能匹配值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51433643/

10-09 03:02