-4

我有这个类的测试用例,我必须填写所有方法。我得到了一些最后的那些只是简单地返回已经创建的东西。但我不知道如何做其余的方法。有小费吗?我们正在创建一个 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);
    }
}
4

1 回答 1

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您的游戏将停止。

于 2013-10-22T23:06:28.893 回答