Hello!
So i have this working code for a deck of cards. I can generate cards and print them out. However this is all in one class.
I want to be able to make it so that i have one class with a name "Card" and anothe with the name "Deck" and use the Card class to get one or
more cards from the Deck class and at the same be able to tell me the same things such as what cards, or card, that i have drawn and of how many
cards that are left.
But this, however, is getting to me... I don't know how to get this working.
I mean i should just be able to make a couple of methods in the Deck class and then call them all from the Card class.
And within each method the lines of code, that are below, i should just be able to use for each method.
Did that but didn't work at all haha.
Any ideas?
This is the working code for the one class:
public static void main(String[] args) { int Mnumber = 3; // Value for the amout of cards that are given out! int counter = 0; // Deck objectDeck = new Deck(); String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" }; String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; for (int i = 0; i < ourDeck.length; i++) { ourDeck[i] = i; } // objectDeck.flush(ourDeck); for (int i = 0; i < ourDeck.length; i++) { int index = (int) (Math.random() * ourDeck.length); int temp = ourDeck[i]; ourDeck[i] = ourDeck[index]; ourDeck[index] = temp; } for (int i = 0; i < Mnumber; i++) { String Suit = suits[ourDeck[i] / 13]; String Rank = rank[ourDeck[i] % 13]; counter++; System.out.println("Card number is " + ourDeck[i] + " : " + Rank + " of " + Suit); if (counter == Mnumber) { System.out.print("And there are " + (52 - Mnumber) + " cards left!"); } } }
Here is the Deck and Card class that i never got working:
public class Deck { public static int[] ourDeck = new int[52]; static String[] suits = { "Spades", "Hearts", "Diamonds", "Clubs" }; static String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" }; public int flush(int[] ourDeck) { int temp = 0; int i = 0; for (i = 0; i < ourDeck.length; i++) { ourDeck[i] = i; for (int k = 0; k < ourDeck.length; k++) { int index = (int) (Math.random() * ourDeck.length); temp = ourDeck[k]; ourDeck[k] = ourDeck[index]; ourDeck[index] = temp; } } return temp; } public int create(int i) { flush(ourDeck); int Mnumber = 0; int counter = 0; for (i = 0; i < Mnumber; i++) { String Suit = suits[ourDeck[i] / 13]; String Rank = rank[ourDeck[i] % 13]; counter++; System.out.println("Card number is " + ourDeck[i] + " : " + Rank + " of " + Suit); if (counter == Mnumber) { System.out.print("And there are " + (52 - Mnumber) + " cards left!"); } } return i; } }
public class Card { public static int[] ourDeck = new int[52]; public static void main(String[] args) { Deck objectDeck = new Deck(); objectDeck.flush(ourDeck); } }