-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_24.java
More file actions
137 lines (117 loc) · 4.23 KB
/
Copy pathExercise07_24.java
File metadata and controls
137 lines (117 loc) · 4.23 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
/***************************************************************************
* This program creates a deck of cards, shuffles them, picks the top four,
* checks if they are from four different suites, and tries again if they aren't
* different. Then it prints the natural English name of the cards.
***************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_24 {
public static void main(String[] args) {
int[] deck = createDeckOfCards();
int[] topFourCardsArray = new int[4];
while(!allSuites(topFourCardsArray)){ //shuffle until allSuites is true
deck = shuffleDeck(deck);
topFourCardsArray = topFour(deck);
}
printTopFour(topFourCardsArray);
}
//creates a deck of cards in order and puts them into an int array
public static int[] createDeckOfCards(){
int[] deck = new int[52];
for (int i = 0; i < deck.length; i++) {
deck[i] = i;
}
/*for(int j : deck)
System.out.println(j);
*/
return deck;
}
//shuffles a deck of cards
public static int[] shuffleDeck(int[] deck){
for (int i = 0; i < 500; i++) {
int indexOne = (int)(Math.random()*52);
int indexTwo = (int)(Math.random()*52);
if(indexOne != indexTwo) //make sure a swap will accually happen
deck = swapNumbers(deck, indexOne, indexTwo);
}
/*
for(int j : deck)
System.out.println(j);
*/
return deck;
}
//returns the top four cards in an array
public static int[] topFour(int[] deck){
int[] topfour = new int[4];
java.lang.System.arraycopy(deck, 0, topfour, 0, 4);
return topfour;
}
//returns true if all four suites are present in the first four cards
public static boolean allSuites(int[] topfour){
if(topfour.length != 4)
System.out.println("Error: top four array in allSuites does not have exactly 4 elements");
boolean[] includesSuites = new boolean[4];
for (int i = 0; i < 4; i++) {
int cardSuite = (int)topfour[i]/13;
includesSuites[cardSuite] = true;
}
return(includesSuites[0] && includesSuites[1] && includesSuites[2] && includesSuites[3]);
}
//Takes an array as an input, the first and second int are the indexes to be swapped.
public static int[] swapNumbers(int[] deck, int indexOne, int indexTwo){
int temp = deck[indexOne];
deck[indexOne] = deck[indexTwo];
deck[indexTwo] = temp;
return deck;
}
//switch case to name the top four cards and print the results
public static void printTopFour(int[] topFour){
String suite;
String card;
for(int i : topFour){
switch(i/13){ //find the suite
case 0: suite = "Diamonds";
break;
case 1: suite = "Clubs";
break;
case 2: suite = "Hearts";
break;
case 3: suite = "Spades";
break;
default: suite = "Error";
}
switch(i/4){
case 0: card = "Ace";
break;
case 1: card = "2";
break;
case 2: card = "3";
break;
case 3: card = "4";
break;
case 4: card = "5";
break;
case 5: card = "6";
break;
case 6: card = "7";
break;
case 7: card = "8";
break;
case 8: card = "9";
break;
case 9: card = "10";
break;
case 10: card = "Jack";
break;
case 11: card = "Queen";
break;
case 12: card = "King";
break;
default: card = "Error";
}
System.out.println(card + " of " + suite);
}
}
}