gpt4 book ai didi

JAVA从数组转为列表并使用shuffle方法

转载 作者:行者123 更新时间:2023-12-02 05:34:46 27 4
gpt4 key购买 nike

我的任务是创建一副牌并随机发五张牌。我终于让它“工作”了,但我需要一些帮助。我刚刚读到,java 现在有一个 shuffle 方法来对列表进行洗牌。我们现在正在使用数组,所以我认为需要数组,有没有办法将 shuffle 方法与数组一起使用?如果没有,有人可以指导我切换到列表吗?我认为这将有助于回答我的下一个问题。我唯一要做的就是以某种方式说明在随机发出 5 张牌后,牌组中还剩下多少张牌。但是,鉴于我如何使用数组,并且您无法从数组中删除项目,因为它是固定的(对吗?)我想看看是否有一种方法可以模拟从数组中删除卡片,以便它们不会被处理再次?或者将数组转换为列表/集合会更容易吗?

这是我的代码。任何帮助是极大的赞赏。另外,我也会接受有关清理代码的任何建议......

public class CardGame {
public static void main (String [] args) {

DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle(1000);

Card b;
for (int i = 0; i < 5; i++) {
b = deck.deal();
System.out.println("Deal a card: " + b);
}
}
}


class Card {

public static final int SPADE = 4;
public static final int HEART = 3;
public static final int CLUB = 2;
public static final int DIAMOND = 1;

private static final String[] Suit = {"*", "Hearts", "Clubs", "Spades", "Diamonds"};
private static final String[] Rank = {"*", "*", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

private int cardSuit;
private int cardRank;

public Card( int suit, int rank ) {
if ( rank == 1 )
cardRank = 14; // Give Ace the rank 14
else
cardRank = (int) rank;

cardSuit = (int) suit;
}

public int suit() {
return ( this.cardSuit );
}

public String suitStr() {
return( this.Suit[ this.cardSuit ] );
}

public int rank() {
return ( this.cardRank );
}

public String rankStr() {
return ( Rank[ this.cardRank ] );
}

public String toString() {
return ( Rank[ this.cardRank ] + " of "+ Suit[ this.cardSuit ] );
}
}

class DeckOfCards {
public static final int NEWCARDS = 52;
private Card[] deckOfCards; // Contains all 52 cards
private int currentCard; // deal THIS card in deck

public DeckOfCards( ) {
deckOfCards = new Card[ NEWCARDS ];
int i = 0;

for (int suit = Card.DIAMOND; suit <= Card.SPADE; suit++)
for ( int rank = 1; rank <= 13; rank++ )
deckOfCards[i++] = new Card(suit, rank);
currentCard = 0;
}
//shuffle(n): shuffle the deck
public void shuffle(int n) {
int i, j, k;

for ( k = 0; k < n; k++ ){
i = (int) ( NEWCARDS * Math.random() ); // Pick 2 random cards
j = (int) ( NEWCARDS * Math.random() ); // in the deck

//swap these randomly picked cards
Card temp = deckOfCards[i];
deckOfCards[i] = deckOfCards[j];
deckOfCards[j] = temp;
}
currentCard = 0; // Reset current card to deal
}

//deal(): deal deckOfCards[currentCard] out
public Card deal() {

if (currentCard < NEWCARDS) {
return ( deckOfCards[currentCard++] );
}
else{
System.out.println("Out of cards error");
return ( null ); // Error;
}
}

public String toString() {
String s = "";
int k;
k = 0;

for ( int i = 0; i < 4; i++ ) {
for ( int j = 1; j <= 13; j++ )
s += (deckOfCards[k++] + " ");
s += "\n";
}
return (s);
}
}

在获得所有帮助后,我决定重写我的代码,这就是我得到的,但我在将它们组合在一起并使其工作时遇到了一些麻烦!

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CardGame {
public static void main (String [] args) {

DeckOfCards deck = new DeckOfCards();
//call shuffle
deck.shuffle();

Card b;
for (int i = 0; i < 5; i++) {
b = deck.deal();
System.out.println("Deal a card: " + b);
}
}
}



class Card {

enum Suit {
HEARTS(1),
CLUBS(2),
DIAMONDS(3),
SPADES(4);

private int suitValue;

private Suit (int suitValue)
{
this.suitValue = suitValue;
}

public int getSuitValue()
{
return suitValue;
}
}

private Suit suit;
private Value value;

public Card (Suit suit, Value value)
{
this.suit = suit;
this.value = value;
}

public Suit getSuit() {
return suit;
}
public Value getValue() {
return value;
}

/*
public int compareTo(Card o) {

return 0;
}
} */

class DeckOfCards
{
private List<Card> cards = new ArrayList<Card>();

public DeckOfCards () {

for (Suit suit : Suit.values()) {
for (Value value : Value.values()) {
Card card = new Card(suit, value);
cards.add(card);
}
}
}

public List<Card> getCards() {
return cards;
}

public void shuffleDeckOfCards() {
Collections.shuffle(cards);
}


public String toString() {
return this.Value + " of "+ this.Suit;
}
}

最佳答案

这里是一个完整工作的示例程序,它演示了您需要的所有内容 - 从数组中洗牌和删除牌。

示例卡类别:

public class Card {
private String value = "";

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Card(String value) {
super();
this.value = value;
}

}

示例程序:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class CardGame {

public static void main(String[] args) {

List<Card> pack = new ArrayList<Card>();
Card c1 = new Card("c1");
Card c2 = new Card("c2");
Card c3 = new Card("c3");
pack.add(c1);
pack.add(c2);
pack.add(c3);

System.out.print("List : ");
CardGame.displayCardList(pack);

Card[] packArr = cardListToArray(pack);

System.out.print("Array : ");
CardGame.displayCardArray(packArr);

// http://stackoverflow.com/questions/4228975/how-to-randomize-arraylist
long seed = System.nanoTime();
Collections.shuffle(pack, new Random(seed));

System.out.print("Shuffle List : ");
CardGame.displayCardList(pack);

packArr = cardListToArray(pack);

System.out.print("Shuffle Array : ");
CardGame.displayCardArray(packArr);

System.out.print("Remove Card from Array : ");
CardGame.removeCardFromArray(packArr, new Card("c1"));
CardGame.displayCardArray(packArr);

}

public static boolean removeCardFromArray(Card[] packArr, Card card) {

boolean cardFound = false;

for (int i = 0; i < packArr.length; i++) {

if (packArr[i].getValue().equalsIgnoreCase(card.getValue())) {
packArr[i] = null;
cardFound = true;
break;
}

}

return cardFound;

}

public static Card[] cardListToArray(List<Card> pack) {

Card[] packArr = new Card[pack.size()];

for (int i = 0; i < pack.size(); i++) {
packArr[i] = pack.get(i);
}

return packArr;

}

public static void displayCardList(List<Card> pack) {

for (Card c : pack) {
System.out.print(c.getValue() + ", ");
}
System.out.println();
}

public static void displayCardArray(Card[] packArr) {

for (Card c : packArr) {

if (c == null) {
System.out.print("REMOVED, ");
} else {
System.out.print(c.getValue() + ", ");

}

}
System.out.println();
}

}

关于JAVA从数组转为列表并使用shuffle方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25100724/

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