Deck of Cards issue -Java -
i having problems getting deckofcards class compile. keeps telling me cannot find symbol when pointing deck2.shuffle()
, deck2.deal()
. ideas why?
import java.lang.math; import java.util.random; public class deckofcards { private cards[] deck; private int cardhold; public deckofcards() { deck = new cards[52]; int n = 0; (int = 1; <= 13; i++) { (int j = 1; j <= 4; j++) { deck[n] = new cards(i, j); n = n + 1; } } cardhold = -1; } public void shuffle() { // shuffles ands resets deck int = 0; while (i < 52) { int rando = (int) (5.0 * (math.random())); cards temp = deck[rando]; deck[rando] = deck[i]; deck[i] = temp; i++; } } public cards deal() { // if there more cards left in deck, return next 1 , // increment // index; return null if cards have been dealt // ***question, increment before or // after??***---------------------------------------- if (!hasmorecards()) { return null; } else { cards temp = null; temp = deck[cardhold]; cardhold = cardhold + 1; return temp; } } public boolean hasmorecards() { // returns true if there more cards left, else return false if (cardhold == 0) return false; else return true; } public static void main(string[] args) { deckofcards deck2 = new deckofcards(); deck2.shuffle(); (int = 0; < 52; i++) system.out.println(deck2.deal()); } }
below class card class, maybe causing issue?
public class cards { protected int rank; protected int suit; protected string[] snames = { "hearts", "clubs", "spades", "diamonds" }; protected string[] rnames = { "ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king" }; public cards(int rank, int suit) { suit = suit; rank = rank; } public string tostring() { return ("your card is: " + rnames[rank - 1] + " of " + snames[suit - 1]); } public int getrank() { return rank; } public int getsuit() { return suit; } }
your compiler issues behind you, there lots of issues code.
public class deckofcards { private cards[] deck; private int cardhold; public deckofcards() { deck = new cards[52]; int n = 0; (int = 1; <= 13; i++) { (int j = 1; j <= 4; j++) { deck[n] = new cards(i, j); n = n+1; } } cardhold = -1; } public void shuffle() { int = 0; while (i < 52) { int rando = (int) (5.0*(math.random())); cards temp = deck[rando]; deck[rando] = deck[i]; deck[i] = temp; i++; } } public cards deal() { return (hasmorecards() ? deck[++cardhold] : null); } public boolean hasmorecards() { return (cardhold != 0); } public static void main(string[] args) { deckofcards deck2 = new deckofcards(); deck2.shuffle(); (int = 0; < 52; i++) system.out.println(deck2.deal()); } }
and:
public class cards { protected int rank; protected int suit; protected string[] snames = {"hearts", "clubs", "spades", "diamonds"}; protected string[] rnames = {"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"}; public cards(int rank, int suit) { this.suit = suit; this.rank = rank; } public string tostring() { return ("your card is: "+rnames[rank-1]+" of "+snames[suit-1]); } public int getrank() { return rank; } public int getsuit() { return suit; } }
Comments
Post a Comment