ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-从卡座底部更换卡

2019-11-21 01:03:40  阅读:210  来源: 互联网

标签:collections for-loop arraylist arrays 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有