Commit 0965f53c authored by Qi LI's avatar Qi LI
Browse files

enter points and find next player

parent 7823a4d7
package game; package game;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class Set { public class Set {
private final List<Player> players; private final List<Player> players;
private HashMap<Player, ScoreTable> tables;
private int currentPlayerIndex = 0;
private int frameCount = 0;
public Set(List<Player> players) { public Set(List<Player> players) {
this.players = players; this.players = players;
this.tables = new HashMap<Player, ScoreTable>();
for (Player player : players) {
tables.put(player, new ScoreTable());
}
} }
public int getPlayerNumber() { public void enterPoint(int point) {
return players.size(); ScoreTable tableToEnter = tables.get(players.get(currentPlayerIndex));
tableToEnter.enterPoint(point);
updatePlayerIndex();
} }
public List getPlayers() { public Player getNextPlayer() {
return players; return players.get(currentPlayerIndex);
}
public boolean isOver() {
return currentPlayerIndex == -1;
}
private void updatePlayerIndex() {
if (frameCount < 10) {
Frame currentFrame = tables.get(players.get(currentPlayerIndex)).getCurrentFrame();
if (currentFrame.isFinished()) currentPlayerIndex++;
if (currentPlayerIndex == players.size()) {
currentPlayerIndex = 0;
frameCount++;
}
}
if (frameCount == 10) {
int nextPlayerIndex = -1;
for (int i = 0; i < players.size() && nextPlayerIndex == -1; ++i) {
if (!tables.get(players.get(i)).isComplete())
nextPlayerIndex = i;
}
currentPlayerIndex = nextPlayerIndex;
}
} }
} }
package test;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import game.Player;
import game.Set;
public class TestSet {
public TestSet() {}
@Test
public void testCase0() {
Player alice = new Player("Alice");
Player bob = new Player("Bob");
Set set = new Set(Arrays.asList(new Player[] {alice, bob}));
int[] points = {10, 7, 2, 8, 0, 10, 7};
Player[] expected = {alice, bob, bob, alice, alice, bob, alice};
Player[] actuals = new Player[expected.length];
for (int i = 0; i < points.length; ++i) {
actuals[i] = set.getNextPlayer();
set.enterPoint(points[i]);
}
Assert.assertArrayEquals(expected, actuals);
}
@Test
public void testCase1() {
Player alice = new Player("Alice");
Player bob = new Player("Bob");
Player charlie = new Player("Charlie");
Set set = new Set(Arrays.asList(new Player[] {alice, bob, charlie}));
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 3; ++j)
set.enterPoint(10);
}
int[] points = {4, 5, 7, 3, 10, 9, 10, 8};
Player[] expectedPlayer = {alice, alice, bob, bob, charlie, bob, charlie, charlie};
Player[] actualPlayer = new Player[points.length];
boolean[] expectedOver = new boolean[points.length];
boolean[] actualOver = new boolean[points.length];
for (int i = 0; i < points.length; ++i) {
actualPlayer[i] = set.getNextPlayer();
set.enterPoint(points[i]);
expectedOver[i] = false;
actualOver[i] = set.isOver();
}
expectedOver[points.length - 1] = true;
Assert.assertArrayEquals(expectedPlayer, actualPlayer);
Assert.assertArrayEquals(expectedOver, actualOver);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment