Hi I recently started working with Java (this morning to be exact) and I'm running into some trouble. I'm trying to make a simple program that will put a deck of playing cards into an array, shuffle that array, then print the shuffled deck. My eventual goal is to make a fun little game of Texas Hold'em that the users of my website can play, but for now I'm just looking to get the cards shuffled.

My background is in C so I have written up this program in C if anyone wants to see kind of what I'm looking for in the program. I would like to be able to put this Java program up on my website (just to show my fellow staff members that I am making progress) so if you could write it out as an applet or point me to a good tutorial that will show me how to do that I would greatly appreciate it. Thanks in advance!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card {
  const char *face;
  const char *suit;
};

typedef struct card Card;

void fillDeck(Card * const wDeck, const char * wFace[], const char * wSuit[]);
void shuffle(Card *const wDeck);
void deal(const Card * const wDeck);

int main(void)
{
  Card deck[52];

  const char *face[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
  const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};

  srand(time(NULL));

  fillDeck(deck, face, suit);
  shuffle(deck);
  deal(deck);
  return 0;
}

void fillDeck(Card * const wDeck, const char * wFace[], const char * wSuit[])
{
  int i;
  for (i = 0; i <= 51; i++) {
    wDeck[i].face = wFace[i % 13];
    wDeck[i].suit = wSuit[i / 13];
  }
}

void shuffle(Card * const wDeck)
{
  int i, j;
  Card temp;

  for(i = 0; i <= 51; i++) {
    j = rand() % 52;
    temp = wDeck[i];
    wDeck[i] = wDeck[j];
    wDeck[j] = temp;
  }
}

void deal(const Card * const wDeck)
{
  int i;

  for (i = 0; i <= 51; i++) {
    printf("%5s of %-8s%s", wDeck[i].face, wDeck[i].suit, (i + 1) % 4 ? " " : "\n");
  }
}

Sorry about not including my code. I just got this to work, but now I want to be able to turn this into an applet. At the bottom is my sad attempt at making this into an applet that will print on a browser.

//DeckOfCards.java
import java.io.*;

public class DeckOfCards
{
  public static void main(String args[])
  {
    java.util.Random random=new java.util.Random();
    for (int i = 0; i < 100; i++) {
      int card1 = Math.abs(random.nextInt()) % 52;
      int card2 = Math.abs(random.nextInt()) % 52;

      String temp = cards.deck[card1];
      cards.deck[card1] = cards.deck[card2];
      cards.deck[card2] = temp;
    }

    for (int j = 0; j < 52; j++) {
      System.out.println(cards.deck[j]);
      if ((j+1)%13==0) {
        System.out.println();
      }
    }
  }
}





//cards.java
public class cards
{
  static String[] deck = {"Ace of Spades", "Deuce of Spades", "Three of Spades", "Four of Spades", "Five of Spades",
  "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", 
  "Queen of Spades", "King of Spades", "Ace of Clubs", "Deuce of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs",
  "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", 
  "Queen of Clubs", "King of Clubs", "Ace of Diamonds", "Deuce of Diamonds", "Three of Diamonds",
  "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds",
  "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds",
  "Ace of Hearts", "Deuce of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", 
  "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts",
  "Jack of Hearts", "Queen of Hearts", "King of Hearts"};
}





//DeckOfCards.java
import java.applet.*;
import java.awt.*;
import java.util.Random;

public class DeckOfCards extends Applet
{
  public void paint(Graphics g)
  {
    java.util.Random random=new java.util.Random();
    for (int i = 0; i < 100; i++) {
      int card1 = Math.abs(random.nextInt()) % 52;
      int card2 = Math.abs(random.nextInt()) % 52;

      String temp = start.deck[card1];
      start.deck[card1] = start.deck[card2];
      start.deck[card2] = temp;
    }

    for (int j = 0; j < 52; j++) {
      String k = start.deck[j];
      g.drawString(k + " ", 25, 50);
      /*if ((j+1)%13==0) {
        g.drawString();
      }*/
    }
  }
}

So you can see there's some pretty big differences between my C code and this Java. Also I know there's much better ways to write this program than the way I did it but go easy on me cause I am new to this

Can you explain what your problems are? What happens when you compile and execute the code?

The paint() method is for painting/drawing on the graphics. Unrelated logic(lines 59 to 67) should be elsewhere in the program.

Well this is what I see when I open it up in a browser

example4

To me it looks like everything is being printed at the same time in the same place

You need to change the x and/or y values used in the drawString() method to draw the Strings at different locations.

OK I get what you're saying but do you htink you could provide a little example for me? Thanks for the help so far!

int x = 25;
g.drawString(k + " ", x, 50);
x = x + 100;
g.drawString(k + " ", x, 50);  // draw second at another location

Just tried it out and it looks like it's still all jumbled up but now it's twice as long.

That code was an example for drawing one String at two different x locations. You will need to tailor it for your needs by changing the x and y values used by the drawString() method to be where you want the Strings drawn.

Ah OK thanks for bearing with me! I'll go and work on this some more. Thanks for your help

Good luck. I'm done for tonight. Back tomorrow.

Just went ahead and messed around with the code a bit and it seems to be working great now! Thank you so much for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.