我正在编写代码来尝试创建一个玩AI的扑克。我已经提供了.jar文件,其中包含可以处理游戏的类,只要我让AI代码正常工作。
话虽如此,我遇到了以下相当恼人的错误。为清楚起见,以下代码块包含错误,在PokerGame.class
中调用我的播放器的代码部分,PokerPlayer
接口以及扩展PokerPlayer
接口的类。
这是一个例外:
Exception in thread "main" java.lang.RuntimeException: Class blake.MyPokerPlayer must have a default constructor
at pokergame.PokerGame.loadPlayer(PokerGame.java:84)
at pokergame.PokerGame.<init>(PokerGame.java:100)
at pokergame.PokerGame.main(PokerGame.java:590)
at blake.MyPokerPlayer.main(MyPokerPlayer.java:18)
Caused by: java.lang.NoSuchMethodException: blake.MyPokerPlayer.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getConstructor(Class.java:1825)
at pokergame.PokerGame.loadPlayer(PokerGame.java:78)
... 3 more
这是代码:
package pokergame;
import java.util.*;
import java.lang.reflect.*;
public class PokerGame implements PokerListener {
// Other methods above
private PokerPlayer loadPlayer(String classname) {
PokerPlayer ret = null;
try {
Class<?> cls = Class.forName(classname);
Constructor<?> ctor = cls.getConstructor();
Object o = ctor.newInstance();
ret = (PokerPlayer)o;
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot find player class: " + classname, e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Class " + classname + " must have a default constructor", e);
} catch (ClassCastException e) {
throw new RuntimeException("Player class does not implement PokerPlayer interface: " + classname, e);
} catch (Exception e) {
throw new RuntimeException("Error trying to instantiate player class: " + classname, e);
}
return ret;
}
// Other methods below
}
package pokergame;
public interface PokerPlayer extends PokerListener {
int numChips();
void collectChips(int numChips);
void acceptChips(int numChips);
void setId(String id);
PokerDecision decide(PokerGameDetails game, int betRequiredToCall);
PokerHand bestHand();
}
package player;
import java.util.*;
import pokergame.*;
public class MyPokerPlayer implements PokerPlayer {
private String id;
private ArrayList<PokerCard> handCards = new ArrayList<>();
private ArrayList<PokerCard> tableCards = new ArrayList<>();
private ArrayList<PokerCard> deck = new ArrayList<>();
private int numChips;
public static void main(String[] args) {
PokerGame.main(new String[]{"pokerstooges.Larry", "player.MyPokerPlayer"});
}
MyPokerPlayer() { }
// ***************
// THESE NEED WORK
// ***************
public void onEvent(PokerEvent e) {
if (e instanceof PokerNewHandEvent) { System.out.println(e); }
else if (e instanceof PokerPlayerEliminatedEvent) { System.out.println(e); }
else if (e instanceof PokerPotDistributionEvent) { System.out.println(e); }
else if (e instanceof PokerCollectingBlindEvent) { System.out.println(e); }
else if (e instanceof PokerNewCardEvent) { System.out.println(e); }
else if (e instanceof PokerDecisionEvent) { System.out.println(e); }
else if (e instanceof PokerHandRevealEvent) { System.out.println(e); }
}
public PokerDecision decide(PokerGameDetails game, int betRequiredToCall) {
PokerDecision decision = new PokerDecision(PokerDecision.TYPE.FOLD);
return decision;
}
public String toString() {
// Something here...
return "";
}
// *****************
// THESE SHOULD WORK
// *****************
public int numChips() { return this.numChips; }
public void collectChips(int numChips) { this.numChips -= numChips; }
public void acceptChips(int numChips) { this.numChips += numChips; }
public void setId(String id) { this.id = id; }
public String getId() { return this.id; }
public PokerHand bestHand() {
ArrayList<String> hands = new ArrayList<>();
String handCardString = handCards.get(0) + " " + handCards.get(1);
if (tableCards.size() == 0) {
hands.add(handCardString);
}
else if (tableCards.size() == 3) {
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(2));
}
else if (tableCards.size() > 3) {
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(2));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(3));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(2) + " " + tableCards.get(3));
hands.add(handCardString + " " + tableCards.get(1) + " " + tableCards.get(2) + " " + tableCards.get(3));
if (tableCards.size() == 5) {
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(1) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(2) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(0) + " " + tableCards.get(3) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(1) + " " + tableCards.get(2) + " " + tableCards.get(4));
hands.add(handCardString + " " + tableCards.get(2) + " " + tableCards.get(3) + " " + tableCards.get(4));
}
}
PokerHand bestHand = compareHands(hands);
return bestHand;
}
public PokerHand compareHands(ArrayList<String> hands) {
PokerHand handA = new PokerHand(hands.remove(0));
PokerHand handB;
if (hands.size() == 1) { return new PokerHand(hands.remove(0)); }
else if (hands.size() > 2) { handB = compareHands(hands); }
else { handB = new PokerHand(hands.remove(0)); }
if (handA.compareTo(handB) > 0) { return handA; }
else { return handB; }
}
private void addCards(List<String> cardStrings) {
for (String cardString : cardStrings) { deck.add(new PokerCard(cardString)); }
}
}
如MyPokerPlayer
所示,有一个默认构造函数:
MyPokerPlayer() { }
我尝试在其前面添加public
,以防万一,但这也无济于事。
答案 0 :(得分:0)
异常(Class blake.MyPokerPlayer must have a default constructor
)中的类与代码段中的类(player.MyPokerPlayer
)之间似乎不匹配
你能检查你传递给loadPlayer(String)的类名吗?
此外,构造函数MyPokerPlayer是包作用域的,这可能导致运行时反射调用失败(请将构造函数设置为public)。 这通常是一个问题,因为pokergame包不是定义类的那个。