질문 (짧은 버전) : 어떻게 ArrayList의 요소를 서로 비교합니까?ArrayList 포커 게임에서의 비교 Java
저는 ArrayList의 기본 사항을 거의 잘 알고 있습니다 (추가, 가져 오기, 설정, 크기 ...). 나는 최고의 포커 핸드를 결정하기 위해 오브젝트를 비교하기 위해 ArrayList로 들어가는 데 어려움을 겪고있다. 카드에 관한 정보를 저장하는 수업이 있습니다.
카드 클래스 :
/** class Card : for creating playing card objects
* it is an immutable class.
* Rank - valid values are 1 to 13
* Suit - valid values are 0 to 3
* Do not modify this class!
*/
class Card {
/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
/* Data field of a card: rank and suit */
private int cardRank; /* values: 1-13 (see Rank[] above) */
private int cardSuit; /* values: 0-3 (see Suit[] above) */
/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws PlayingCardException {
if ((rank < 1) || (rank > 13))
throw new PlayingCardException("Invalid rank:"+rank);
else
cardRank = rank;
if ((suit < 0) || (suit > 3))
throw new PlayingCardException("Invalid suit:"+suit);
else
cardSuit = suit;
}
/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }
/* Few quick tests here */
public static void main(String args[])
{
try {
Card c1 = new Card(1,3); // A Spades
System.out.println(c1);
c1 = new Card(10,0); // 10 Clubs
System.out.println(c1);
//c1 = new Card(10,5); // generate exception here
}
catch (PlayingCardException e)
{
System.out.println("PlayingCardException: "+e.getMessage());
}
}
}
그리고 클래스 (이것은 내가 문제를 알아내는 데있어 클래스) 카드의 각 손을 확인합니다. 나는 현재이 코드를 추가하여 ArrayList를 추가하고 각각의 손을 다시 인쇄합니다. (필자의 능력에 너무 익숙하지 않았기 때문에 별도의 ArrayList를 만들 수 있는지 확인하기 위해) 그러나 비교할 방법을 알 수는 없습니다 각 카드의 요소 (랭크와 슈트).
확인 손 클래스 :
/** Check current currentHand using multipliers and goodHandTypes arrays
* Must print yourHandType (default is "Sorry, you lost") at the end o function.
* This can be checked by testCheckHands() and main() method.
*/
private void checkHands()
{
// implement this method!
ArrayList<Card> multiplierCheck = new ArrayList<Card>();
String yourhandtype = "Sorry, you lost";
for (int toList = 0; toList<5; toList++) {
multiplierCheck.add(currentHand.get(toList));
}
System.out.println(multiplierCheck);
System.out.println(yourhandtype);
}
그리고 손 (스트레이트, 플러시, 종류 세)를 이기고 손을 만들어 손을 확인 테스트하는 방법에 관한 것이다. Check Hands Class에서 카드를 서로 비교하는 방법을 알 수 없습니다.
testCheckHands() 메소드 당신이 작동하지 않습니다하지만, ArrayList를 반복하는 방법을 말했다 경우 ..
public void testCheckHands()
{
try {
currentHand = new ArrayList<Card>();
// set Royal Flush
currentHand.add(new Card(1,3));
currentHand.add(new Card(10,3));
currentHand.add(new Card(12,3));
currentHand.add(new Card(11,3));
currentHand.add(new Card(13,3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight Flush
currentHand.set(0,new Card(9,3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight
currentHand.set(4, new Card(8,1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Flush
currentHand.set(4, new Card(5,3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush ",
// "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };
// set Four of a Kind
currentHand.clear();
currentHand.add(new Card(8,3));
currentHand.add(new Card(8,0));
currentHand.add(new Card(12,3));
currentHand.add(new Card(8,1));
currentHand.add(new Card(8,2));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Three of a Kind
currentHand.set(4, new Card(11,3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Full House
currentHand.set(2, new Card(11,1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Two Pairs
currentHand.set(1, new Card(9,1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// set Royal Pair
currentHand.set(0, new Card(3,1));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
// non Royal Pair
currentHand.set(2, new Card(3,3));
System.out.println(currentHand);
checkHands();
System.out.println("-----------------------------------");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
'checkHnads'는 정확히 무엇을하고 어떻게 작동하지 않습니까? –
@peeskillet 아직 아무 것도하지 않는다고 생각합니다. 나는 그것이 OP가 요구하고있는 것이라고 생각한다. Mikael 정확히 무슨 문제입니까? 내가 가지고있는 것은 각 플레이어의 손에 대한 ArrayList입니다. 따라서 카드를 가져 와서 포커 규칙과 비교하십시오. 질문은 실제로 ArrayList 또는 포커의 카드 값을 비교해야합니까? – Radiodef
@Radiodef 난 arraylist 안에있는 카드 안의 정보에 접근하는 방법을 모르겠습니다. 나는 [arraylist name] .get (0) .getRank()를 사용하여 슈트에 접근하는 방법을 알아 냈습니다.하지만 스트레이트 (순위에서)를 찾는 데 어려움을 겪었습니다. 또 다른 로얄 플러시> 종류의 3> 플러시> 스트레이트 (또는 순서가 실제로 무엇이든). – MikaelCMiller