forked from n-utku-n/Simple-Okey-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
163 lines (138 loc) · 5.17 KB
/
Copy pathPlayer.java
File metadata and controls
163 lines (138 loc) · 5.17 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.net.SocketImplFactory;
import java.util.Arrays;
public class Player {
private String playerName;
private Tile[] playerTiles;
private int numberOfTiles;
public Player(String name) {
setName(name);
playerTiles = new Tile[15]; // there are at most 15 tiles a player owns at any time
numberOfTiles = 0; // currently this player owns 0 tiles, will pick tiles at the beggining of the game
}
/**
* Removes the tile in the given index by making all elements greater than the given index move 1 index to the left
*/
public Tile getAndRemoveTile(int index) {
Tile tile = playerTiles[index];
if (this.playerTiles[index] != null) {
for (int i = index; i < numberOfTiles - 1; i++) {
playerTiles[i] = playerTiles[i + 1];
}
playerTiles[numberOfTiles - 1] = null;
numberOfTiles--;
return tile;
}
return null;
}
/**
* Adds the given tile to the array of tiles kept in the player class and increments numberOfTiles by 1.
* Finds the index the tile should be inserted without sorting the array.
* Finds the index it should be inserted to and shifts all elements greater than the index to the right by 1
* @author Elif Bozkurt, Ali Sevindi, Mert Uzun
*/
public void addTile(Tile t) {
if (this.numberOfTiles >= 15) return; //if the player has 15 or more tiles return
int index = this.numberOfTiles; // if there is no tiles with the value of t, t is added to the end of the array.
if (index > 0){
for (int i = 0; i < numberOfTiles; i++) {
if (t.compareTo(getTiles()[i]) < 1) {
index = i;
break;
}
}
for (int j = this.numberOfTiles ; j > index; j--) {
playerTiles[j] = playerTiles[j - 1];
}
}
playerTiles[index] = t;
numberOfTiles ++;
}
/**
* DONE: This method works for chains of length 4 as after sorting the array it only checks the 3 tiles after that tile
* if we have ...4-4-4-4-4... it should count the first four 4s as a chain and not the last 4. By the game logic the player
* should only have chains of length 4 since there cant be tiles of same color in the same chain. It returns chainsOfFour == 3 and not
* chainsOfFour >= 3 since there cannot be more than 3 chains. If the player is winning there should be 3 chains of length 4 and 2 extra tiles
* @author Elif Bozkurt, Utku Kabukçu
*/
public boolean isWinningHand() {
int chainsOfFour = 0;
// Remove elements those are not null
Tile[] tilesCopy = Arrays.stream(this.playerTiles).filter(tile -> tile != null).toArray(Tile[]::new);
Arrays.sort(tilesCopy);
for (int i = 0; i < tilesCopy.length - 3; i++) {
if (tilesCopy[i].canFormChainWith(tilesCopy[i + 1]) &&
tilesCopy[i + 1].canFormChainWith(tilesCopy[i + 2]) &&
tilesCopy[i + 2].canFormChainWith(tilesCopy[i + 3])) {
chainsOfFour++;
i += 3;
}
}
return chainsOfFour == 3;
}
public int findPositionOfTile(Tile t) {
int tilePosition = -1;
for (int i = 0; i < numberOfTiles; i++) {
if(playerTiles[i].compareTo(t) == 0) {
tilePosition = i;
}
}
return tilePosition;
}
/**
* Counts the number of pairs in a player's hand.
* A pair is four tiles with the same value but different colors.
* @author Mert Uzun
*/
public int countPairs() {
int pairs = 0;
int[] colorCount = new int[8]; // Since values are in range [1,7], index 0 is unused.
// Count occurrences of each value
for (int i = 0; i < numberOfTiles; i++) {
if (playerTiles[i] != null) {
colorCount[playerTiles[i].getValue()]++;
}
}
// Count valid pairs
for (int i = 1; i <= 7; i++) {
pairs += colorCount[i] / 4;
}
return pairs;
}
/**
* Display players current hand with the indexes on top and each tile at the bottom,
* @author Elif Bozkurt, Mert Uzun
*/
public void displayTiles() {
System.out.println(playerName + "'s Tiles:");
for (int i = 0; i < numberOfTiles; i++) {
if(playerTiles[i] == null){
continue;
}
else{
System.out.printf("%-3d",i);
}
}
System.out.println();
for (int i = 0; i < numberOfTiles; i++) {
if (playerTiles[i] == null) {
continue;
}
else{
System.out.print(playerTiles[i].toString() + " ");
}
}
System.out.println();
}
public Tile[] getTiles() {
return playerTiles;
}
public void setName(String name) {
playerName = name;
}
public String getName() {
return playerName;
}
public int getNumberOfTiles(){
return numberOfTiles;
}
}