-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.java
152 lines (131 loc) · 3.41 KB
/
Card.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
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
/**
* Maxim Karpinsky
* ID: 2405869
* email: [email protected]
* Course: CPSC 231
* Assignment: Programming Mastery Project 3-A: Let’s Play Cards!
*/
import java.util.Random;
import java.util.HashMap;
/**
* Card class serves for creating individual cards with their properties like
* suit and card value along with a string representation and comparison to
* other cards.
*/
public class Card{
private HashMap<Integer, String> suits;
private HashMap<Integer, String> values;
private int cardSuit;
private int cardValue;
/**
* Creates the bounds for the card suit and value range so when a card is
* generated or inputted it aligns with the matches in here.
*/
private void createCardBounds()
{
suits = new HashMap<Integer, String>();
values = new HashMap<Integer, String>();
suits.put(0, "Hearts");
suits.put(1, "Spades");
suits.put(2, "Clubs");
suits.put(3, "Diamonds");
for(int i = 2; i <= 10; i++)
{
values.put(i, "" + i);
}
values.put(11, "Jack");
values.put(12, "Queen");
values.put(13, "King");
values.put(14, "Ace");
}
/**
* Default constructor for the card class generates a random card.
*/
public Card()
{
createCardBounds();
Random rand = new Random();
cardSuit = rand.nextInt(4);
cardValue = rand.nextInt(13) + 2;
}
/**
* Overloaded constructor for card class with inputs to create a specific card.
* @param suit the suit of the card
* @param value the card value 2-10 or Ace, Jack, Queen, King (numerically represented)
*/
public Card(int suit, int value)
{
createCardBounds();
cardSuit = suit;
cardValue = value;
}
/**
* Copy constructor for card class
* @param otherCard the other card object to copy from
*/
public Card(Card otherCard)
{
createCardBounds();
cardSuit = otherCard.getSuit();
cardValue = otherCard.getCardValue();
}
/**
* Setter method for the suit of the card
* @param suit numerical value of the suit to set to
*/
public void setSuit(int suit)
{
cardSuit = suit;
}
/**
* Get method for the suit of the card
* @return the suit of the card in its numerical representation.
*/
public int getSuit()
{
return cardSuit;
}
public String getStringSuit(int num)
{
return suits.get(num);
}
public String getStringCardValue(int num)
{
return values.get(num);
}
/**
* setter method for the value of the card
* @param value the numerical value of the card to be set to
*/
public void setCardValue(int value)
{
cardValue = value;
}
/**
* getter method for the value of the card
* @return the numerical representation of the value of the card
*/
public int getCardValue()
{
return cardValue;
}
/**
* String representation of the card stating its suit and value
* @return the string format of the card so its readable
*/
public String toString()
{
return(values.get(getCardValue()) + " of " + suits.get(getSuit()));
}
/**
* Comparison method to check if card is equal to another card based on if
* their suits and values are the same.
* @param otherCard the other card object to compare to
* @return true if the cards are equal or false if cards are not equal.
*/
public boolean equals(Card otherCard)
{
return(getSuit() == otherCard.getSuit() &&
getCardValue() == otherCard.getCardValue());
}
}