Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a75a55d

Browse files
committed
Break out common code from RoShamBo1.java, reuse in patterns
1 parent ede3954 commit a75a55d

File tree

8 files changed

+156
-569
lines changed

8 files changed

+156
-569
lines changed

concurrent/PSP2.txt

Lines changed: 69 additions & 453 deletions
Large diffs are not rendered by default.

enums/Item.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// enums/Item.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package enums;
6+
7+
public interface Item {
8+
Outcome compete(Item it);
9+
Outcome eval(Paper p);
10+
Outcome eval(Scissors s);
11+
Outcome eval(Rock r);
12+
}

enums/Paper.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// enums/Paper.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package enums;
6+
import static enums.Outcome.*;
7+
8+
public class Paper implements Item {
9+
@Override public Outcome compete(Item it) {
10+
return it.eval(this);
11+
}
12+
@Override
13+
public Outcome eval(Paper p) { return DRAW; }
14+
@Override
15+
public Outcome eval(Scissors s) { return WIN; }
16+
@Override
17+
public Outcome eval(Rock r) { return LOSE; }
18+
@Override public String toString() {
19+
return "Paper";
20+
}
21+
}

enums/RoShamBo1.java

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,6 @@
66
// {java enums.RoShamBo1}
77
package enums;
88
import java.util.*;
9-
import static enums.Outcome.*;
10-
11-
interface Item {
12-
Outcome compete(Item it);
13-
Outcome eval(Paper p);
14-
Outcome eval(Scissors s);
15-
Outcome eval(Rock r);
16-
}
17-
18-
class Paper implements Item {
19-
@Override public Outcome compete(Item it) {
20-
return it.eval(this);
21-
}
22-
@Override
23-
public Outcome eval(Paper p) { return DRAW; }
24-
@Override
25-
public Outcome eval(Scissors s) { return WIN; }
26-
@Override
27-
public Outcome eval(Rock r) { return LOSE; }
28-
@Override public String toString() {
29-
return "Paper";
30-
}
31-
}
32-
33-
class Scissors implements Item {
34-
@Override public Outcome compete(Item it) {
35-
return it.eval(this);
36-
}
37-
@Override
38-
public Outcome eval(Paper p) { return LOSE; }
39-
@Override
40-
public Outcome eval(Scissors s) { return DRAW; }
41-
@Override
42-
public Outcome eval(Rock r) { return WIN; }
43-
@Override public String toString() {
44-
return "Scissors";
45-
}
46-
}
47-
48-
class Rock implements Item {
49-
@Override public Outcome compete(Item it) {
50-
return it.eval(this);
51-
}
52-
@Override
53-
public Outcome eval(Paper p) { return WIN; }
54-
@Override
55-
public Outcome eval(Scissors s) { return LOSE; }
56-
@Override
57-
public Outcome eval(Rock r) { return DRAW; }
58-
@Override public String toString() {
59-
return "Rock";
60-
}
61-
}
629

6310
public class RoShamBo1 {
6411
static final int SIZE = 20;

enums/Rock.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// enums/Rock.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package enums;
6+
import static enums.Outcome.*;
7+
8+
public class Rock implements Item {
9+
@Override public Outcome compete(Item it) {
10+
return it.eval(this);
11+
}
12+
@Override
13+
public Outcome eval(Paper p) { return WIN; }
14+
@Override
15+
public Outcome eval(Scissors s) { return LOSE; }
16+
@Override
17+
public Outcome eval(Rock r) { return DRAW; }
18+
@Override public String toString() {
19+
return "Rock";
20+
}
21+
}

enums/Scissors.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// enums/Scissors.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
package enums;
6+
import static enums.Outcome.*;
7+
8+
public class Scissors implements Item {
9+
@Override public Outcome compete(Item it) {
10+
return it.eval(this);
11+
}
12+
@Override
13+
public Outcome eval(Paper p) { return LOSE; }
14+
@Override
15+
public Outcome eval(Scissors s) { return DRAW; }
16+
@Override
17+
public Outcome eval(Rock r) { return WIN; }
18+
@Override public String toString() {
19+
return "Scissors";
20+
}
21+
}

gradle/subprojects.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ project(':collections') {
5858
}
5959
}
6060

61+
project(':patterns') {
62+
dependencies {
63+
compile project(':enums')
64+
}
65+
}
66+
6167
configure(subprojects - project(':onjava')) {
6268
dependencies {
6369
compile project(':onjava')

patterns/PaperScissorsRock.java

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,72 +6,15 @@
66
import java.util.*;
77
import java.util.function.*;
88
import java.util.stream.*;
9+
import enums.Outcome;
10+
import static enums.Outcome.*;
11+
import enums.Item;
12+
import enums.Paper;
13+
import enums.Scissors;
14+
import enums.Rock;
915
import onjava.*;
1016
import static onjava.Tuple.*;
1117

12-
enum Outcome { WIN, LOSE, DRAW }
13-
14-
interface Item {
15-
Outcome compete(Item it);
16-
Outcome eval(Paper p);
17-
Outcome eval(Scissors s);
18-
Outcome eval(Rock r);
19-
}
20-
21-
class Paper implements Item {
22-
@Override public Outcome compete(Item it) {
23-
return it.eval(this);
24-
}
25-
@Override public Outcome eval(Paper p) {
26-
return Outcome.DRAW;
27-
}
28-
@Override public Outcome eval(Scissors s) {
29-
return Outcome.WIN;
30-
}
31-
@Override public Outcome eval(Rock r) {
32-
return Outcome.LOSE;
33-
}
34-
@Override public String toString() {
35-
return "Paper";
36-
}
37-
}
38-
39-
class Scissors implements Item {
40-
@Override public Outcome compete(Item it) {
41-
return it.eval(this);
42-
}
43-
@Override public Outcome eval(Paper p) {
44-
return Outcome.LOSE;
45-
}
46-
@Override public Outcome eval(Scissors s) {
47-
return Outcome.DRAW;
48-
}
49-
@Override public Outcome eval(Rock r) {
50-
return Outcome.WIN;
51-
}
52-
@Override public String toString() {
53-
return "Scissors";
54-
}
55-
}
56-
57-
class Rock implements Item {
58-
@Override public Outcome compete(Item it) {
59-
return it.eval(this);
60-
}
61-
@Override public Outcome eval(Paper p) {
62-
return Outcome.WIN;
63-
}
64-
@Override public Outcome eval(Scissors s) {
65-
return Outcome.LOSE;
66-
}
67-
@Override public Outcome eval(Rock r) {
68-
return Outcome.DRAW;
69-
}
70-
@Override public String toString() {
71-
return "Rock";
72-
}
73-
}
74-
7518
class ItemFactory {
7619
static List<Supplier<Item>> items =
7720
Arrays.asList(

0 commit comments

Comments
 (0)