编程语言
首页 > 编程语言> > Java-从卡座底部更换卡

Java-从卡座底部更换卡

作者:互联网

我是Java的初学者,并且创建了一个2类程序“ VideoPoker”,该程序基本上模仿一个5人平局游戏.在此游戏中,一副纸牌被洗牌,并且(现在已洗牌的副牌)前5张牌被用来打5张平局.发卡时,用户可以删除他/她获得的5张卡中的一些,全部或全部,也可以不删除.如果用户删除了一张卡,它将被删除并替换为下一张最上面的卡.我陷入了用户决定删除卡的问题.在我的程序中,当有人取出所有卡片或有时甚至只取出一些卡片时,第二张卡片(索引1)和第四卡片(索引3)总是留在下一手(5张卡片)中打印.我的尝试是在下面的代码PICTURE中:

Deck.java:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class Deck {
    // Constructing a deck from two arrays
    String[] suit = { "C", "D", "H", "S" };
    String[] rank = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
    ArrayList<String> deck = new ArrayList<String>(suit.length * rank.length);
    int currentCard = 0;

    // Storing public variables
    int suits = suit.length;
    int ranks = rank.length;
    int deckSize = deck.size();

    // Constructs a deck with 52 cards
    public Deck(){
        for(int i = 0; i < suits; i ++){
            for(int j = 0; j < ranks; j++){
                String temp = rank[j] + suit[i];
                deck.add(temp);
            }
        }
    }

    public String toString(){
        return Arrays.deepToString(deck.toArray());    
    }

    // Fisher-Yates Shuffle 
    public ArrayList<String> shuffle(){
        Collections.shuffle(deck);
        return deck;
    }

    public String deal(){
        return deck.get(currentCard++);
    }

    public String remove(int i){
        return deck.remove(i);
    }

    public String get(int i){
        return deck.get(i);
    }

    public ArrayList<String> getList(){
        return deck;
    }
}

Dealer.java(测试程序):

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Dealer {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Deck testDeck = new Deck();

        System.out.println(testDeck);

        testDeck.shuffle();

        System.out.println(testDeck);

        for (int i = 0; i < 5; i ++){
            System.out.print(testDeck.deal() + " "); 
        }

        String choice;

        for (int i = 0; i < 5; i++){
            System.out.print("\nWould you like to remove card " + (i + 1) + "? ");
            choice = in.next();
            if (choice.equals("Y")){
                testDeck.remove(i);
            }
        }

        for (int i = 0; i < 5; i++){
           System.out.print(testDeck.get(i) + " ");  
        }
    }
}

输出:

解决方法:

您的问题在这里:

for (int i = 0; i < 5; i++){
    System.out.print("\nWould you like to remove card " + (i + 1) + "? ");
    choice = in.next();
    if (choice.equals("Y")){
        testDeck.remove(i); // <-- removing the 'i'th element is the problem
    }
}

在此块中,您要询问用户是否要在特定位置移除卡,但是请考虑这一点.如果用户说“是!我要移除第一张卡!”,则会在索引处移除最上面的卡0,这很好.但这也是问题出现的地方,因为现在您有了一个全新的平台!以前位于索引1(在您的示例中为“心”的10)的卡现在实际上位于索引0.因此,实际上,您将牌组视作永不改变,而实际上它有可能动态变化并您没有在代码中说明这一点.

可能的(尽管很粗糙)解决方案:

// a list used to store the index of cards to be removed    
ArrayList<Integer> indexesToRemove = new ArrayList();

for (int i = 0; i < 5; i++) {
    System.out.print("\nWould you like to remove card " + (i + 1) + "? ");
    choice = in.next();
    if (choice.equals("Y")) {
        indexesToRemove.add(i); // record index of card to be removed
    }
}

// here we remove the cards all at once    
for(int i = 0; i < indexesToRemove.size(); i++) {
    // each iteration is a guaranteed removal so
    // we subtract by i to counteract for each subsequent removal
    testDeck.remove(indexesToRemove.get(i) - i);
}

标签:collections,for-loop,arraylist,arrays,java
来源: https://codeday.me/bug/20191121/2048144.html