Commit 5bc5120e authored by Qi LI's avatar Qi LI
Browse files

add Game test

parent 40052ea2
......@@ -2,20 +2,32 @@ package game;
import java.util.List;
import java.util.Scanner;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.InputMismatchException;
public class Game {
private List<Player> players = new ArrayList<Player>();
private Scanner inputReader = new Scanner(System.in);
private Scanner inputReader = null;
public Game() {}
public Game(InputStream in) {
this.inputReader = new Scanner(in);
}
private void enterPlayers(){
int newPlayerNumber = 0;
while(true) {
System.out.format("Number of new players: ");
newPlayerNumber = inputReader.nextInt();
try {
newPlayerNumber = inputReader.nextInt();
} catch (InputMismatchException i) {
System.out.println(inputReader.next() + "is not a number.");
}
if (newPlayerNumber > 0 || players.size() != 0) break;
}
......@@ -33,8 +45,7 @@ public class Game {
while(!newSet.isOver()) {
Player next = newSet.getNextPlayer();
if (!next.isOff())
{
if (!next.isOff()) {
int point = -1;
while (point < 0 || point > 10) {
System.out.format("%n%s, please enter the point: ", next.getName());
......@@ -50,7 +61,7 @@ public class Game {
}
} else if (command.equals("quit")) {
next.quitNow();
System.out.format("%n%s has quit the game !", next.getName());
System.out.format("%n%s has quit the game !%n", next.getName());
break;
}
} else {
......@@ -66,25 +77,21 @@ public class Game {
}
}
}
}
else
{
int point =0;
} else {
int point = 0;
try {
String info = newSet.enterPoint(point);
if (info != null) System.out.println(info);
newSet.enterPoint(point);
//System.out.format("%s's score table:%n%s%n", next.getName(), newSet.getScoreTable(next));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
return newSet.getWinner();
}
public boolean quitGame() {
System.out.println("%nGo on next set? (Type 'quit' to stop)");
System.out.println("\nGo on next set? (Type 'quit' to stop)");
return inputReader.next().equals("quit");
}
......@@ -96,7 +103,23 @@ public class Game {
}
public static void main (String[] args) {
Game game = new Game();
Game game = null;
if (args.length >= 2) {
Path path = FileSystems.getDefault().getPath("src", "test", args[1]);
try {
InputStream in = Files.newInputStream(path);
game = new Game(in);
} catch (IOException e) {
System.out.println("No file " + path.toString() + " found!");
System.out.println("Read input from System.in instead");
game = new Game(System.in);
}
} else {
game = new Game(System.in);
}
while(true) {
game.enterPlayers();
game.enrollAllPlayers();
......
package test;
import org.junit.Test;
import game.Game;
public class TestGame {
public TestGame() {}
@Test
public void testCase0() {
Game.main(new String[]{"game", "input"});
}
}
2
Alice
Bob
10
9
0
score
quit
quit
no
no new player
-1
11
10
10
10
10
quit
quit
quit
\ No newline at end of file
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