Java 中的纸牌游戏:创建“纸牌手”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20926835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Card Game in Java: Creating a "hand of cards"
提问by T.Woody
I would like to first say that this is not homework, but a pet project that I am working on in Java to better understand a complete Java program, and will hopefully be my first attempt at making my own GUI.
我想首先说这不是家庭作业,而是我正在用 Java 进行的一个宠物项目,以更好地理解一个完整的 Java 程序,希望这将是我第一次尝试制作自己的 GUI。
That aside, I have a question about creating a hand of cards from my current code. As it stands, I have a main class called DummyRummy
, a class for creating cards, a class that creates a standard deck of cards (which includes Jokers and "shuffles" the deck when a new deck is created), and a PlayerHands
class. I am strictly working with ArrayList
s for all of the code, and the PlayersHands
class should return two ArrayList
s that will be used. However, when I try to call the PlayerHands()
method in the public static void main()
, PlayerHands
cannot be located for some reason... Here is the code for PlayerHands
:
除此之外,我有一个关于从我当前的代码创建一手牌的问题。就目前而言,我有一个名为 的主类DummyRummy
,一个用于创建卡片的类,一个创建标准卡片组的类(其中包括 Jokers 并在创建新卡片组时“洗牌”卡片组)和一个PlayerHands
类。我严格使用ArrayList
s 来处理所有代码,并且PlayersHands
该类应该返回两个ArrayList
将使用的 s。但是,当我尝试调用 中的PlayerHands()
方法时public static void main()
,PlayerHands
由于某种原因无法定位...这是 的代码PlayerHands
:
package dummyrummy;
public class PlayerHands {
private Card[] playerOneCards;
private Card[] playerTwoCards;
private int[] value;
PlayerHands(deck d, int round)
{
value = new int[round+3];
playerOneCards = new Card[round+2];
playerTwoCards = new Card[round+2];
//(round+2) is the handsize at any given time
for (int x=0; x<round+3; x++)
{
playerOneCards[x] = d.drawFromDeck(); //fills up one hand.
playerTwoCards[x] = d.drawFromDeck(); //fills up second hand.
}
}
}
Here is the DummyRummy
class.
这是DummyRummy
课堂。
package dummyrummy;
import java.util.*;
public class DummyRummy {
public static void main(String[] args) {
deck testDeck;
testDeck = new deck();
System.out.println(testDeck.getTotalCards());
System.out.println(testDeck.getClass());
int round = 1;
PlayerHands(testDeck, round); //This is where the error is occurring
}//End of arguments
}//End of DummyRummy class
Here is the Card
class:
这是Card
课程:
package dummyrummy;
public class Card
{
private short rank, suit;
private static String[] suits = { "Hearts", "Spades", "Diamonds", "Clubs", "Joker" };
private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Hyman", "Queen", "King" };
private static String[] jokers = {"Joker", "Joker"};
private static String[] ranks2 = {"0", "0"};
public static String rankAsString( int __rank ) {
if (__rank != 0){
return ranks[__rank];
}//End of if statement
return ranks2[__rank];
}//End of rankAsString class
Card(short suit, short rank)
{
this.rank=rank;
this.suit=suit;
}//End of Card Initializer
public @Override String toString()
{
if(suit == 5){
return "Joker";
}//End of if statement that calls jokers
if(rank == 0){
return "Joker";
}
return ranks[rank] + " of " + suits[suit];
}//End of toString method
public short getRank() {
return rank;
}//End of getRank method
public short getSuit() {
return suit;
}//End of getSuit method
}//End of Card
And finally, here is the deck()
class:
最后,这是deck()
课程:
package dummyrummy;
import java.util.Random;
import java.util.ArrayList;
public class deck {
private ArrayList<Card> cards;
deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;
short jokerSuit=5;
short jokerRank = 0;
cards.add(new Card(jokerSuit, jokerRank));
cards.add(new Card(jokerSuit,jokerRank));
for (short suit=0; suit<=3; suit++)
{
for (short rank=0; rank<=12; rank++)
{
cards.add(new Card(suit,rank));
}
}//End of for-loop
int deckSize = 54;
for (int i=0; i<1000; i++)
{
index_1 = generator.nextInt( cards.size() );
index_2 = generator.nextInt( cards.size() );
temp = cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}//End of for-loop
}//End of deck()
public Card drawFromDeck()
{
/*
* This method removes the top card of the already shuffled deck.
* The next step to take with this class is put the drawn card into another
* array that represents a players hand.
* This will take two arrays, and must be called depending on what player 'drawsFromDeck'.
*/
return cards.remove( 0 );
}//End of drawFromDeck()
public int getTotalCards()
{
return cards.size();
}//End of getTotalCards()
}//End of class deck
Thank you for your time, and I appreciate any help that may come. I would also be more than happy to provide my other code, if necessary.
感谢您抽出宝贵时间,我感谢您提供的任何帮助。如有必要,我也很乐意提供我的其他代码。
EDIT:I have added the class and package above.
编辑:我已经添加了上面的类和包。
采纳答案by fabian
PlayerHands(deck, int)
is a constructor. Therefore you have to call it like this (in class DummyRummy
):
PlayerHands(deck, int)
是一个构造函数。因此你必须这样称呼它(在课堂上DummyRummy
):
new PlayerHands(testDeck, round);
And since i guess you want to work with the instance you created, you should save the reference to a variable:
因为我猜你想使用你创建的实例,你应该保存对变量的引用:
PlayerHands playerHands = new PlayerHands(testDeck, round);
回答by The Guy with The Hat
PlayerHands(deck d, int round)
is not a method, it is a constructor.
PlayerHands(deck d, int round)
不是一个方法,它是一个构造函数。
To correctly get the two hands, you can use this code:
要正确获得两只手,您可以使用以下代码:
...
int round = 1;
PlayerHands playerHands = new PlayerHands(testDeck, round); //This creates a new instance of the PlayerHands class
//access the players' hands like this:
Card[] player1Hand = playerHands.playerOneCards; //sets "player1Hand" to the "Card[] playerOneCards" contained in "playerHands"
Card[] player2Hand = playerHands.playertwoCards; //sets "player2Hand" to the "Card[] playerTwoCards" contained in "playerHands"
}//End of arguments
}//End of DummyRummy class