-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.java
More file actions
130 lines (112 loc) · 4.16 KB
/
Copy pathTile.java
File metadata and controls
130 lines (112 loc) · 4.16 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
package test;
import java.util.Objects;
import java.util.Random;
public class Tile {
public final char letter;
public final int score;
public char getLetter() {
return letter;
}
public int getScore() {
return score;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Tile)) return false;
Tile tile = (Tile) o; // Cast to Tile
return letter == tile.letter && score == tile.score;
}
@Override
public int hashCode() {
return Objects.hash(letter, score);
}
private Tile(char letter, int score) {
this.letter = letter;
this.score = score;
}
public static class Bag {
private int[] quantity;
private Tile[] tile_array;
private static Bag b = null;
private Bag() {
this.quantity = new int[]{9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
this.tile_array = new Tile[quantity.length]; // Creating an array of 26 Tile objects
int[] tile_values = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
// Initializing the array of tiles with quantity values and characters 'A' to 'Z'
char ch = 'A';
for (int i = 0; i < quantity.length; i++) {
tile_array[i] = new Tile(ch, tile_values[i]);
ch++;
}
}
public static Bag getBag() {
if(b == null)
b = new Bag();
return b;
}
public Tile getRand() {
if(this.size() == 0)
return null;
else{
Random random = new Random();
int min = 0; // Minimum value
int max = 25; // Maximum value
int randomNumberInRange = random.nextInt(max - min + 1) + min;
while(quantity[randomNumberInRange] == 0) {
randomNumberInRange = (randomNumberInRange + 1) % 26;
}
quantity[randomNumberInRange]--;
return tile_array[randomNumberInRange];
}
}
public Tile getTile(char ch) {
if(ch >= 'A' && ch <= 'Z'){
char characterA = 'A'; // Character to convert
int asciiValueA = (int) characterA; // Convert char to ASCII value
int asciiValueChar = (int) ch;
asciiValueChar-=asciiValueA;
if(quantity[asciiValueChar] == 0)
return null;
else {
quantity[asciiValueChar]--;
return tile_array[asciiValueChar];
}
}
else{
return null;
}
}
private int[] getFullQuantity(){
int[] full = new int[] {9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
return full;
}
public void put(Tile t) {
char characterA = 'A'; // Character to convert
int asciiValueA = (int) characterA; // Convert char to ASCII value
int asciiValueChar = (int) t.getLetter();
asciiValueChar-=asciiValueA;
int full = getFullQuantity()[asciiValueChar];
if(full>this.quantity[asciiValueChar]){
this.quantity[asciiValueChar]++;
}
}
public int size(){
int s = 0;
for (int a: quantity) {
s+=a;
}
return s;
}
// Method to return a duplicate of the quantity array
public int[] getQuantities() {
// Creating a new array with the same length as the quantity array
int[] duplicateArray = new int[quantity.length];
// Copying elements from the quantity array to the duplicate array
for (int i = 0; i < quantity.length; i++) {
duplicateArray[i] = quantity[i];
}
return duplicateArray;
}
}
}