2014-12-08 71 views
3

我是一名java初学者,创建了一个基本上模仿单人5平局游戏的2级课程“VideoPoker”。在这场比赛中,一副牌被洗牌,前5张牌(现在洗牌的牌)被用来打5张牌。用户可以移除卡片发放时给予的5张卡片中的一部​​分,全部卡片或全部卡片。如果用户取出一张卡,它将被移除,并用下一张顶牌替换。我被卡在用户决定取出卡的地步。在我的程序中,当有人删除所有卡片或有时甚至只有一些卡片#2(索引1)和卡片#4(索引3)总是留在下一手(5张卡片)打印中。我的尝试是在代码图片下面从卡组底部更换卡片

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) + " "); 
     } 
    } 
} 

输出:

output

+1

就像一个审美笔记,你可以考虑用'T'替换'10',这样所有的等级都是一个字符。 – AJMansfield 2014-12-08 02:18:02

+0

@AJMansfield谢谢,会做。 – mshades 2014-12-08 02:20:15

+0

尝试移除卡1几次,或在每张卡被移除后显示手。这将对发生的事情给出一个公平的想法。 – BevynQ 2014-12-08 02:40:58

回答

2

你的问题在这里:

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处的牌(在你的例子中它是Hearts的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); 
} 
+0

即时消息同意你的第一个解释,但在'解决:'如果他删除所有?看看打印屏幕。他已经移除了所有的牌并仍然获得相同的牌。 – Secondo 2014-12-08 02:46:01

+0

你是对的,我的解决方案是不正确的 - 现在编辑 – bwegs 2014-12-08 02:47:40

+0

他正在从他的套牌中移除一张卡,而不是玩家卡。希望这会有助于改善你的回答:) – Secondo 2014-12-08 02:49:40

1

的问题归结为您实现甲板逻辑的方式而不是使用ArrayList的存储您的卡。然后通过它进行索引,您应该使用ArrayDeque(发音为“数组卡”)来处理存储和删除逻辑。

你应该跟甲板上的玩家手中的内容分开;主要的挑战在于你将甲板和玩家的手联系在一起。通过使程序更通用,可以消除很多复杂性。

在使程序更一般化的同时,您还应该使用单独的类来处理卡片。不需要很花哨,只有两个公共final字段,一个构造函数和一个toString方法。