-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.java
More file actions
50 lines (40 loc) · 1.08 KB
/
Copy pathHand.java
File metadata and controls
50 lines (40 loc) · 1.08 KB
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
package strategy;
public class Hand {
public static final int HANDVALUE_GUU = 0; // 012 石头剪刀布
public static final int HANDVALUE_CHO = 1;
public static final int HANDVALUE_PAA = 2;
public static final Hand[] hand = {
new Hand(HANDVALUE_GUU),
new Hand(HANDVALUE_CHO),
new Hand(HANDVALUE_PAA)
};
public static final String[] name = {
"石头", "剪刀", "布"
};
private int handValue;
public Hand(int handValue) {
this.handValue = handValue;
}
public static Hand getHand(int handValue) {
return hand[handValue];
}
public boolean isStrongerThan(Hand h) {
return fight(h) == 1;
}
public boolean isWeekerThan(Hand h) {
return fight(h) == -1;
}
public int fight(Hand h) {
if (this == h) {
return 0;
} else if ((this.handValue + 1) % 3 == h.handValue) {
return 1;
} else {
return -1;
}
}
@Override
public String toString() {
return name[handValue];
}
}