나는 Java 기술을 향상시키기 위해 블랙 잭 시뮬레이터를 구축하고 타격 할 때 서로 통신하도록 클래스를 가져 오는 데 문제가 있습니다.다른 클래스의 메서드를 호출하여 비 정적으로 유지
플레이어와 딜러에게 공통적 인 방법을 포함하는 휴먼 클래스를 만들고 있는데, 그 중 하나 인 히트 클래스입니다. 나는 카드 덱을 만드는 클래스 덱을 가지고 있는데이 클래스는 getCard라는 메서드를 가지고있다. Human에서는 hit 메소드가 Deck 클래스에서 getCard를 호출하기를 원합니다.
이 메서드는 Deck 클래스의 인스턴스 변수 인 데크에서 카드를 제거해야하므로 getCard를 정적으로 만들고 싶지 않습니다. Human 클래스 내에서 새로운 갑판 인스턴스를 만들고 싶지 않습니다. 인간당 한 갑판이 아니라 게임 당 하나의 갑판 만 필요하기 때문입니다.
어떻게 제대로 수행 할 수 있습니까?
//Removes a random card from the deck and deletes it from the deck.
//It removes one card per call to the function.
public Card getCard(){
Random rand = new Random();
int index = rand.nextInt(deck.size());
Card toDeal = new Card (deck.get(index).getName(),
deck.get(index).getSuit(),
deck.get(index).getValue());
deck.remove(index);
return toDeal;
}
: 지금의 IDE (넷빈즈) 여기
hand.add(Deck().getCard());
는 갑판 클래스의 getCard 방법입니다, 기호, 인간의 히트 방법이 줄 방법 덱()를 찾을 수 없습니다 "라는
:
그리고 여기, 나는 두 클래스를 포함하고 내가 뭔가를 포함하지 않았다
경우public void hit(){
hand.add(Deck().getCard());
}
인간 클래스의 히트 방법
package blackjack;
import java.util.*;
public class Deck {
private static int numSuits = 4;
private static int numRanks = 13;
private static int numCards = numSuits * numRanks;
private ArrayList<Card> deck;
public Deck() {
String suit = null;
String name = null;
int value = 0;
deck = new ArrayList<Card>();
for (int i=1; i<=numSuits; i++){
for (int j=1; j <= numRanks; j++){
switch (i){
case 1: suit = "Clubs"; break;
case 2: suit = "Diamonds"; break;
case 3: suit = "Hearts"; break;
case 4: suit = "Spades"; break;
}
switch (j){
case 1: name = "Ace"; value = 0; break;
case 2: name = "Two"; value = 2; break;
case 3: name = "Three"; value = 3; break;
case 4: name = "Four"; value =4; break;
case 5: name = "Five"; value = 5; break;
case 6: name = "Six"; value = 6; break;
case 7: name = "Seven"; value = 7; break;
case 8: name = "Eight"; value = 8; break;
case 9: name = "Nine"; value = 9; break;
case 10: name = "Ten"; value = 10; break;
case 11: name = "Jack"; value = 10; break;
case 12: name = "Queen"; value = 10; break;
case 13: name = "King"; value = 10; break;
}
Card card = new Card (name, suit, value);
deck.add(card);
}
}
}
public int getSize(){
return deck.size();
}
public void printDeck(){
for (Card card : deck){
System.out.println(card);
}
}
//Removes a random card from the deck and deletes it from the deck.
//It removes one card per call to the function.
public Card getCard(){
Random rand = new Random();
int index = rand.nextInt(deck.size());
Card toDeal = new Card (deck.get(index).getName(),
deck.get(index).getSuit(),
deck.get(index).getValue());
deck.remove(index);
return toDeal;
}
}
그리고 (불완전) 인간 클래스 :
public class Human {
private int handValue;
private String name;
private ArrayList<Card> hand;
public Human(String name){
this.handValue = 0;
this.name = name;
this.hand = null;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public int getHandValue(ArrayList<Card> hand){
for (Card card: hand){
handValue += card.getValue();
}
return handValue;
}
public void hit(){
hand.add(Deck().getCard());
}
}
그리고 마지막으로, 카드 클래스의 생성자 :
public Card(String name, String suit, int value){
this.name = name;
this.suit = suit;
this.value = value;
}
것은 ('hand.add 시도 (새로운 Deck()) .getCard());'- Deck'의 anon 인스턴스를 만들고 그것의'getCard()'메소드를 호출하십시오. 아니면,'hit()'메소드를'public void hit (갑판 데크) { hand.add (deck.getCard());로 변경할 수 있습니다. }' – ochi
덱에 대한 참조를 Human 객체로 전달할 수 없습니까? – clearlyspam23
상당한 재 작업이 필요하지만 어떤 플레이어가 어떤 카드를 가지고 있는지 추적 할 수 있습니다 (플레이어가 카드를 가지고있는 목록을 쉽게 보관할 수 있지만 데크에 다시 참조 할 필요는 없습니다). 장기적으로 더 쉬워 질 수 있을까요? –