-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCardHandReader_gonz.java
155 lines (136 loc) · 3.54 KB
/
CardHandReader_gonz.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
import java.util.ArrayList;
import java.util.Random;
/**
* @author Brandon Gonzalez
* Assignment: AList-Asg4: Card Hand Dealer
*/
public class CardHandReader_gonz {
public static void main(String[] args) {
Random random = new Random();
ArrayList< Hand > myHands = new ArrayList< Hand >();
Deck myDeck = new Deck();
myDeck.addDeck();
//init myHands
for(int i = 0; i < 4; ++i) {
myHands.add(new Hand(random.nextInt(6) + 2));
}
//Adds cards to hand
for(int i = 0; i< myHands.size(); ++i) {
for(int x = 0; x < myHands.get(i).getSize(); ++x1)
myHands.get(i).getCard(myDeck);
}
//Prints out the cards from each hand
for(int i = 0; i < myHands.size(); ++i) {
System.out.println("Hand "+(i+1));
myHands.get(i).printCards();
System.out.println();
}
}
}
class Card {
//Instance variables
private int rank;
private String suit;
private String[] name = {"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"};
//Constructor
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
/**
* Returns the rank
*/
public Integer getRank() {
return rank;
}
/**
* Returns the card name
*/
public String getName() {
return name[rank%13];
}
/**
* ToString methode that prints out the rank and suit
*/
public String toString() {
return getName()+" of "+suit;
}
}
class Deck {
//Instance Variables
Random random = new Random();
private ArrayList< Card > myDeck = new ArrayList< Card >();
private int deckSize;
private String[] suit = {"Hearts",
"Spade",
"Ace",
"Club"};
public Deck() {
this.deckSize = 52;
}
/**
* Adds a 52 card deck to the myDeck arrayList
*/
public void addDeck() {
for(String mySuit: suit)
for(int i = 1; i < 14; ++i)
myDeck.add(new Card(i, mySuit));
}
/**
* This picks a random card, then removes it from the deck, then returns the val
* deckSize is decremented by 1 to prevent out of index errors
*/
public Card dealCard() {
int randVal = random.nextInt((deckSize-2) + 2);
Card retCard = myDeck.get(randVal);
myDeck.remove(randVal);
deckSize--;
return retCard;
}
}
class Hand {
//Instance variable
private ArrayList< Card > myHand = new ArrayList< Card >();
private int max_hand_size;
public Hand(int maxSize) {
this.max_hand_size = maxSize;
}
/**
* Gets a card from the Deck passed in the params
* Calls Deck.dealCard() which removes the card from the deck
*/
public void getCard(Deck myDeck){
if(myHand.size() < max_hand_size)
myHand.add(myDeck.dealCard());
else
System.out.println("Max hand size reached!");
}
/**
* Prints out all the cards
*/
public void printCards() {
for(Card myCard: myHand)
System.out.println(myCard.toString());
}
public void useCard() {
/* Needs to be implemented
* Print out cards in hand -> prompt to pick which card
* -> use that card -> remove card from hand -> reinsert into deck?
*/
}
public int getSize() {
return max_hand_size;
}
}