Java纸牌游戏:我的纸牌游戏从卡组中删除2张牌,而不是1张

问题描述 投票:-1回答:2

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

规则:猜测下一张更高或更低的纸牌。如果您猜对了就得分,如果您猜对了,游戏就结束了。

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

有人可以看到出什么问题了。

奖金问题:我游戏中的A是最高价值的卡。但是我以某种方式设法输掉了,而在获得一张王牌时猜得更低(也许这与我相信我拥有张王牌是因为我的成绩)有关,如果有人可以帮忙,我会非常感激。

打包CardGame;

import 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");

}

}

打包CardGame;

import 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;
}

}

打包CardGame;

公共类卡{

// 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;
    }
}

}

运行此代码,以便您也可以播放它。规则:猜下一张是更高或更低的牌。如果您猜对了,得到一个分数,如果您猜对了,游戏结束。问题是“ Deck.getCards()....

java eclipse object arraylist static
2个回答
0
投票

您正在两次使用getNextCard,然后再检查结果。因此它将删除2张卡。如果要更新当前卡,请以“正确”方法将nextCard更改为currentCard。


0
投票

删除nextCard =牌组。 ....从正确的方法。移动

© www.soinside.com 2019 - 2024. All rights reserved.