Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

PromptFor, RockPaperScissorsTest accounts for trailing newline,

remove InteractivePlayer still hanging around
  • Loading branch information...
commit 7b8816c1f2025881be4387b9670000033a72d580 1 parent 726bbd1
@youngnh authored
View
66 src/com/twoguys/rps/InteractivePlayer.java
@@ -1,66 +0,0 @@
-package com.twoguys.rps;
-
-import java.io.*;
-
-public class InteractivePlayer implements Player {
-
- private String prompt;
- private LineNumberReader fromPlayer;
- private Writer toPlayer;
- private String name;
-
- public InteractivePlayer(Reader fromPlayer, Writer toPlayer, int n) throws IOException {
- this.prompt = "Player " + n + " Name: ";
- this.toPlayer = toPlayer;
- this.fromPlayer = new LineNumberReader(fromPlayer);
- init();
- }
-
- private void init() throws IOException {
- toPlayer.write(prompt);
- toPlayer.flush();
- name = fromPlayer.readLine();
- }
-
- @Override
- public String getName() {
- return name;
- }
-
- @Override
- public Throw getThrow() {
- Throw thrown = null;
- try {
- toPlayer.write("[R]ock, [P]aper, or [S]cissors? ");
- toPlayer.flush();
- String input = fromPlayer.readLine();
- if(input != null) {
- char choice = input.toUpperCase().charAt(0);
- switch(choice) {
- case 'R':
- thrown = new Rock();
- break;
- case 'P':
- thrown = new Paper();
- break;
- case 'S':
- thrown = new Scissors();
- break;
- }
- }
- } catch(IOException e) {
- // squelch
- }
- return thrown;
- }
-
- @Override
- public void notifyWinner() {
- try {
- toPlayer.write(getName() + " Wins!\n");
- toPlayer.flush();
- } catch(IOException e) {
- // squelch
- }
- }
-}
View
11 src/com/twoguys/util/PromptFor.java
@@ -0,0 +1,11 @@
+package com.twoguys.util;
+
+import java.io.*;
+
+public class PromptFor<T> extends PromptForFrom<T> {
+
+ public PromptFor(Print notify, Reader<T> reader) {
+ super(new LineNumberReader(new InputStreamReader(System.in)), notify, reader);
+ }
+
+}
View
5 test/RockPaperScissorsTest.java
@@ -65,6 +65,7 @@ public void testNoArgumentsResultsInSingleThrowGame() throws Exception {
rps.run(args);
String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/noargs_game.expected")).replace("\n", "");
+ expected += "\n";
String actual = out.toString();
assertEquals(expected, actual);
@@ -81,6 +82,7 @@ public void testFirstToGameResultsInBothPlayersPlaying() throws Exception {
rps.run(args);
String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/firstto_game.expected")).replace("\n", "");
+ expected += "\n";
String actual = out.toString();
assertEquals(expected, actual);
@@ -97,6 +99,7 @@ public void testBestOfStopsAfterPlayerWinsMoreThanHalf() throws Exception {
rps.run(args);
String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/bestof_game.expected")).replace("\n", "");
+ expected += "\n";
String actual = out.toString();
assertEquals(expected, actual);
@@ -113,6 +116,7 @@ public void testWinByGameContinuesUntilOnePlayerBeatsOtherByMargin() throws Exce
rps.run(args);
String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/winby_game.expected")).replace("\n", "");
+ expected += "\n";
String actual = out.toString();
assertEquals(expected, actual);
@@ -129,6 +133,7 @@ public void testNateWins() throws Exception {
rps.run(args);
String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/nate_wins.expected")).replace("\n", "");
+ expected += "\n";
String actual = out.toString();
assertEquals(expected, actual);
View
103 test/com/twoguys/rps/InteractivePlayerTest.java
@@ -1,103 +0,0 @@
-package com.twoguys.rps;
-
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
-
-import java.io.*;
-import org.junit.*;
-
-public class InteractivePlayerTest {
-
- public final static String P1_PROMPT = "Player 1 Name: ";
- public final static String P2_PROMPT = "Player 2 Name: ";
- public final static String MOVE_PROMPT = "[R]ock, [P]aper, or [S]cissors? ";
- private String nate = "Nate";
- private String ben = "Ben";
- private Reader in;
- private CharArrayWriter out;
-
- @Before
- public void setup() {
- in = new StringReader(nate + "\n");
- out = new CharArrayWriter();
- }
-
- @Test
- public void testPromptsForName() throws Exception {
- Player player = new InteractivePlayer(in, out, 1);
-
- String written = out.toString();
- assertEquals(P1_PROMPT, written);
- }
-
- @Test
- public void testPromptsPlayerNumber() throws Exception {
- Player player1 = new InteractivePlayer(in, out, 1);
- String written = out.toString();
- assertEquals(P1_PROMPT, written);
-
- out.reset();
- Player player2 = new InteractivePlayer(in, out, 2);
- written = out.toString();
- assertEquals(P2_PROMPT, written);
- }
-
- @Test
- public void testReadsNameFromInput() throws Exception {
- Player player = new InteractivePlayer(in, out, 1);
-
- assertEquals(-1, in.read());
- }
-
- @Test
- public void testSetsPlayerName() throws Exception {
- Player player = new InteractivePlayer(in, out, 1);
- assertEquals(nate, player.getName());
-
- in = new StringReader(ben + "\n");
- player = new InteractivePlayer(in, out, 2);
- assertEquals(ben, player.getName());
- }
-
- @Test
- public void testPromptsForMove() throws Exception {
- Player player = new InteractivePlayer(in, out, 1);
- out.reset();
-
- player.getThrow();
- String written = out.toString();
- assertEquals(MOVE_PROMPT, written);
- }
-
- @Test
- public void testGetThrowReturnsRock() throws Exception {
- in = new StringReader(nate + "\n" + "R" + "\n");
-
- Player player = new InteractivePlayer(in, out, 1);
- out.reset();
-
- Throw thrown = player.getThrow();
- assertEquals(Rock.class, thrown.getClass());
- }
-
- @Test
- public void testGetThrowReturnsPaper() throws Exception {
- in = new StringReader(nate + "\n" + "P" + "\n");
-
- Player player = new InteractivePlayer(in, out, 1);
- out.reset();
-
- Throw thrown = player.getThrow();
- assertEquals(Paper.class, thrown.getClass());
- }
-
- @Test
- public void testNotifyWinner() throws Exception {
- Player player = new InteractivePlayer(in, out, 1);
- out.reset();
-
- player.notifyWinner();
- String written = out.toString();
- assertEquals(nate + " Wins!\n", written);
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.