Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'type_driven' - the Haskell-inspired Java
implementation of RPS Conflicts: .gitignore build.xml src/RockPaperScissors.java src/com/twoguys/rps/Game.java src/com/twoguys/rps/Player.java src/com/twoguys/rps/Throw.java test/com/twoguys/rps/GameTest.java test/com/twoguys/rps/ThrowTest.java
- Loading branch information
Showing
57 changed files
with
1,928 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
build/* | ||
dist/* | ||
dist/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Player 1 Name: | ||
Player 2 Name: | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
Ben Wins! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Nate | ||
Ben | ||
R | ||
P | ||
R | ||
S | ||
R | ||
P | ||
R | ||
S | ||
R | ||
P |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Player 1 Name: | ||
Player 2 Name: | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
Ben Wins! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Nate | ||
Ben | ||
R | ||
P | ||
R | ||
P | ||
R | ||
P |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Player 1 Name: | ||
Player 2 Name: | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
Nate Wins! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Nate | ||
Ben | ||
R | ||
S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Player 1 Name: | ||
Player 2 Name: | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
Ben Wins! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Nate | ||
Ben | ||
R | ||
P |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Player 1 Name: | ||
Player 2 Name: | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
[R]ock, [P]aper, or [S]cissors? | ||
Ben Wins! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Nate | ||
Ben | ||
R | ||
P | ||
R | ||
S | ||
R | ||
P | ||
R | ||
S | ||
R | ||
P | ||
R | ||
P | ||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
|
||
import com.twoguys.rps.*; | ||
import com.twoguys.util.*; | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class RockPaperScissors { | ||
|
||
public static void main(String[] args) throws Exception { | ||
Reader in = new InputStreamReader(System.in); | ||
private LineNumberReader in; | ||
private Writer out; | ||
|
||
public static void main(String[] args) { | ||
InputStreamReader stdin = new InputStreamReader(System.in); | ||
LineNumberReader in = new LineNumberReader(stdin); | ||
Writer out = new OutputStreamWriter(System.out); | ||
|
||
RockPaperScissors rps = new RockPaperScissors(in, out); | ||
rps.run(args); | ||
} | ||
|
||
public RockPaperScissors(LineNumberReader in, Writer out) { | ||
this.in = in; | ||
this.out = out; | ||
} | ||
|
||
public void run(String[] args) { | ||
try { | ||
Either<String, WinLogic> logicOrError = WinLogicFactory.create(args); | ||
|
||
Player p1 = new InteractivePlayer(in, out, 1); | ||
Player p2 = new InteractivePlayer(in, out, 2); | ||
if(logicOrError.left() != null) { | ||
out.write(logicOrError.left()); | ||
} else { | ||
WinLogic logic = logicOrError.right(); | ||
|
||
Game game = new Game(p1, p2); | ||
Player winner = game.play(); | ||
winner.notifyWinner(); | ||
PlayerPrompt prompt1 = new PlayerPrompt(in, out, 1); | ||
PlayerPrompt prompt2 = new PlayerPrompt(in, out, 2); | ||
|
||
Player player1 = prompt1.prompt(); | ||
Player player2 = prompt2.prompt(); | ||
|
||
AllThrows p1Throws = new AllThrows(new ThrowPrompt(in, out)); | ||
AllThrows p2Throws = new AllThrows(new ThrowPrompt(in, out)); | ||
|
||
Pair<Player, Iterator<Throw>> p1 = new Pair<Player, Iterator<Throw>>(player1, p1Throws); | ||
Pair<Player, Iterator<Throw>> p2 = new Pair<Player, Iterator<Throw>>(player2, p2Throws); | ||
|
||
Game game = new Game(logic, p1, p2); | ||
Player winner = game.play(); | ||
|
||
out.write(winner.getName() + " Wins!"); | ||
} | ||
} catch(Exception e) { | ||
e.printStackTrace(new PrintWriter(out)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.twoguys.rps; | ||
|
||
import com.twoguys.util.*; | ||
import java.text.*; | ||
import java.util.*; | ||
|
||
public class AllThrows implements Iterator<Throw> { | ||
|
||
private Prompt<Throw> prompt; | ||
private Throw next; | ||
|
||
public AllThrows(Prompt<Throw> prompt) { | ||
this.prompt = prompt; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
try { | ||
next = prompt.prompt(); | ||
} catch(ParseException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Throw next() { | ||
Throw result = next; | ||
try { | ||
if(next == null) { | ||
result = prompt.prompt(); | ||
} | ||
next = null; | ||
return result; | ||
} catch(Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.twoguys.rps; | ||
|
||
import com.twoguys.util.*; | ||
|
||
public class BestOf implements WinLogic { | ||
|
||
private int of; | ||
private WinLogic impl; | ||
|
||
public BestOf(int of) { | ||
if(of <= 1 || of % 2 == 0) { | ||
throw new IllegalArgumentException(of + " is not valid, must provide an odd number greater than 1"); | ||
} | ||
|
||
this.of = of; | ||
this.impl = new FirstTo(of / 2 + 1); | ||
} | ||
|
||
public Maybe<Player> determineWinner(Pair<Player, Integer> p1, Pair<Player, Integer> p2) { | ||
return impl.determineWinner(p1, p2); | ||
} | ||
|
||
public int getOf() { | ||
return of; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.twoguys.rps; | ||
|
||
import com.twoguys.util.*; | ||
|
||
public class FirstTo extends WinBy { | ||
|
||
public FirstTo(int to) { | ||
super(to, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,50 @@ | ||
package com.twoguys.rps; | ||
|
||
import com.twoguys.util.*; | ||
import java.util.*; | ||
|
||
public class Game { | ||
|
||
private Player p1; | ||
private Player p2; | ||
public static final Maybe<Player> NO_WINNER = new Nothing(); | ||
|
||
private WinLogic logic; | ||
private Player player1; | ||
private Player player2; | ||
private int p1Score = 0; | ||
private int p2Score = 0; | ||
private Iterator<Throw> p1Throws; | ||
private Iterator<Throw> p2Throws; | ||
|
||
public Game(Player p1, Player p2) { | ||
this.p1 = p1; | ||
this.p2 = p2; | ||
public Game(WinLogic logic, Pair<Player, Iterator<Throw>> player1, Pair<Player, Iterator<Throw>> player2) { | ||
this.logic = logic; | ||
this.player1 = player1.a; | ||
this.player2 = player2.a; | ||
this.p1Throws = player1.b; | ||
this.p2Throws = player2.b; | ||
} | ||
|
||
public Player play() { | ||
Throw p1Throw = p1.getThrow(); | ||
Throw p2Throw = p2.getThrow(); | ||
private Maybe<Player> getWinner() { | ||
Pair<Player, Integer> p1 = new Pair<Player, Integer>(player1, p1Score); | ||
Pair<Player, Integer> p2 = new Pair<Player, Integer>(player2, p2Score); | ||
|
||
return p1Throw.beats(p2Throw) ? p1 : p2; | ||
return logic.determineWinner(p1, p2); | ||
} | ||
|
||
public Player play() { | ||
Maybe<Player> player = getWinner(); | ||
while(player.equals(NO_WINNER)) { | ||
if(p1Throws.hasNext() && p2Throws.hasNext()) { | ||
Throw t1 = p1Throws.next(); | ||
Throw t2 = p2Throws.next(); | ||
if(t1.beats(t2)) { | ||
p1Score++; | ||
} else if(t2.beats(t1)) { | ||
p2Score++; | ||
} | ||
} | ||
player = getWinner(); | ||
} | ||
return player.value(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
package com.twoguys.rps; | ||
|
||
public interface Player { | ||
public class Player { | ||
|
||
public String getName(); | ||
public Throw getThrow(); | ||
public void notifyWinner(); | ||
private String name; | ||
|
||
public Player(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.twoguys.rps; | ||
|
||
import com.twoguys.util.*; | ||
import java.io.*; | ||
|
||
public class PlayerPrompt extends Prompt<Player> { | ||
|
||
public PlayerPrompt(LineNumberReader in, Writer out, int n) { | ||
super(in, new Notify(out, "Player " + n + " Name: "), new com.twoguys.util.Reader<Player>(new ReadPlayer())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.twoguys.rps; | ||
|
||
import com.twoguys.util.*; | ||
import java.util.*; | ||
|
||
public class ReadPlayer implements Read<Player> { | ||
|
||
public List<Pair<Player, String>> read(String str) { | ||
Player player = new Player(str); | ||
Pair<Player, String> parsed = new Pair<Player, String>(player, ""); | ||
return Arrays.asList(parsed); | ||
} | ||
} |
Oops, something went wrong.