Skip to content

Commit

Permalink
Break out common code from RoShamBo1.java, reuse in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceEckel committed Mar 5, 2021
1 parent ede3954 commit a75a55d
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 569 deletions.
522 changes: 69 additions & 453 deletions concurrent/PSP2.txt

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions enums/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// enums/Item.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;

public interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}
21 changes: 21 additions & 0 deletions enums/Paper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// enums/Paper.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
import static enums.Outcome.*;

public class Paper implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return DRAW; }
@Override
public Outcome eval(Scissors s) { return WIN; }
@Override
public Outcome eval(Rock r) { return LOSE; }
@Override public String toString() {
return "Paper";
}
}
53 changes: 0 additions & 53 deletions enums/RoShamBo1.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,6 @@
// {java enums.RoShamBo1}
package enums;
import java.util.*;
import static enums.Outcome.*;

interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}

class Paper implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return DRAW; }
@Override
public Outcome eval(Scissors s) { return WIN; }
@Override
public Outcome eval(Rock r) { return LOSE; }
@Override public String toString() {
return "Paper";
}
}

class Scissors implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return LOSE; }
@Override
public Outcome eval(Scissors s) { return DRAW; }
@Override
public Outcome eval(Rock r) { return WIN; }
@Override public String toString() {
return "Scissors";
}
}

class Rock implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return WIN; }
@Override
public Outcome eval(Scissors s) { return LOSE; }
@Override
public Outcome eval(Rock r) { return DRAW; }
@Override public String toString() {
return "Rock";
}
}

public class RoShamBo1 {
static final int SIZE = 20;
Expand Down
21 changes: 21 additions & 0 deletions enums/Rock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// enums/Rock.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
import static enums.Outcome.*;

public class Rock implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return WIN; }
@Override
public Outcome eval(Scissors s) { return LOSE; }
@Override
public Outcome eval(Rock r) { return DRAW; }
@Override public String toString() {
return "Rock";
}
}
21 changes: 21 additions & 0 deletions enums/Scissors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// enums/Scissors.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
package enums;
import static enums.Outcome.*;

public class Scissors implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) { return LOSE; }
@Override
public Outcome eval(Scissors s) { return DRAW; }
@Override
public Outcome eval(Rock r) { return WIN; }
@Override public String toString() {
return "Scissors";
}
}
6 changes: 6 additions & 0 deletions gradle/subprojects.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ project(':collections') {
}
}

project(':patterns') {
dependencies {
compile project(':enums')
}
}

configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')
Expand Down
69 changes: 6 additions & 63 deletions patterns/PaperScissorsRock.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,15 @@
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import enums.Outcome;
import static enums.Outcome.*;
import enums.Item;
import enums.Paper;
import enums.Scissors;
import enums.Rock;
import onjava.*;
import static onjava.Tuple.*;

enum Outcome { WIN, LOSE, DRAW }

interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}

class Paper implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override public Outcome eval(Paper p) {
return Outcome.DRAW;
}
@Override public Outcome eval(Scissors s) {
return Outcome.WIN;
}
@Override public Outcome eval(Rock r) {
return Outcome.LOSE;
}
@Override public String toString() {
return "Paper";
}
}

class Scissors implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override public Outcome eval(Paper p) {
return Outcome.LOSE;
}
@Override public Outcome eval(Scissors s) {
return Outcome.DRAW;
}
@Override public Outcome eval(Rock r) {
return Outcome.WIN;
}
@Override public String toString() {
return "Scissors";
}
}

class Rock implements Item {
@Override public Outcome compete(Item it) {
return it.eval(this);
}
@Override public Outcome eval(Paper p) {
return Outcome.WIN;
}
@Override public Outcome eval(Scissors s) {
return Outcome.LOSE;
}
@Override public Outcome eval(Rock r) {
return Outcome.DRAW;
}
@Override public String toString() {
return "Rock";
}
}

class ItemFactory {
static List<Supplier<Item>> items =
Arrays.asList(
Expand Down

0 comments on commit a75a55d

Please sign in to comment.