编辑:我已经部分解决了这个问题。我之前已经从它自己的方法中删除了for循环并将其放到main中以使其正常工作,但是由于某种原因,这次它不起作用。那是因为正在初始化一个新的Cards对象(在// The Flop注释下面),我想重置了卡片组。如果我删除它并将循环放在那里而不是它自己的方法,它将起作用。但是,问题仍然存在于方法中,因为我需要创建一个新的对象来与Deck类进行“通信”。我将尝试将对象作为参数传递或将其设置为可访问的位置,看看是否可行,然后我将重新编辑该帖子。
编辑2:将Cards对象作为参数传递已成功!将作为答案发布。
发布开始: 我正在尝试为我的课堂编写德州扑克游戏。我正在尝试使用方法来整理main()。不幸的是,它缝到了纸牌在我拥有的其他阵列(玩家的手,对手的手和棋盘)之间重复。我似乎不在我老师的最爱名单上,因此当他被问到时他没有提供太多帮助。
这是主要班级:
public class PokerGame
{
private int pot = 0;
public static void main(String[] args)
{
//Create Objects to connect classes.
Deck Cards = new Deck(0,"s");
Player Player = new Player();
Opponents Op1 = new Opponents();
//Bring deck into this main class.
Deck[] aDeck = new Deck[52];
Deck[] fDeck = Cards.buildDeck(aDeck);
Deck[] mDeck = Cards.shuffle(fDeck,100);
//Create the Board and Hand arrays.
Deck[] Board = new Deck[5];
Deck[] Burned = new Deck[3];
Deck[] pHand = new Deck[2];
Deck[] o1Hand = new Deck[2];
int count;
for(count = 0; count < pHand.length; count++)
{
pHand[count] = Cards.deal(mDeck);
System.out.println("P " + pHand[count].namedRank + " of " + pHand[count].s);
o1Hand[count] = Cards.deal(mDeck);
}
System.out.println("o1 " + o1Hand[0].namedRank + " of " + o1Hand[0].s);
System.out.println("o1 " + o1Hand[1].namedRank + " of " + o1Hand[1].s);
//The Flop
Cards = new Deck(0,"s");
Burned[0] = Cards.deal(mDeck);
Board = flop(Board,mDeck);
//The Turn
Burned[1] = Cards.deal(mDeck);
Board[3] = Cards.deal(mDeck);
System.out.println("\n\n");
System.out.println("Now the Board is...");
for(int b = 0; b < 4; b++)
{
System.out.println(Board[b].namedRank + " of " + Board[b].s);
}
showdown(Board,pHand,o1Hand);
}
public static Deck[] flop(Deck[] Board,Deck[] mDeck)
{
Deck Cards = new Deck(0,"s");
System.out.println();
System.out.println("The Board is...");
for(int b = 0; b < 3; b++)
{
Board[b] = Cards.deal(mDeck);
System.out.println(Board[b].namedRank + " of " + Board[b].s);
}
return Board;
}
这是我洗牌交易的Deck课。
public class Deck
{
public Deck[] Deck = new Deck[52];
private int index = 0;
public int rank;
public String s;
public String namedRank;
private int x = 0;
/**
* Constructor for card Objects
*
* Cannot get to work,not sure where issue is.
*/
public Deck(int rank,String s)
{
if(rank == 1)
{
this.rank = 14;
}
else
{
this.rank = rank;
}
String numRank = String.valueOf(rank);
if(this.rank == 11)
{
this.namedRank = "Jack";
}
else if(this.rank == 12)
{
this.namedRank = "Queen";
}
else if(this.rank == 13)
{
this.namedRank = "King";
}
else if(this.rank == 14)
{
this.namedRank = "Ace";
}
else
{
this.namedRank = numRank;
}
this.s = s;
}
/**
* Assigns each element in the array a card
*
* @param Deck array before elements assigned
* @return Deck array after elemnts assigned
*/
public Deck[] buildDeck(Deck[] fDeck)
{
String[] suit = {"Club","Diamond","Heart","Spade"};
int c;
String of = " of ";
for(c = 0; c < suit.length; c++)
{
for(rank = 1; rank < 14; rank++)
{
s = suit[c];
fDeck[x] = new Deck(rank,s);
x++;
}
}
return fDeck;
}
/**
* Shuffles the cards by picking to random numbers and switching them
*
* @param Deck array after elemetns assigned
* @return Deck array after elements are mixed up
*/
public Deck[] shuffle(Deck[] mDeck,int shuffle)
{
for(int count = 0; count < shuffle; count++)
{
int f = ((int)(Math.random() * 100) % 51 + 1);
int s = 0;
Deck move = mDeck[f];
mDeck[f] = mDeck[s];
mDeck[s] = move;
}
return mDeck;
}
/**
* Deals the cards and keeps track by incrasing the index
*
* @param Deck array after elements are mixed up
* @return single Deck array element at whatever position int index is at
*/
public Deck deal(Deck[] dDeck)
{
index++;
return dDeck[index];
}
}
从任何意义上说,任何帮助都是值得赞赏的,因为我已经为此努力了很长时间。在继续工作之前,我将继续做我会遇到的麻烦。