【问题标题】:Java Card game: my Card game removes 2 cards from the deck instead of 1Java 纸牌游戏:我的纸牌游戏从牌库中取出 2 张牌,而不是 1 张
【发布时间】:2023-03-28 11:10:02
【问题描述】:

运行此代码,以便您也可以玩它。

规则:猜下一张是大牌还是小牌。猜对了就得分,猜错了游戏结束。

问题是“Deck.getCards().size()”大小的牌组每次移除 2 张牌。我相信如果猜对了,我打印的卡片确实会显示另一张卡片,而不是我之前必须猜测的卡片。

谁能看出哪里不对。

额外问题:我的游戏中的王牌是价值最高的牌。但不知何故,我在获得 A 时猜测较低(也许这与我相信我有 A 的原因有关)如果有人能帮我解决这个问题,我会非常感激..

包装纸牌游戏;

导入 java.util.Scanner; 导入 java.util.*;

公开课游戏{

// score int
private static int score;

// currentCard : Card
private static Card currentCard;
// Next Card
private static Card nextCard;
// Scanner
private static Deck deck;

private static Scanner sc = new Scanner(System.in);

// Main

public static void main(String[] args) {

    deck = new Deck();

    currentCard = deck.getNextCard();

    Game.gameTurn();
}

// get turn method
public static void gameTurn() {
    // System.out.println(Deck.getCards().size());
    score = 0;


    System.out.print("your current card is " + currentCard.getName() + " is it higher or lower? > ");

    while (true) {

        String answer = sc.nextLine();
        Card nextCard = deck.getNextCard();

        if (answer.equals("higher") && nextCard.isHigherOrEqual(currentCard)) {

            correct();
        } else if (answer.equals("lower") && !nextCard.isHigherOrEqual(currentCard)) {
            correct();

        } else {

            gameOver();
            break;
        }
    }
}

// correct method

public static void correct() {

    score++;
    System.out.println("You are right! your score is: " + score);
    System.out.println(Deck.getCards().size() + " total cards");
    nextCard = Deck.getNextCard();  
    System.out.println("your current card is " + nextCard.getName());

}

// Game over Method

public static void gameOver() {
    System.out.println("you lost");
    System.out.println("total score is " + score + "!");
    System.out.println(Deck.getCards().size() + " total cards");

}

}

包装纸牌游戏;

导入 java.util.ArrayList; 导入 java.util.Collections;

公开课甲板{

// cards = Card = new ArrayList<>()
private static ArrayList<Card> cards = new ArrayList<>();

// Deck Method
public Deck() {

    for (int i = 0; i < 4; i++) {
        String suits;

        switch (i) {
        // hart
        case 0:
            suits = "Hearts";
            break;
        // ruit
        case 1:
            suits = "Diamonds";
            break;
        // schoppen
        case 2:
            suits = "Spades";
            break;
        // klaveren
        case 3:
            suits = "Clubs";
            break;

        default:
            suits = "";
            break;
        }
        for (int j = 2; j <= 10; j++) {
            int value = j;
            String name = j + " of " + suits;
            Card c = new Card(suits, name, value);

            cards.add(c);

        }
        // add 1.3 tot 1.11

        Card jack = new Card(suits, "Jack of " + suits, 11);

        cards.add(jack); // JACK

        Card queen = new Card(suits, "Queen of " + suits, 12);

        cards.add(queen); // QUEEN

        Card king = new Card(suits, "King of " + suits, 13);

        cards.add(king); // KING

        Card ace = new Card(suits, "Ace of " + suits, 14);
        cards.add(ace); // ACE

    }
    // 1.11 shuffle cards
    Collections.shuffle(cards);
}

// getNextCard() : Card

public static Card getNextCard() {

    Card nextCard = cards.remove(0);

    return nextCard;

}

// getCards() : ArrayList <card>
public static ArrayList<Card> getCards() {

    return cards;
}

}

包装纸牌游戏;

公共类卡片{

// Suit String
private String suit;

// Name String
private String name;
// Value int
private int value;

// Card constructor
public Card(String suit, String name, int value) {
    this.value = value;
    this.name = name;
    this.suit = suit;

}

public int getValue() {
    return value;
}

// toString Method
public String toString() {

    return suit;
}

public String getName() {
    return name;
}
// isHigherorEqual Method

public boolean isHigherOrEqual(Card c) {
    if (this.value >= c.getValue()) {
        return true;
    } else {
        return false;
    }
}

}

【问题讨论】:

  • 99 44/100% 确定 Deck.getCards().size() 不会删除任何卡片。但是nextCard = Deck.nextCard() 可以。你确定你想在那个时候发另一张牌吗?或者nextCard 是否应该成为下一次猜测的currentCard
  • 下一张卡片应该是您应该猜到的。示例:第一张牌是 3 个壁炉(你的答案更高) 正确!你的下一张牌是方块 5。好的,您的下一张牌是否高于 5 颗钻石? (等等......)我确实用 currentCard 和 nextCard 做了一些实验,所以这就是为什么它会被搞砸的原因。它每次都会删除 2 张卡片,我不知道为什么。
  • 拿出你的调试器并逐步执行。这就是你会看到的。在main 中,您首先处理一张“当前”牌,询问用户“更高还是更低?”。现在循环,你得到用户的选择,发一张“下一张”牌,看看是高还是低。如果用户猜对了,你打电话给correct()correct() 打印一些东西,然后发“下一张”牌。现在correct() 返回,你回到'main' 中的循环顶部。获取用户的选择并发一张“下一张”牌,因此,猜对后发了两张牌,“当前”牌没有改变。
  • 是的,我不得不将“ Card nextCard = deck.getNextCard(); ”替换掉!它确实奏效了! (还没有测试我是否真的打印了猜出的牌,如果我得到了王牌,如果我回答错误,我应该输掉)但我想我会在一些测试后找到它。非常感谢!

标签: java eclipse object arraylist static


【解决方案1】:

在检查结果之前,您使用了两次 getNextCard。所以它会删除2张牌。如果要更新当前卡片,请在 Correct 方法中将 nextCard 更改为 currentCard。

public static void correct() {

    score++;
    System.out.println("You are right! your score is: " + score);
    System.out.println(Deck.getCards().size() + " total cards");
    currentCard = Deck.getNextCard();  
    System.out.println("your current card is " + currentCard.getName());

}

【讨论】:

    【解决方案2】:

    移除 nextCard = Deck。 ....从正确的方法。 移动

     String answer = sc.nextLine();
     Card nextCard = deck.getNextCard();
     System.out.println("your current card is " + nextCard.getName());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多