-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPSMatch.java
executable file
·126 lines (107 loc) · 2.89 KB
/
RPSMatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* An RPSMatch represents a single game of Rock-Paper-Scissors. A new
* instance of this class is created for every match, and the
* RPSWorld then stores this instance in its match record.
*/
public class RPSMatch
{
private RPSCritter one;
private RPSCritter two;
private int oneThrow;
private int twoThrow;
private boolean oneValid;
private boolean twoValid;
//constants
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;
public RPSMatch(RPSCritter one, RPSCritter two)
{
this.one = one;
this.two = two;
oneThrow = one.fight(two);
twoThrow = two.fight(one);
oneValid = validThrow(oneThrow);
twoValid = validThrow(twoThrow);
String oneThrowName = getThrowName(oneThrow);
String twoThrowName = getThrowName(twoThrow);
System.out.println("\t\t" + one.yourName() + " throws: " + oneThrowName);
System.out.println("\t\t" + two.yourName() + " throws: " + twoThrowName);
if (!oneValid)
{
System.out.println("\t> "+one.yourName() + " forfeits, invalid throw");
if(one.getGrid() != null) one.removeSelfFromGrid();
}
if (!twoValid)
{
System.out.println("\t> "+two.yourName() + " forfeits, invalid throw");
if(two.getGrid() != null) two.removeSelfFromGrid();
}
}
public String toString()
{
String o = one.getClass().toString() + ":" + getThrowName(oneThrow);
String t = two.getClass().toString() + ":" + getThrowName(twoThrow);
return "[" + o + " vs " + t + "]";
}
public boolean validThrow(int sign)
{
if (sign == ROCK) return true;
if (sign == PAPER) return true;
if (sign == SCISSORS) return true;
return false;
}
public Class getCritterOneClass()
{
return one.getClass();
}
public int getCritterOneThrow()
{
return oneThrow;
}
public Class getCritterTwoClass()
{
return two.getClass();
}
public int getCritterTwoThrow()
{
return twoThrow;
}
public boolean isDraw()
{
return oneThrow == twoThrow;
}
public RPSCritter getWinner()
{
if(!oneValid || !twoValid)
{
if(!oneValid && !twoValid) return null;
if(!oneValid) return two;
if(!twoValid) return one;
}
else if(oneThrow == ROCK)
{
if (twoThrow == PAPER) return two;
if (twoThrow == SCISSORS) return one;
}
else if(oneThrow == PAPER)
{
if (twoThrow == SCISSORS) return two;
if (twoThrow == ROCK) return one;
}
else if(oneThrow == SCISSORS)
{
if (twoThrow == ROCK) return two;
if (twoThrow == PAPER) return one;
}
// we should never get this far
return null;
}
private String getThrowName(int t)
{
if(t == ROCK) return "ROCK";
if(t == PAPER) return "PAPER";
if(t == SCISSORS) return "SCISSORS";
return "invalid";
}
}