oop - Trouble retrieving random object from "deck" of card objects -
my issue : when i'm drawing random card deck i've instantiated returns correct(i.e. king_clubs) weird 1 (i.e. seven_). odd because when decks instantiated, added print line statement in deck constructor see if id's being added correctly. every time without fail card id's correct.
the println in blackjack's hituser() method check id of card being drawn. it's correct other times it's not. can tell me what's happening , why?
runnable relevant code below
my card class:
import java.util.*; import javax.swing.*; import java.awt.*; public class card { //cardvalues[] represents tangible values attached card faces(ace, one, two, three, ..., king) private final static int cardvalues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; private final static string cardids[] = {"ace_", "two_", "three_", "four_", "five_", "six_", "seven_", "eight_", "nine_", "ten_", "jack_", "queen_", "king_"}; private int value; //name of card, i.e. "ace"; private string cardid; /** * constructor * @param v - card value(i.e. 1 ace or 13 king) */ public card(int v){ setvalue(v); setcardid(v); } /** * constructor * @param v - card value(i.e. 1 ace or 13 king) * @param id - manually set id card(used 'illegal' card instantiation) */ public card(int v, string id){ setvalue(v); setcardid(id); } /** * returns card id * @return - card id of respective card */ public string getcardid(){ return cardid; } /** * returns card value * @return - number value of respective card */ public int getvalue(){ return value; } /** * setter method card value * @param v - value */ public void setvalue(int v){ //checks see if v valid cardvalue if(v >= 1 && v <= 13){ value = v; } } /** * 'legal' setter method card id * @param v - number value of card */ public void setcardid(int v){ //checks see if v valid cardvalue if(v >= 1 && v <= 13){ cardid = cardids[v - 1]; } } /** * 'illegal' setter method card id * @param id - string value of card id */ public void setcardid(string id){ cardid = id; } public string geticon(){ return "blackjack/" + this.getcardid() + ".png"; } my deck class:
import java.util.*; import javax.swing.*; import java.awt.*; public class deck { private int numcards; private string cardsuits[] = {"spades", "hearts", "diamonds", "clubs"}; private static arraylist<card> cards = new arraylist<card>(); public static jlabel[] cardicons = new jlabel[52]; public static int dealerhand, playerhand; /** * constructor */ public deck(){ //will keep track of index of 52 cards created int counter = 0; for(int y = 0; y < 4; y++){ string suit = cardsuits[y]; for(int z = 1; z < 14; z++){ //adds new card , initializes number value cards.add(new card(z)); //replaces card id new id containing card suit (i.e. spades) if(cards.get(counter).getcardid().indexof("_") == cards.get(counter).getcardid().length() - 1){ string newid = cards.get(counter).getcardid() + suit; cards.get(counter).setcardid(newid); system.out.println(newid); counter++; } else { } } } } /** * removes card deck - gets rid of object in array of cards once has been drawn * @param id - card removed */ public void removecard(string id){ for(int = 0; < 52; i++){ if(cards.get(i).getcardid().equalsignorecase(id)){ cards.remove(i); } } } /** * returns object of card within deck * @param c - index of card within deck * @return card object */ public card getcard(int c){ return cards.get(c); } /** * returns random card array of cards in deck * @return - random card object */ public card getrandomcard(){ random r = new random(); int index = r.nextint(cards.size()); return cards.get(index); } /** * resets deck original, 'perfect' order */ public void reset(){ for(int x = 0; x<cards.size(); x++){ cards.remove(x); } //will keep track of index of 52 cards created int counter = 0; for(int y = 0; y < 4; y++){ string suit = cardsuits[y]; for(int z = 0; z < 13; z++){ //adds new card , initializes number value cards.add(new card(z)); //replaces card id new id containing card suit (i.e. spades) string newid = cards.get(counter).getcardid() + suit; cards.get(counter).setcardid(newid); counter++; } } } my blackjack class:
import javax.swing.*; import net.miginfocom.swing.miglayout; /** * class provides of functionality of blackjack game * @author mohamed amadou * */ public class blackjack extends jframe implements actionlistener{ public miglayout mig = new miglayout("insets 0"); public deck bjdeckhouse = new deck(), bjdeckuser = new deck(); public int dealeroffset = 65, useroffset = 65, househand = 0, public final int max_bet = 100000; public jpanel bj = new jpanel(new miglayout("insets 0")), blackjack = new jpanel(new miglayout("insets 0")); public jbutton hit; public blackjack(){ buildui(); mig.layoutcontainer(bj); setsize(780, 700); setresizable(false); setlayout(mig); settitle("blackjack"); setdefaultcloseoperation(dispose_on_close); setvisible(true); } public void buildui(){ getcontentpane().add(bj); bj.setbackgroundcolor(color.black); hit = new jbutton("hit"); hit.addactionlistener(this); bj.add(hit, "pos 570px 285px"); } public void hituser(int hand){ jpanel test = new jpanel(new miglayout("insets 0")); jframe testframe = new jframe(); testframe.add(test); card hitcard = bjdeckuser.getrandomcard(); hand += hitcard.getvalue(); string position = "pos "+ useroffset + "px" + " 580px"; system.out.println(hitcard.getcardid()); bjdeckuser.removecard(hitcard.getcardid()); turns++; checkbust(hand); testframe.setsize(400, 400); testframe.setresizable(false); testframe.setlayout(mig); testframe.settitle("blackjack rules"); testframe.setdefaultcloseoperation(dispose_on_close); testframe.setvisible(true); } public void actionperformed(actionevent e) { if(e.getsource() == hit){ hituser(userhand); } }
your problem lies in reset():
for(int x = 0; x<cards.size(); x++){ cards.remove(x); } this not remove items (just play through), should use cards.clear(). since deck not empty, , add 52 cards, deck incorrect. additionally weird names, seven_clubs_spades , similar.
also, code little bit confusing. suite should attribute of card, not of deck. cards should immutable (maybe enums depending on usage). have 2 initialization code snippets (you should have 1). gui code in "business model". few ideas clean :)
Comments
Post a Comment