我的任务是创建 4 个 java 类来启动一个关于 cribbage 游戏的项目。我已经完成了其中的 3 个,最后一个只有一个 toString 错误。
这是我的游戏类的代码
public class Game implements CribbageConstants {
private Player player1, player2;
private boolean player1Deals;
private Game game;
/**
* No argument constructor - set default values for Game
*/
public Game() {
player1 = new Player("?");
player2 = new Player("?");
player1Deals = true;
}
// convenience constructor
public Game(Player player1, Player player2) {
this.player1 = player1;
this.player2 = player2;
this.player1Deals = true;
}
//mutator
public void setPlayer1(Player player1) {
this.player1 = player1;
}
public void setPlayer2(Player player2) {
this.player2 = player2;
}
public void setPlayer1Deals(boolean player1Deals) {
this.player1Deals = player1Deals;
}
public void setGame(Game game) {
this.game = game;
}
//utility methods
public Player getPlayer1() {
return player1;
}
public Player getPlayer2() {
return player2;
}
/**
* @return the player1Deals
*/
public boolean isPlayer1Deals() {
return player1Deals;
}
/**
* @return the game
*/
public Game getGame() {
return game;
}
//-----------utility methods------------
public String toString() {
return "Game between " + player1 + " and " + player2;
}
}
这是我的 testToString 代码
public void testToString() {
System.out.println("toString");
Game instance = new Game();
Player p1 = new Player("Jim");
p1.setHand(hand1);
Player p2 = new Player("George");
p2.setHand(hand2);
assertEquals("Game between ? (0) and ? (0)", instance.toString());
instance.setPlayer1(p1);
assertEquals("Game between Jim (0) and ? (0)", instance.toString());
instance.setPlayer2(p2);
assertEquals("Game between Jim (0) and George (0)", instance.toString());
}
我得到的错误是:
Failed: expected <Game between ?([0) and ? (0)]>
but was: Game between ? ([AC, AC, AC, AC, AC, AC) @0 and ? (AC, AC, AC, AC, AC, AC) @ 0]>
注意 - AC AC AC AC AC AC 是上一课程中指定的默认手。我只是对为什么 player1 和 player2 返回默认手而不是名称感到困惑
这是我的播放器类的代码
/*
* player class
*/
package model;
public class Player implements CribbageConstants {
//-----fields------
private String name;
private Hand hand;
private int position;
//---------- Constructors --------- delete constructors for 33.3%
/**
* No argument constructor - set default values for card
*/
public Player() {
name = "?";
hand = new Hand();
position = 0;
}
// convenience constructor
public Player(String name) {
this.name = name;
hand = new Hand();
}
//------mutator-----
public void setName(String name) {
this.name = name;
}
public void setHand(Hand hand) {
this.hand = hand;
}
public void setPosition(int position) {
this.position = position;
}
//-------------- Utility methods --------------
/**
* Provide a text representation of a hand.
*
* @return the hand's cards
*/
public String getName() {
return name;
}
public Hand getHand() {
return hand;
}
public int getPosition() {
return position;
}
//-----------utility methods------------
public String toString() {
return name + " (" + hand + ")" + " @ " + position;
}
}
这是我的 toString 的 Player 类代码
public void testToString() {
System.out.println("toString");
Player instance = new Player();
assertEquals("? (AC, AC, AC, AC, AC, AC) @ 0", instance.toString());
instance.setName("Alexander");
instance.setHand(hand2);
instance.setPosition(50);
assertEquals("Alexander (7H, 8C, QS, AH, 4D, 4S) @ 50", instance.toString());
}