Sunteți pe pagina 1din 8

Brian Poor

Answers to Elevens Lab (Activity 2 6):


Activity 2:

1. Explain in your own words the relationship between a deck and a card.
A Deck is an ArrayList of Card.

2. Consider the deck initialized with the statements below. How many cards does the
deck contain?
String[] ranks = {"jack", "queen", "king"};
String[] suits = {"blue", "red"};
int[] pointValues = {11, 12, 13};
Deck d = new Deck(ranks, suits, pointValues);
There are 3 x 2 = 6 cards in the deck.
3. The game of Twenty-One is played with a deck of 52 cards. Ranks run from ace
(highest) down to 2 (lowest). Suits are spades, hearts, diamonds, and clubs as in
many other games. A face card has point value 10; an ace has point value 11; point
values for 2, , 10 are 2, , 10, respectively. Specify the contents of the ranks,
suits, and pointValues arrays so that the statement
Deck d = new Deck(ranks, suits, pointValues);
initializes a deck for a Twenty-One game.
String[] ranks = {"ace", "king, queen", "jack", 10, 9, 8, 7,
6, 5, 4, 3, 2};
String[] suits = {"spades", "hearts", diamonds, clubs};
int[] pointValues = {11, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2};
Deck d = new Deck(ranks, suits, pointValues);
4. Does the order of elements of the ranks, suits, and pointValues arrays matter?
No. As long as the pointValues are corresponded to the correct ranks.
Activity 3:
1. Write a static method named flip that simulates a flip of a weighted coin by
returning either "heads" or "tails" each time it is called. The coin is twice as likely to
turn up heads as tails. Thus, flip should return "heads" about twice as often as it
returns "tails."
private static int headChance = 2;
private static int tailChance = 1;

public static void flip () {


int total = headChance + tailChance;
int toss = (int)(Math.random()*total + 1);

if (toss >= 1 && toss <= headChance) {


System.out.println("Head");
headCount++;
} else {
System.out.println("Tail");
tailCount++;
}

2. Write a static method named arePermutations that, given two int arrays of the
same length but with no duplicate elements, returns true if one array is a permutation
of the other (i.e., the arrays differ only in how their contents are arranged). Otherwise,
it should return false.
The static method arePermutations are implemented in Shuffler.java.
3. Suppose that the initial contents of the values array in Shuffler.java are
{1, 2, 3, 4}. For what sequence of random integers would the efficient selection
shuffle change values to contain {4, 3, 2, 1}?
The sequence of random integers are 4, 3, 3, 4.
Shuffler.java code:
package Activity3Code;
/**
* This class provides a convenient way to test shuffling methods.
*/
public class Shuffler {
/**
* The number of consecutive shuffle steps to be performed in each call
* to each sorting procedure.
*/
private static final int SHUFFLE_COUNT = 8;
/**
* The number of values to shuffle.
*/
private static final int VALUE_COUNT = 6;
/**
* Tests shuffling methods.
* @param args is not used.
*/
public static void main(String[] args) {

System.out.println("Results of " + SHUFFLE_COUNT +


" consecutive perfect shuffles:");
int[] values1 = new int[VALUE_COUNT];
for (int i = 0; i < values1.length; i++) {
values1[i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++) {
perfectShuffle(values1);
System.out.print(" " + j + ":");
for (int k = 0; k < values1.length; k++) {
System.out.print(" " + values1[k]);
}
System.out.println();
}
System.out.println();
System.out.println("Results of " + SHUFFLE_COUNT +
" consecutive efficient selection
shuffles:");
int[] values2 = new int[VALUE_COUNT];
for (int i = 0; i < values2.length; i++) {
values2[i] = i;
}
for (int j = 1; j <= SHUFFLE_COUNT; j++) {
selectionShuffle(values2);
System.out.print(" " + j + ":");
for (int k = 0; k < values2.length; k++) {
System.out.print(" " + values2[k]);
}
System.out.print(" is permutation of perfect shuffle? " +
arePermutation(values1, values2));
System.out.println();
}
System.out.println();
}
/**
* Apply a "perfect shuffle" to the argument.
* The perfect shuffle algorithm splits the deck in half, then interleaves
* the cards in one half with the cards in the other.
* @param values is an array of integers simulating cards to be shuffled.
*/
public static void perfectShuffle(int[] values) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 3 *** */
int valCt = values.length;
int halfCt = (valCt%2 == 0)? (valCt/2):(valCt + 1)/2;
int k;
int[] shuffled = new int[valCt];
k = 0;
for (int i = 0; i < halfCt; i++ ) {
shuffled[k] = values[i];
k += 2;
}
k = 1;

for (int i = halfCt; i < valCt; i++) {


shuffled[k] = values[i];
k += 2;
}
//values = shuffled;

for (int i = 0; i < valCt; i++) {


values[i] = shuffled[i];
}

/**
* Apply an "efficient selection shuffle" to the argument.
* The selection shuffle algorithm conceptually maintains two sequences
* of cards: the selected cards (initially empty) and the not-yet-selected
* cards (initially the entire deck). It repeatedly does the following until
* all cards have been selected: randomly remove a card from those not yet
* selected and add it to the selected cards.
* An efficient version of this algorithm makes use of arrays to avoid
* searching for an as-yet-unselected card.
* @param values is an array of integers simulating cards to be shuffled.
*/
public static void selectionShuffle(int[] values) {
/* *** TO BE IMPLEMENTED IN ACTIVITY 3 *** */
int k = values.length;
int chosen, temp;

while (k > 0) {
chosen = (int)(Math.random()*(values.length));
//System.out.println("chose pos:"+chosen);
k--;
temp = values[chosen];
values[chosen] = values[k];
values[k] = temp;
}

public static boolean arePermutation(int[] values1, int[] values2) {


int[] myvals1 = new int[values1.length];
int[] myvals2 = new int[values2.length];
int i, j;
if (values1.length != values2.length) return false;
for (i = 0; i < myvals1.length; i++) {
myvals1[i] = values1[i];
}
for (i = 0; i < myvals2.length; i++) {
myvals2[i] = values2[i];
}
i = myvals1.length;
while (i > 0) {
i--;
j = myvals2.length;
while (j > 0) {
j--;

if (myvals2[j] == myvals1[i] && myvals1[i] != -1) {


myvals2[j] = -1;
myvals1[i] = -1;
}

}
for (i = myvals1.length; i > 0; i--) {
if (myvals1[i-1] != -1 || myvals2[i-1] != -1) return false;
}
return true;
}

Activity 4:
Deck.java code:
package Activity4Code;
import java.util.List;
import java.util.ArrayList;
/**
* The Deck class represents a shuffled deck of cards.
* It provides several operations including
*
initialize, shuffle, deal, and check if empty.
*/
public class Deck {
/**
* cards contains all the cards in the deck.
*/
private List<Card> cards;
/**
* size is the number of not-yet-dealt cards.
* Cards are dealt from the top (highest index) down.
* The next card to be dealt is at size - 1.
*/
private int size;
/**
* Creates a new <code>Deck</code> instance.<BR>
* It pairs each element of ranks with each element of suits,
* and produces one of the corresponding card.
* @param ranks is an array containing all of the card ranks.
* @param suits is an array containing all of the card suits.
* @param values is an array containing all of the card point values.
*/
public Deck(String[] ranks, String[] suits, int[] values) {
cards = new ArrayList<Card>();
for (int j = 0; j < ranks.length; j++) {
for (String suitString : suits) {
cards.add(new Card(ranks[j], suitString, values[j]));
}
}

size = cards.size();
shuffle();

/**
* Determines if this deck is empty (no undealt cards).
* @return true if this deck is empty, false otherwise.
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Accesses the number of undealt cards in this deck.
* @return the number of undealt cards in this deck.
*/
public int size() {
return size;
}
/**
* Randomly permute the given collection of cards
* and reset the size to represent the entire deck.
*/
public void shuffle() {
/* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
int k = cards.size();
int chosen;
Card swapCard;
while (k > 0) {
chosen = (int)(Math.random()*(cards.size()));
//System.out.println("chose pos:"+chosen);
k--;
swapCard = cards.get(chosen);
cards.set(chosen, cards.get(k));
cards.set(k, swapCard);
}
}
/**
* Deals a card from this deck.
* @return the card just dealt, or null if all the cards have been
*
previously dealt.
*/
public Card deal() {
if (isEmpty()) {
return null;
}
size--;
Card c = cards.get(size);
return c;
}
/**
* Generates and returns a string representation of this deck.
* @return a string representation of this deck.

*/
@Override
public String toString() {
String rtn = "size = " + size + "\nUndealt cards: \n";

console.

for (int k = size - 1; k >= 0; k--) {


rtn = rtn + cards.get(k);
if (k != 0) {
rtn = rtn + ", ";
}
if ((size - k) % 2 == 0) {
// Insert carriage returns so entire deck is visible on
}

rtn = rtn + "\n";

}
rtn = rtn + "\nDealt cards: \n";
for (int k = cards.size() - 1; k >= size; k--) {
rtn = rtn + cards.get(k);
if (k != size) {
rtn = rtn + ", ";
}
if ((k - cards.size()) % 2 == 0) {
// Insert carriage returns so entire deck is visible on
console.

rtn = rtn + "\n";


}

rtn = rtn + "\n";


return rtn;

Activity 6:
1. List all possible plays for the board 5 4 2 6 A J K 5 2
5 s 6 c, 5 c 6c
2. If the deck is empty and the board has three cards left, must they be J, Q, and K? Why or
why not?
Yes, because all other cards must have been paired up in pairs of two cards, and then there
will be all J's Q's and K's left which can only grouped in groups of 3, the only way of having 3
cards left following the rules will be a J, Q, K.

3. Does the game involve any strategy? That is, when more than one play is possible,
does it matter which one is chosen? Briefly explain your answer.
Yes.

When more than one play is possible, a bad choice will lead to a losing

configuration and a good one might lead to winning. So there is strategy involved (it is
also possible no matter what you choose, you will lose).

S-ar putea să vă placă și