我有这个类的测试用例,我必须填写所有方法。我已经获得了一些简单地返回已经创建的东西的东西。但我不知道如何做其他方法。有小费吗?我们正在创造一个Yahtzee游戏。
这是我必须填写方法的课程
import java.util.Vector;
import model.Player;
public class YahtzeeEngine {
public static final int MAX_ROLLS = 0;
private Vector<Player> myPlayers;
private Roller myRoller;
private Player myPlayerUp;
private int myNumberRollsUsed;
public Vector myPlayer;
public YahtzeeEngine(int numPlayers) {
}
public void startGame() {
}
public void selectPlayerUp(Player player) {
}
public void switchPlayerUp() {
}
public boolean incrementRollsUsed() {
return false;
}
public void resetNumberRollsUsed() {
myNumberRollsUsed = 0;
}
public Roller getRoller() {
return myRoller;
}
public Player getPlayerUp() {
return myPlayerUp;
}
public Vector<Player> getPlayers() {
return myPlayers;
}
public int getNumberRollsUsed() {
return myNumberRollsUsed;
}
public String toString() {
return null;
}
}
这是完成的测试类
package yahtzee.testing;
import static org.junit.Assert.*;
import yahtzee.model.Player;
import yahtzee.model.Roller;
import yahtzee.model.YahtzeeEngine;
import org.junit.Assert;
import org.junit.Test;
/**
* This class is built to test the methods within the YahtzeeEngine class.
* Each test case should pass if the code is properly implemented.
*/
public class TestYahtzeeEngine {
/**
* Tests that the Category keeps track of the proper
* amount of rolls that have been used.
*/
@Test
public void testIncrement() {
YahtzeeEngine ye = new YahtzeeEngine(2);
for (int i = 0; i < 3; i++) {
ye.incrementRollsUsed();
}
assertTrue(ye.getNumberRollsUsed() == 3);
}
/**
* Tests if the number of rolls is reset to zero when resetRolls() has been called.
*/
@Test
public void testResetRolls() {
YahtzeeEngine ye = new YahtzeeEngine(1);
for (int i = 0; i < 3; i++) {
ye.incrementRollsUsed();
}
ye.resetNumberRollsUsed();
assertTrue(ye.getNumberRollsUsed() == 0);
}
/**
* Tests if the current player up will rotate properly
* when the switchPlayerUp() method is invoked.
*/
@Test
public void testPlayerUp() {
YahtzeeEngine ye = new YahtzeeEngine(2);
Player player1 = ye.getPlayers().get(0);
ye.selectPlayerUp(player1);
ye.switchPlayerUp();
assertFalse(ye.getPlayerUp().equals(player1));
}
/**
* Tests if the player vector is the correct length for min number of players.
*/
@Test
public void testMinPlayerVector() {
YahtzeeEngine ye = new YahtzeeEngine(1);
assertTrue(ye.getPlayers().size() == 1);
}
/**
* Tests if the player vector is the correct length for max number of players.
*/
@Test
public void testMaxPlayerVector() {
YahtzeeEngine ye = new YahtzeeEngine(6);
assertTrue(ye.getPlayers().size() == 6);
}
/**
* Test that bad number of players passed in defaults to 2 players.
*/
@Test
public void testTooFewPlayerVector1() {
YahtzeeEngine ye = new YahtzeeEngine(0);
assertTrue(ye.getPlayers().size() == 2);
}
@Test
public void testTooFewPlayerVector2() {
YahtzeeEngine ye = new YahtzeeEngine(7);
assertTrue(ye.getPlayers().size() == 2);
}
/**
* Tests that the game is properly instantiated after startGame() has been called.
*/
@Test
public void testStartOfGame() {
YahtzeeEngine ye = new YahtzeeEngine(2);
ye.startGame();
assertTrue(ye.getNumberRollsUsed() == 0);
assertTrue(ye.getPlayerUp() != null);
assertTrue(ye.getPlayers().get(0) != null);
assertTrue(ye.getPlayers().get(1) != null);
assertTrue(ye.getRoller() != null);
}
/**
* Testing values from YahtzeeEngine on default construction.
*/
@Test
public void testYahtzeeEngineConstructor() {
YahtzeeEngine ye = new YahtzeeEngine(1);
assertTrue(ye.getNumberRollsUsed() == 0);
assertTrue(ye.getPlayerUp() == null);
assertTrue(ye.getRoller() == null);
assertTrue(ye.getPlayers().size() == 1);
}
}
答案 0 :(得分:0)
方法“toString()”我将用于以格式化方式返回值。 例如,如果你的类有浮点数X,Y,Z我将返回(“X =”+ X +“Y =”+ Y +“Z =”+ Z)
在startGame()中我会创建一个运行游戏循环的线程:
public void startGame() {
Thread game = new Thread(this);
game.start();
}
public void stopGame() {
System.exit(1);
}
public void run() {
while (run) {
// TODO your game
}
// terminate
stopGame();
}
你必须为此实现Runnable,因为否则你不能将'this'赋给Thread构造函数。
变量run是一个布尔值,表示你的游戏仍在运行,所以如果你将它设置为false
,你的游戏就会停止。