I try to implement the card game "Skat" in Java
The Rules:
There are 32 cards. Each card exists only once in the game.
The colors are: Clubs, Spades, Hearts, Diamonds
The values are: Ace, King, Queen, Jack, Ten, Nine, Eight, Seven
Those cards are shuffled. There are three players
First round: each player is given 3 cards. Then 2 cards are put into the Skat.
Second round: each player is given another 4 cards.
Third round: each player is given another 3 cards.
Desired output: the cards of each player revealed (every card should exist once)
My output: Diamonds Seven (32 times)
My code:
Class Card:
package skat;
public class Card
{
private static String color, value;
public static String getColor()
{
return color;
}
public static void setColor(String color)
{
Card.color = color;
}
public static String getValue()
{
return value;
}
public static void setValue(String value)
{
Card.value = value;
}
@Override
public String toString()
{
return color + " " + value;
}
}
Class Main:
package skat;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
Card[] game = new Card[32];
genCardDeck(game);
shuffleCardDeck(game);
Card[] player1 = new Card[10];
Card[] player2 = new Card[10];
Card[] player3 = new Card[10];
Card[] skat = new Card[2];
dealCards(game,player1,player2,player3,skat);
printCardDeck(game);
}
public static void genCardDeck(Card[] deck) //Generate the deck
{
final String colors[] = {"Clubs","Spades","Hearts","Diamonds"};
final String values[] = {"Ace","King","Queen","Jack","Ten","Nine","Eight","Seven"};
int i = 0;
for (String f : colors)
{
for (String w : values)
{
deck[i] = new Card();
deck[i].setColor(f);
deck[i].setValues(w);
i++;
}
}
}
public static void printCardDeck(Card[] deck) //Print out the deck
{
for (Card c : deck)
{
System.out.println(c);
}
}
public static void shuffleCardDeck(Card[] deck) //Shuffle the card deck
{
Random rand = new Random();
for (int i = 0; i < deck.length; i++) {
int randomNum = rand.nextInt(deck.length);
Card temp = deck[i];
deck[i] = deck[randomNum];
deck[randomNum] = temp;
}
}
public static void dealCards(Card[] deck, Card[] player1,
Card[] player2, Card[] player3, Card[] skat) //Deal the cards
{
//Round 1
int i;
for(i=0;i<9;i++)
{
if(i<3)
player1[i]=deck[i]; //Player 1
else if(i<6)
player2[i-3]=deck[i]; //Player 2
else
player3[i-6]=deck[i]; //Player 3
}
for(i=0;i<2;i++)
{
skat[i]=deck[i]; //Skat
}
//Round 2
for(i=0;i<12;i++)
{
if(i<4)
player1[i]=deck[i+3]; //Player 1
else if(i<8)
player2[i-4]=deck[i+3]; //Player 2
else
player3[i-8]=deck[i+3]; //Player 3
}
//Round 3
for(i=0;i<9;i++)
{
if(i<3)
player1[i]=deck[i+7]; //Player 1
else if(i<6)
player2[i-3]=deck[i+7]; //Player 2
else
player3[i-6]=deck[i+7]; //Player 3
}
}
}
dealCardscall in themainmethod. And your problem could be found if you debug the code. Knowing how to debug is a valuable skill for a developer; the problem is inshuffleCardDeckmethod. - KarelGcolorandvaluestatic inCard? Do you only have 1 color and value per JVM? - Jeroen Steenbeeke