Jump to content

Jogo da Sueca em C


PleaseCheese

Recommended Posts

Estou a fazer um jogo de cartas da sueca e já consegui mostrar o baralho, "partir" e mostrar a mão de cada player o meu problema está em organizar as cartas de acordo com o naipe e a grandeza e fazer com que eles "joguem" a carta e assistam a jogada anterior.

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

#define DECK_NSUITS     4
#define DECK_NVALUES    10
#define DECK_SIZE       (DECK_NSUITS * DECK_NVALUES)

#define HAND_COUNT      4
#define HAND_SIZE       (DECK_SIZE / HAND_COUNT)

#define card_suit(card) ((card >> 8) & 0xff)
#define card_value(card) (card & 0xff)
#define card_compose(suit, value) (((suit & 0xff) << 8) | (value & 0xff))
#define card_swap(card1, card2) do { if (*card1 != *card2) { *card1 ^= *card2; *card2 ^= *card1; *card1 ^= *card2; } } while (0)

void deck_build(short * deck) {
    int suit = 0, value = 0;

    for (suit = 1; suit <= DECK_NSUITS; suit++)
        for (value = 1; value <= DECK_NVALUES; value++)
            deck[(suit - 1) * DECK_NVALUES + value - 1] = card_compose(suit, value);
}

void card_output(short card) {
    switch (card_suit(card)) {
        case 1 : printf("%c", 3); break;
        case 2 : printf("%c", 4); break;
        case 3 : printf("%c", 5); break;
        case 4 : printf("%c", 6); break;
    }
    switch (card_value(card)) {
        case 1  : printf("A "); break;
        case 8 : printf("Q "); break;
        case 9 : printf("J "); break;
        case 10 : printf("K "); break;
        default : printf("%-2d", card_value(card));
    }
}

void deck_shuffle(short * deck) {
    int i = 0, r = 0;

    for (i = 0; i < DECK_SIZE; i++) {
        r = rand() % (DECK_SIZE - i);
        card_swap(&deck[r], &deck[i + r]);
    }
}


int main() {
    short deck[DECK_SIZE];
    short hands[HAND_COUNT][HAND_SIZE];
    int i, j;

    /* start randon number genereation */
    srand(time(NULL));
    
    /* deck build */
    deck_build(deck);

    /* deck destribution to the players hands */
    for (i = 0; i < HAND_COUNT; i++)
    memcpy(hands[i], &deck[i * HAND_SIZE], HAND_SIZE * sizeof(short));

    /* hand presentation */
    printf("BARALHO:\n");
    for (i = 0; i < HAND_COUNT; i++) {
        for (j = 0; j < HAND_SIZE; j++) {
            card_output(hands[i][j]);
        }
        printf("\n");
    }
    printf("\n");

    /* deck shuffle */
    deck_shuffle(deck);

    /* deck destribution to the players hands */
    for (i = 0; i < HAND_COUNT; i++)
    memcpy(hands[i], &deck[i * HAND_SIZE], HAND_SIZE * sizeof(short));

    
    /* player que baralha */
    int num = (rand() %
    (4)) + 1;
    printf("player%d baralha:\n", num);

    /* hand presentation */
    for (i = 0; i < HAND_COUNT; i++) {
        for (j = 0; j < HAND_SIZE; j++) {
            card_output(hands[i][j]);
        }
        printf("\n");
    }
   
    printf("\n");printf("\n");
    
    /* parte a hand */
    if(num == 4){
        printf("player1 parte:\n");
    }else{
        printf("player%d parte:\n", num+1);
    }
    
    for (i = 0; i < HAND_COUNT; i++) {
        for (j = 0; j < HAND_SIZE; j++) {
            card_output(hands[i][j+1]);
        }
        printf("\n");
    }

    printf("\n");printf("\n");


    /* dá a hand */
    if(num+1==4){
      printf("player1 da:\n");
    }
    else if(num+1==5){
      printf("player2 da:\n");
    }else{
        printf("player%d da:\n", num+2);
    }

    for (i = 0; i < HAND_COUNT; i++) {
        printf("player%d: ", i+1);
        for (j = 0; j < HAND_SIZE; j++) {
            card_output(hands[i][j+1]);
        }
        printf("\n");
    }
    
    return 0;
}

Uma ideia do resultado final

sueca.png.814fe764bc001adb1a5d34dce39d98

Também já arranjei muito código daqui mas fui alterando para o que precisava

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.