I recently posted to question about my toString method that thankfully was answered by someone on the forum. Now I am having some trouble with getting the results I want from my getRandomCard() method. I will post the entire page of code but this is what I am trying to achieve:
-get a random number (lets say it it 52) -now create a temp card to return so I can make the value null. - I want to now amend my array so that the card I just used is replaced by the last number in the deck that has a value - which is why I have the variable cardsRunning.
If anyone can see where I am going wrong I would love some help.
public class Deck {
private Card[] card = new Card[100];
private int cardsRunning = 100;
static int DECKSIZE = 100;
static java.lang.String emailID = "MITNY013";
private boolean random = true;
/**
* Constructor for the Deck. Adds the following cards to the deck in the following order
* 4x Super Lucky Strike, Damage 100, Mana 2
* 6x Mega Santa Hit, Damage 80, Mana 2
* 10x Critical Hit, Damage 50, Mana 5
* 10x Massive Strike, Damage 40, Mana 7
* 15x Wrong Way Down A One Way Street, Damage 30, Mana 10
* 15x Bender Rules Here, Damage 15, Mana 10
* 40x Trade, Damage 5, Mana 5 <br>
* My solution length: 16 lines (space lines not included)
* @param random - Whether to turn on random features
*/
public Deck(boolean random) {
for (int index = 0; index <= 3; index++) {
card[index] = new Card( 2, 100, "Lucky Strike");
}
for (int index = 4; index <= 10; index++) {
card[index] = new Card( 2, 80, "Santa Hit");
}
for (int index = 11; index <= 20; index++) {
card[index] = new Card( 5, 50, "Critical Hit");
}
for (int index = 21; index <= 30; index++) {
card[index] = new Card ( 7, 40, "Massive Strike");
}
for (int index = 31; index <= 45; index++) {
card[index] = new Card( 10, 30, "Wrong Way Down A One Way Street");
}
for (int index = 46; index <= 60; index++) {
card[index] = new Card( 10, 15, "Bender Rules Here");
}
for (int index = 61; index <= 99; index++) {
card[index] = new Card( 5, 5, "Trade");
}
}
/**
* Returns a string representation of the entire deck in the format
* Deck [ 1:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
* 2:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
* 3:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
* 4:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]] <br>
* My solution length: 5 lines
* @overrides - toString in class java.lang.Object
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int index = 0; index < card.length; index++) {
sb.append((index + 1) + ": " + card[index].toString() + "\n");
}
return sb.toString();
}
/**
* Uses a random number generator to get a card from the deck somewhere then swaps the
* last card in the deck to that position and reduces the cardsRemaining by one.
* This is important. if the random flag is false you should always get the card at
* position 0. When you swap the card out you should also set the old position to null
* for safety. <br>
* My solution length: 10 lines
* @return - the card drawn or null if no cards left in deck
*/
public Card getRandomCard() {
// use random number generator
Random rand = new Random();
int randomCardNo = rand.nextInt(this.cardsRunning);
Card newCard = this.card[randomCardNo];
//reorder the array - swaps the last card in the deck to that position
card[randomCardNo] = card[getCardsRemaining() -1];
card[getCardsRemaining() -1] = null;
this.cardsRunning--;
if (this.cardsRunning == 0) {
return null;
}
else {
return newCard;}
}
/**
* Return the number of cards remaining in the deck.
* My solution length: 1 lines
* @return - the total cards remaining
*/
public int getCardsRemaining() {
return this.cardsRunning;}}
Aucun commentaire:
Enregistrer un commentaire