Uno Card Game - Can this be simpler?
Hello!
I'm trying to make a UNO Card Game, here are the rules if you do not know them: Uno (card game) - Wikipedia, the free encyclopedia
Anyway, i'm trying to build the basic structure of the card/deck to later be able to easily compare the card/plays with integers. It's all good, but i was wondering if anyone can give me a tip if i could make this simpler and, possibly, shorter somewhere:
Code:
#include <stdio.h>
int main()
{
enum Cards
{
Zero, One, Two, Tree, Four, Five, Six, Seven, Eight, Nine, Skip, Draw2, WildDraw4, Reverse, Wild
};
enum Values
{
Special=20, Joker=50
};
enum Colors
{
Blue, Red, Yellow, Green, Black
};
typedef struct {
enum Cards Card;
enum Values Value;
enum Colors Color;
} Card;
Card card[108];
int i, j;
// Initializes the cards from 0-9 of all colors
for(i=0;i<=39;i++)
{
for(j=0;i<=4;j++)
{
// Just ignore the wrong definition below...
card[i] = (Card){ 0, 20, j};
}
}
// Other FORs
return 0;
}
It looked good when i created the Enums, but when i got to the point of initializing the 108 cards deck, i was like "oh man!". I will have to do a bunch of FORs and some manual initialization.
Anyone more experienced with this type of problem has any hints for me?
Thanks in advance!
Why isn't the deck being shuffled?
I proceeded into developing a little bit more of the code, but the shuffle is not working. It's probably because I am making a double call do the definition code, but I couldn't fix it. Sorry if it's another language.
Main:
Code:
#include <stdio.h>
#include "baralho.h"
int main()
{
int i;
embaralhar();
printf("-----------------------------------\n");
for (i=0;i<TAMBAR;i++)
{
printf("%d : %d\n", i, baralho[i]);
}
return 0;
}
Functions:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "baralho.h"
void embaralhar()
{
int i, pos1, pos2;
unsigned char tmp;
srand(time(NULL));
for (i=0;i<TAMBAR;i++)
{
printf("%d\n", baralho[i]);
}
printf("--------------------\n");
for (i=0;i<TAMBAR;i++)
{
do
{
pos1 = rand () % (TAMBAR-1);
pos2 = rand () % (TAMBAR-1);
} while (pos1==pos2);
printf("pos1: %d\n", pos1);
printf("pos2: %d\n", pos2);
tmp=baralho[pos1];
baralho[pos1]=baralho[pos2];
baralho[pos2]=tmp;
}
printf("--------------------\n");
for (i=0;i<TAMBAR;i++)
{
printf("%d\n", baralho[i]);
}
}
Prototypes:
Code:
typedef unsigned char card_t;
#define COR_MASCARA 112U
#define COR_PRETO 0U
#define COR_VERMELHO 16U
#define COR_VERDE 32U
#define COR_AMARELO 48U
#define COR_AZUL 64U
#define RANK_MASCARA 15U
#define RANK_ZERO 0U
#define RANK_UM 1U
#define RANK_DOIS 2U
#define RANK_TRES 3U
#define RANK_QUATRO 4U
#define RANK_CINCO 5U
#define RANK_SEIS 6U
#define RANK_SETE 7U
#define RANK_OITO 8U
#define RANK_NOVE 9U
#define RANK_PULAR 10U
#define RANK_PESCARDOIS 11U
#define RANK_INVERTER 12U
#define RANK_CORINGA 13U
#define RANK_CORINGAQUATRO 14U
#define TAMBAR 108
static card_t baralho[TAMBAR] = {
COR_VERMELHO | RANK_ZERO,
COR_VERMELHO | RANK_UM, COR_VERMELHO | RANK_UM,
COR_VERMELHO | RANK_DOIS, COR_VERMELHO | RANK_DOIS,
COR_VERMELHO | RANK_TRES, COR_VERMELHO | RANK_TRES,
COR_VERMELHO | RANK_QUATRO, COR_VERMELHO | RANK_QUATRO,
COR_VERMELHO | RANK_CINCO, COR_VERMELHO | RANK_CINCO,
COR_VERMELHO | RANK_SEIS, COR_VERMELHO | RANK_SEIS,
COR_VERMELHO | RANK_SETE, COR_VERMELHO | RANK_SETE,
COR_VERMELHO | RANK_OITO, COR_VERMELHO | RANK_OITO,
COR_VERMELHO | RANK_NOVE, COR_VERMELHO | RANK_NOVE,
COR_VERMELHO | RANK_PULAR, COR_VERMELHO | RANK_PULAR,
COR_VERMELHO | RANK_PESCARDOIS, COR_VERMELHO | RANK_PESCARDOIS,
COR_VERMELHO | RANK_INVERTER, COR_VERMELHO | RANK_INVERTER,
COR_VERDE | RANK_ZERO,
COR_VERDE | RANK_UM, COR_VERDE | RANK_UM,
COR_VERDE | RANK_DOIS, COR_VERDE | RANK_DOIS,
COR_VERDE | RANK_TRES, COR_VERDE | RANK_TRES,
COR_VERDE | RANK_QUATRO, COR_VERDE | RANK_QUATRO,
COR_VERDE | RANK_CINCO, COR_VERDE | RANK_CINCO,
COR_VERDE | RANK_SEIS, COR_VERDE | RANK_SEIS,
COR_VERDE | RANK_SETE, COR_VERDE | RANK_SETE,
COR_VERDE | RANK_OITO, COR_VERDE | RANK_OITO,
COR_VERDE | RANK_NOVE, COR_VERDE | RANK_NOVE,
COR_VERDE | RANK_PULAR, COR_VERDE | RANK_PULAR,
COR_VERDE | RANK_PESCARDOIS, COR_VERDE | RANK_PESCARDOIS,
COR_VERDE | RANK_INVERTER, COR_VERDE | RANK_INVERTER,
COR_AMARELO | RANK_ZERO,
COR_AMARELO | RANK_UM, COR_AMARELO | RANK_UM,
COR_AMARELO | RANK_DOIS, COR_AMARELO | RANK_DOIS,
COR_AMARELO | RANK_TRES, COR_AMARELO | RANK_TRES,
COR_AMARELO | RANK_QUATRO, COR_AMARELO | RANK_QUATRO,
COR_AMARELO | RANK_CINCO, COR_AMARELO | RANK_CINCO,
COR_AMARELO | RANK_SEIS, COR_AMARELO | RANK_SEIS,
COR_AMARELO | RANK_SETE, COR_AMARELO | RANK_SETE,
COR_AMARELO | RANK_OITO, COR_AMARELO | RANK_OITO,
COR_AMARELO | RANK_NOVE, COR_AMARELO | RANK_NOVE,
COR_AMARELO | RANK_PULAR, COR_AMARELO | RANK_PULAR,
COR_AMARELO | RANK_PESCARDOIS, COR_AMARELO | RANK_PESCARDOIS,
COR_AMARELO | RANK_INVERTER, COR_AMARELO | RANK_INVERTER,
COR_AZUL | RANK_ZERO,
COR_AZUL | RANK_UM, COR_AZUL | RANK_UM,
COR_AZUL | RANK_DOIS, COR_AZUL | RANK_DOIS,
COR_AZUL | RANK_TRES, COR_AZUL | RANK_TRES,
COR_AZUL | RANK_QUATRO, COR_AZUL | RANK_QUATRO,
COR_AZUL | RANK_CINCO, COR_AZUL | RANK_CINCO,
COR_AZUL | RANK_SEIS, COR_AZUL | RANK_SEIS,
COR_AZUL | RANK_SETE, COR_AZUL | RANK_SETE,
COR_AZUL | RANK_OITO, COR_AZUL | RANK_OITO,
COR_AZUL | RANK_NOVE, COR_AZUL | RANK_NOVE,
COR_AZUL | RANK_PULAR, COR_AZUL | RANK_PULAR,
COR_AZUL | RANK_PESCARDOIS, COR_AZUL | RANK_PESCARDOIS,
COR_AZUL | RANK_INVERTER, COR_AZUL | RANK_INVERTER,
COR_PRETO | RANK_CORINGA,
COR_PRETO | RANK_CORINGA,
COR_PRETO | RANK_CORINGA,
COR_PRETO | RANK_CORINGA,
COR_PRETO | RANK_CORINGAQUATRO,
COR_PRETO | RANK_CORINGAQUATRO,
COR_PRETO | RANK_CORINGAQUATRO,
COR_PRETO | RANK_CORINGAQUATRO
};
#define RANK(carta) ((carta) & RANK_MASCARA)
#define COR(carta) ((carta) & COR_MASCARA)
void embaralhar();
Basically, I am including the prototypes file (baralho.h) into the main file (main.c) which includes the definition of the deck (baralho). But the problem is that when I call the function to shuffle (embaralhar()), the shuffling of the deck only happens inside the functions file, and when I try to call the deck on the main file, the deck is "reset". Why exactly is this happening?
Sorry for the code in another language. If you're unable to understand, I will translate it.
Thanks in advance!