This repository was archived by the owner on Mar 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathArray.java
More file actions
154 lines (138 loc) · 4.55 KB
/
Array.java
File metadata and controls
154 lines (138 loc) · 4.55 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
public class Array {
private int count;
private String array[];
// Constructor initialzing array and its length
public Array(int length) {
this.array = new String[length];
}
// Method which prints the array
// And skips any index with val of null
public void printArray() {
for(int i =0; i < count; i++) {
// checking if the index is a valid card (not null)
if (array[i] != null) {
System.out.print(array[i] + " ");
}
}
}
public String getCard(int index) {
return array[index];
}
public int cardValue(String card) {
if (card.equals("King") != true && card.equals("Queen") != true && card.equals("Jack") != true && card.equals("Ace") != true ) {
return Integer.parseInt(card);
}
else if (card.equals("King") == true) {
return 13;
}
else if (card.equals("Queen") == true) {
return 12;
}
else if (card.equals("Jack") == true) {
return 11;
}
else if (card.equals("Ace") == true) {
return 14;
}
else {
return -1;
}
}
public int cardValue(int index) {
String card = getCard(index);
if (card.equals("King") != true && card.equals("Queen") != true && card.equals("Jack") != true && card.equals("Ace") != true ) {
return Integer.parseInt(card);
}
else if (card.equals("King") == true) {
return 13;
}
else if (card.equals("Queen") == true) {
return 12;
}
else if (card.equals("Jack") == true) {
return 11;
}
else if (card.equals("Ace") == true) {
return 14;
}
else {
return -1;
}
}
public int getLength() {
return count;
}
// Method which gets the index of a specified card
public int getIndex(String cardName) {
// loops through every element in the array
for(int i = 0; i < array.length; i++) {
// Stops when the first card equaling the search
// parameter is found
if(array[i] == cardName) {
return i;
}
}
// returns -1 if none are found
return -1;
}
// Method which adds a card onto the end of the array
// Can expand the array dynamically if needed
public void insertCard(String card) {
// checking if the length of the array is equal to
// the amount of cards in the array (count)
if (array.length == count) {
// creating a new array that's twice the size
String[] newArray = new String[count*2];
// for loop which copies the array onto a dif array
// then replaces the original array with the new one
for(int i=0; i<count; i++) {
newArray[i] = array[i];
}
array = newArray;
}
// add the card onto the end of the array, then increase
// the value of count to indicate another card is in the deck
array[count++] = card;
}
public void removeCard(String cardName) {
// Get the index of the card to be removed and mark it
array[getIndex(cardName)] = "Skip";
String[] newArray = new String[array.length-1];
int index = 0;
// loops through every card in the array
// and if it != the card it skips it
for(int i=0; i<array.length; i++) {
if (array[i] != "Skip") {
newArray[index] = array[i];
index++;
}
}
// Replace the array with the resized one
// And reduce the size of the array by 1
array = newArray;
count--;
}
// Resets the array and makes a new blank deck
public void newBlankDeck(int size) {
// Resets the array and count
String[] blankArray = new String[1];
array = blankArray;
count = 0;
// Adds each card x (size) times
for(int i =0; i<size; i++) {
insertCard("Ace");
insertCard("King");
insertCard("Queen");
insertCard("Jack");
insertCard("10");
insertCard("9");
insertCard("8");
insertCard("7");
insertCard("6");
insertCard("5");
insertCard("4");
insertCard("3");
insertCard("2");
}
}
}