Java Card Game: Deck Class help needed
arrays, class, java, oop, project
Solution
As a starter, your Deck constructor should look something similar to this
for(int i = 2; i <= 14; i++){
for(int j = 0; j < suits.length; j++){
Card card = new Card(i, suits[j]);
card.isDrawn(false);
//ADD CARD TO YOUR DECK HERE
}
}
As for drawCard function, just randomize a number from 1-52 (assuming you are using 52 cards deck) then draw it from your Deck and set `.isDrawn(true)`
Problem
This is my first question on Stack Overflow, so pardon me if I have a rookie mistake within my question and feel free to correct me as I plan to use this site a lot more over the next few months. Anyhow, I'm a grade 12 Student at High School and am stuck on one of my homework questions. So basically we've been given a project of creating a War Game (card game) and we've been asked to do small chunks of it everyday. So far everything has been going good until today, hence my question to y'all. Here's what the project is about: Card - What it has: - Private int value: value of the card - Private char suit: suit of the card - Private Boolean drawn: whether or not the card has been drawn from the deck - What it does: - Public Card(int val, char s): creates a card with value val, suit s, and sets drawn to False - Public int getValue(): returns the value of a card - Public char getSuit(): returns the suit of a card - Public Boolean isDrawn(): returns whether or not a card has been drawn - Public String toString(): returns the string describing the card in the form “value of suit” Deck - What it has: - Private char[] suits = { ‘c’, ‘d’, ‘h’, ‘s’}: representation of suits - Public int numCards: number of cards in the deck (traditionally 52 – make this lower for testing!) - Private Card[] deck: deck stored as an array of cards I'm done everything upto this point. - What it does: - Public Deck(): creates a deck of cards – values ranging from 2-14, one of each suit - Public card drawCard(): draws a random card from the deck that has not been drawn yet - Public String toString(): design however you want for testing purposes Now I've finished doing the Public deck step with the help of Mr. Quynh below however I'm confused as to how I should go about creating the drawCard method. Here's what I have so far: ``` public Card drawnCard(){ int randNum = (int) Math.random()*13+2; } ``` So i know that the above code creates a random Number between 2 and 14 but how do i use that to draw the physical card? Like what do I type so that the program knows to draw a card? Sorry, I know you may feel like I'm asking you for each step now, but I truly am lost and require your help! I'm a newbie when it comes to programming and hence I apologize if this question is far too simple/noobie for you, but I really need your help :) Thank you for your time!