-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCardClass.java
120 lines (96 loc) · 2.45 KB
/
CardClass.java
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
/** Defines an object representing a single card
* in the game TopTrumps
*/
public class CardClass {
/** Class Constants */
private static final int NUM_CATEGORIES = 5;
/** Instance Variables */
private String description;
private int cat1, cat2, cat3, cat4, cat5;
private int [] cardValues;
/** Default Constructor */
public CardClass() {
}
/** Non-default constructor */
public CardClass(String cardInfo) {
String [] tokens = cardInfo.split(" ");
for (int i=0; i<tokens.length; i++)
{
description = tokens[0];
cat1 = Integer.parseInt(tokens[1]);
cat2 = Integer.parseInt(tokens[2]);
cat3 = Integer.parseInt(tokens[3]);
cat4 = Integer.parseInt(tokens[4]);
cat5 = Integer.parseInt(tokens[5]);
}
cardValues = new int [NUM_CATEGORIES+1];
//The cardValues indices correspond to the category numbering
cardValues[0] = 0;
cardValues[1] = cat1;
cardValues[2] = cat2;
cardValues[3] = cat3;
cardValues[4] = cat4;
cardValues[5] = cat5;
}
/** Accessor for description */
public String getDescription() {
return description;
}
/** Accessor for cat1 */
public int getCatOne() {
return cat1;
}
/** Accessor for cat2 */
public int getCatTwo() {
return cat2;
}
/** Accessor for cat3 */
public int getCatThree() {
return cat3;
}
/** Accessor for cat4 */
public int getCatFour() {
return cat4;
}
/** Accessor for cat4 */
public int getCatFive() {
return cat5;
}
/**
* Returns the value at a given index which indicates the category
* for the user's topcard.
* @param indexCat - the index of the category
* @return the value at that index
*/
public int catAtIndex(int indexCat){
return cardValues[indexCat];
}
/**
* Method which finds the highest category value for a
* given card.
* This method uses the find the maxinum value of an array algorithm.
* @return the index of the category with the highest value.
*/
public int getHighestValue() {
int largest = cardValues[0];
int i=1;
int largestIndex = 0;
while (i<cardValues.length) {
if (cardValues[i]>largest) {
largest = cardValues[i];
largestIndex=i;
}
i++;
}
return largestIndex;
}
/**
* Returns a string representation of the card, including both
* the description and the values.
* Example: "TRex 6 6 12 9 9"
* @return card details as a String
*/
public String toString() {
return description +" " +cat1 +" "+ cat2 +" "+ cat3 +" "+ cat4 +" " +cat5;
}
}