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");
}
}