-
Notifications
You must be signed in to change notification settings - Fork 16
/
Lesson_06_Functions_Pass_By_Reference_Cards_Example.java
154 lines (135 loc) · 5.75 KB
/
Lesson_06_Functions_Pass_By_Reference_Cards_Example.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
153
154
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello World with Dr. Dan - A Complete Introduction to Programming from Java to C++ (Code and Course © Dan Grissom)
//
// Additional Lesson Resources from Dr. Dan:
// High-Quality Video Tutorials: www.helloDrDan.com
// Free Commented Code: https://github.com/DanGrissom/hello-world-dr-dan-java
//
// In this lesson you will learn:
// 1) Functions
// a) Passing arrays by reference instead of value
// b) More practice converting repeated code into functions
// 2) String class methods
// a) String.format() to format Strings
// 3) Array copying
// a) Using the Arrays class to make a shallow copy of an array
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Arrays;
import java.util.Random;
public class Lesson_06_Functions_Pass_By_Reference_Cards_Example {
// Constants
private static final String [] SUITS = { "Spades", "Diamonds", "Clubs", "Hearts" };
private static final String [] VALUES = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
// Create random object
private static Random randy = new Random();
///////////////////////////////////////////////////////////////
// MAIN - Entry point where code will begin execution for file
///////////////////////////////////////////////////////////////
public static void main(String[] args) {
// Simple welcome statements printed to screen
System.out.println("Program Objective: Learn to pass (array) by reference and to copy arrays");
System.out.println("===========================================================================");
// Generate a new deck of ordered cards and print
String [] newDeck = createOrderedDeck();
printDeck("A new deck", newDeck);
// Repeatedly create a new shuffle of cards and print
for (int i = 0; i < 5; i++) {
String [] shuffledDeck = createShuffledDeck(newDeck);
printDeck( String.format("Shuffled deck %s", i+1), shuffledDeck );
}
}
///////////////////////////////////////////////////////////////////////////////////////
// This method creates a new shuffled deck from a new deck.
// Parameters:
// NONE
//
// Returns:
// An array of Strings containing an ordered deck of cards
///////////////////////////////////////////////////////////////////////////////////////
private static String[] createOrderedDeck() {
// Create a new deck of "unshuffled" cards from the unique suits and values
int numCardsInDeck = SUITS.length * VALUES.length;
String [] newDeck = new String[numCardsInDeck];
int cardCount = 0;
for (int s = 0; s < SUITS.length; s++) {
for (int v = 0; v < VALUES.length; v++) {
String newCard = VALUES[v] + " of " + SUITS[s];
newDeck[cardCount] = newCard;
cardCount++;
}
}
// Return new deck of cards
return newDeck;
}
///////////////////////////////////////////////////////////////////////////////////////
// This method prints out each card in the deck of cards passed in.
// Parameters:
// cardSetName - A string representing the name of the deck
// deck - An array of Strings (cards)
//
// Returns:
// void (nothing)
///////////////////////////////////////////////////////////////////////////////////////
private static void printDeck(String cardSetName, String[] deck) {
System.out.printf(cardSetName + " of %s cards contains:\n", deck.length);
for (int i = 0; i < deck.length; i++)
System.out.println("\t" + deck[i]);
System.out.println();
}
///////////////////////////////////////////////////////////////////////////////////////
// This method creates a new shuffled deck from a new deck.
// Parameters:
// originDeck - An array of Strings (cards) in order
//
// Returns:
// An new array of Strings (deck of cards) containing a randomly shuffled deck of cards
///////////////////////////////////////////////////////////////////////////////////////
private static String[] createShuffledDeck(String[] originDeck) {
// Make a copy of the originDeck, making sure we never change the origin deck
// so we can always use it again to generate a new deck
String[] deckCopy = Arrays.copyOf(originDeck, originDeck.length);
// Get number of cards in deck
int numCardsInDeck = deckCopy.length;
// Create empty deck of proper size
String [] shuffledDeck = new String [numCardsInDeck];
// Copy cards over, one at a time
for (int i = 0; i < numCardsInDeck; i++) {
// Randomly pull card from new deck...
//String pulledCard = newDeck[i];
String pulledCard = null;
// Check a random card slot to see if it has not already been pulled
int randomIndex = randy.nextInt(numCardsInDeck);
if (!deckCopy[randomIndex].equals("X")) {
pulledCard = deckCopy[randomIndex];
deckCopy[randomIndex] = "X";
} else {
// Look for the first card AFTER the random index...
for (int j = randomIndex + 1; j < deckCopy.length; j++) {
if (!deckCopy[j].equals("X")) {
pulledCard = deckCopy[j];
deckCopy[j] = "X";
break;
}
}
// If no card after the random index, look for the first card BEFORE the random index
if (pulledCard == null) {
for (int j = randomIndex - 1; j >= 0; j--) {
if (!deckCopy[j].equals("X")) {
pulledCard = deckCopy[j];
deckCopy[j] = "X";
break;
}
}
}
}
// If we were NOT able to find an unpulled, we did not init the deck properly
if (pulledCard == null) {
System.err.print("The new deck passed in was not initialized properly. Exiting program...");
System.exit(0);
}
// If make it here, we do have a pulled card; place pulled card into shuffled deck
shuffledDeck[i] = pulledCard;
}
return shuffledDeck;
}
}