gpt4 book ai didi

java 扑克牌数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:24 25 4
gpt4 key购买 nike

我对我正在写的这个程序感到疯狂。我的套牌似乎不起作用,我已经寻找了每一个信息,但连续 8 小时后它仍然不起作用 =( 请帮助指出我需要在哪里做得更好,或者如何更好地编码。

package poker;

public class Deck {

private Card[] cards;

// deck constructor with initial array
public Deck() {
Card[] x= new Card[52];
int index = 0;
for (int suit = 0; suit < 3; suit++) {
for (int value = 1; value < 13; value++) {
cards[index] = new Card(value, suit);
index++;

}

}
}

// copy constructor with a shallow copy of the array
public Deck(Deck other) {
Card[] c = new Card[52];

int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int value = 1; value <= 13; value++) {
cards[index] = new Card(suit, value);
index++;
}

}
}

// method for cards in any position
public Card getCardAt(int position) {
if (position >= cards.length) {
throw new IndexOutOfBoundsException("Values are out of bounds");
} else {
return cards[position];
}
}

// number of cards left after each draw
public int getNumCards() {

return cards.length;
}

// Randomized rearrangement of cards





//have no idea to go about this any further
public void shuffle() {
int temp=0;
for (int row=0;row<cards.length;row++){
int random = (int)(Math.random()*((cards.length-row)+1));
Deck.this.cards[temp]= this.getCardAt(row);
cards[row]=cards[random];
cards[random]=cards[temp];
}
}
//cutting of the cards
public void cut(int position) {
//int temp = this.cards
}

// something to think about on dealing from taking the differences in
// dealing the cards
public Card[] deal(int numCards) {
/* numCards = 5;
for (int i = 0; i < numCards; i++) {

numCards = cards.length - numCards;
}
return deal(numCards);*/
{
numCards = this.getNumCards();
numCards ++;

return deal(5);
}

}

}

我尝试过 Junit 测试,但我似乎很糟糕,但我尝试过

package poker;

import junit.framework.TestCase;

public class StudentTests extends TestCase {
public void testDeck() {
int value=0;
int suit = 0;
Card[] x = new Card[52];
//Card = new Card(getValue(), getSuit());

assertTrue(getValue() == 1 && getSuit() == value);
assertTrue(getValue() == 1 && getSuit() == suit);
;
}
public void testCopyConstructor(){

int value = 0;
int suit = 0;
Card[] sc = new Card[52];
//Card = new Card(getValue(), getSuit());

assertTrue(getValue() == 1 && getSuit() == 0);

}
/* public void testShuffle()
{
Card[] sc = new Card[52];
Card[] sc2 = new Card[52];
assertTrue(sc==(sc2));
//shuffle method
assertFalse(sc==(sc2));

//another shuffle method here
//i have no idea

assertFalse(sc==(sc2));} */

private int getValue() {
// TODO Auto-generated method stub
return 1;
}

private int getSuit() {
// TODO Auto-generated method stub
return 0;
}
}

最佳答案

您的 Deck 的默认构造函数会生成一副 36 张牌,而不是 52 张。首先解决这个问题。

关于java 扑克牌数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5668283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com