forked from n-utku-n/Simple-Okey-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.java
More file actions
98 lines (84 loc) · 2.4 KB
/
Copy pathTile.java
File metadata and controls
98 lines (84 loc) · 2.4 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
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
public class Tile implements Comparable {
int value;
char color;
/*
* Creates a tile using the given color and value, colors are represented
* using the following letters: Y: Yellow, B: Blue, R: Red, K: Black
* Values can be in the range [1,7]. There are four tiles of each color value
* combination (7 * 4 * 4) = 112 tiles, false jokers are not included in this game.
*/
public Tile(int value, char color) {
this.value = value;
this.color = color;
}
/*
* Compares tiles so that they can be added to the hands in order
*/
@Override
public int compareTo(Object t) {
Tile other = (Tile)t;
if(getValue() < other.getValue()) {
return -1;
}
else if(getValue() > other.getValue()) {
return 1;
}
else{
if(colorNameToInt() < other.colorNameToInt()) {
return -1;
}
else if(colorNameToInt() > other.colorNameToInt()) {
return 1;
}
else{
return 0;
}
}
}
public int colorNameToInt() {
if(color == 'Y') {
return 0;
}
else if(color == 'B') {
return 1;
}
else if(color == 'R') {
return 2;
}
else {
return 3;
}
}
// determines if this tile can make a chain with the given tile
public boolean canFormChainWith(Tile t) {
if (t == null) {
return false; // Prevent NullPointerException
}
return t.getColor() != this.color && t.getValue() == this.value;
}
@Override
public String toString() {
return "" + value + color;
}
/**
* Looks for two tiles if they are equal or not, based on both color and number matchings
* Handles the null exception
* @param other other tile to be considered
* @return true or false if these two tiles are equal or not
* @author Mert Uzun, Utku Kabukçu
*/
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Tile)) {
return false;
}
Tile other = (Tile) obj;
return this.toString().equals(other.toString());
}
public int getValue() {
return value;
}
public char getColor() {
return color;
}
}