Registrarse

Juego de Poker escrito en C

Estado
Cerrado para nuevas respuestas.

PabloGTD

WiiU developer
Buenas. Os dejo este código para un poker en C que acabo de rematar. Teoricamente debería funcionar, pero no he podido probarlo debido a que no sé por qué cojones no me funciona el compiladory no tengo ganas ahora de mirar a ver. Espero que os sirva para aprender el funcionamiento de un poker y quien sabe si lo mismo alguien le echa los suficientes huevos para hacerlo funcionar en GBA, aunque advierto que es bastante difícil esto último.

Código:
#include<stdio.h> 
#include<stdlib.h> 
#include<time.h> 
//Variables Globales 
int pokedolar = 288, bet, win = 0, lose = 0;
void shuffle(int [][13]);
void deal(int [][13], const char *[], const char *[]);
void bubble(int n[], int size); //ordenacion por burbuja

main() {

    while (pokedolar > 0) {
        system("color 4F");
        printf("*Esta jugando en Poker Azulona");
        printf("Ha comenzado el juego con 288 Pokedolares");
        srand(time(NULL));
        int i, j, mesa[4][13] = {0};
        srand(time(NULL));
    }
    
    const char *suit[4] = {"Picas", "Corazones", "Treboles", "Diamantes"};
    const char *faces[13] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; 

}
printf("Gracias por jugar en Poker Azulona");
getch();

// Para barajar, almacenamos en un array bidimensional de 4 filas y 13 columnas diferentes numeros aleatorios

void shuffle(int d[][13]) {
    int row, column, card;
    for (card = 1; card < 53; card++) {
        row = rand() % 4;
        column = rand() % 13;
        while (d[row][column] != 0) {
            row = rand() % 4;
            column = rand() % 13;
        }
        d[row][column] = card;
    }
} // el metodo deal repartirá las cartas ya barajadas

void deal(int d[][13], const char *s[], const char *f[]) { // realizamos declaraciones de variables y constantes que nos ayudaran a identificar nuestra mano en el juego 
    int deck2[4][13] = {0};
    int row, column, card, pnt = 0, i;
    int royalFlush(const int [][13]);
    int straightFlush(const int [][13]);
    int poker(const int [][13]);
    int fullHouse(const int [][13]);
    int flush(const int [][13]);
    int straight(const int [][13]);
    int threeOfAKind(const int [][13]);
    int twoPair(const int [][13]);
    int pair(const int [][13]);
    int (*pokerArray[9])(const int) = {royalFlush, straightFlush, poker, fullHouse, flush, straight, threeOfAKind, twoPair, pair};
    void point(int);
    int flag = 1, flag1 = 0, flag2 = 0;
    int apuesta = 0, apuesta1 = 0, apuesta2 = 0;
    int aux = 0, aux1 = 0, aux2 = 0, aux3 = 0;
    apuesta2 = 5;
    pokedolar = pokedolar - apuesta2;
    printf("Numero de Pokedolares=%dn", pokedolar);
    if (pokedolar > 999) apuesta2 = 7;
    if (pokedolar > 1999) apuesta2 = 8;
    printf("Apuesta minima=%dn", apuesta2);
    printf("Manos Ganadas=%dn", win);
    printf("Manos Perdidas=%dn", lose);
    printf("Tus cartas son");
    for (card = 1; card < 8; card++) {
        for (row = 0; row < 4; row++) {
            for (column = 0; column < 13; column++) {
                if (d[row][column] == card) {
                    if (card == 3) {
                        getch();
                        printf("Las cartas del dealer son");
                        getch();
                    }
                    if (card == 6) {
                        getch();
                        printf("Aumente su apuesta o pulsa 0 para pasar=");
                        while (flag < 2) {
                            scanf("%d", &bet);
                            if ((bet > pokedolar) || (bet <= 0)) {
                                if (bet > pokedolar) printf("Debes apostar una cantidad inferior a tus Pokedolares");
                                if (bet <= 0) printf("Debes introducir un número distinto de 0");
                            }
                            if ((bet < pokedolar) || (bet >= 0)) {
                                if (bet <= pokedolar) {
                                    if (bet >= 0) {
                                        aux = aux + bet;
                                        flag = 3;
                                        apuesta = apuesta + bet;
                                    }
                                }
                            } else {
                                flag1 = 3;
                                apuesta = apuesta + bet;
                            }
                        }
                    }
                    if (card == 7) {
                        printf("Aumente su apuesta o pulsa 0 para pasar");
                        while (flag1 < 2) {
                            scanf("%d", &bet);
                            aux2 = aux + bet;
                            printf("%d", bet);
                            if ((aux2 > pokedolar) || (bet < 0)) {
                                if (bet < 0) printf("introduzca un numero mayor que 0");
                                printf("%d", aux2);
                                if (aux2 <= 0) printf("Por favor introduzca un numero mayor que 0");
                                if (bet > pokedolar) printf("Debes apostar una cantidad inferior a tus Pokedolares");
                            } else flag1 = 3;
                            apuesta1 = apuesta1 + bet;
                        }
                    }
                    bet = apuesta + apuesta1 + apuesta2;
                    printf("%-6s de %-8s", f[column], s[row]);
                    deck2[row][column] = 1;
                }
            }
        }
    }
    for (i = 0; i < 9; i++) {
        pnt = (*pokerArray[i])(deck2);
        if (pnt != 0) break;
    }
    getch();
    point(pnt);
}

int royalFlush(const int d2[][13]) {
    int i, j, k = 0, cont = 0, card[5], royal[5] = {0, 9, 10, 11, 12};
    if (flush(d2) == 5) for (i = 0; i < 4; i++) {
            for (j = 0; j < 13; j++) {
                if (d2[i][j] == 1) {
                    card[k] = j;
                    k++;
                }
            }
        }
    bubble(card, 5);
    for (i = 0; i < 5; i++) if (card[i] == royal[i]) cont++;
    if (cont == 5) return 1;
    return 0;
}

int straightFlush(const int d2[][13]) {
    if (flush(d2) == 5) if (straight(d2) == 7) return 2;
    return 0;
}

int poker(const int d2[][13]) {
    int i, j, incr, cont;
    for (incr = 0; incr < 13; incr++) {
        cont = 0;
        for (i = 0; i < 4; i++) {
            for (j = incr; j <= incr; j++) {
                if (d2[i][j] == 1) cont++;
            }
        }
        if (cont == 4) return 3;
    }
    return 0;
}

int fullHouse(const int d2[][13]) {
    if (threeOfAKind(d2) == 7) if (pair(d2) == 9) return 4;
    return 0;
}

int flush(const int d2[][13]) {
    int i, j, cont;
    for (i = 0; i < 4; i++) {
        cont = 0;
        for (j = 0; j < 13; j++) {
            if (d2[i][j] == 1) cont++;
        }
        if (cont == 5) return 5;
    }
    return 0;
}

int straight(const int d2[][13]) {
    int i, j, k = 0, temp, num[7];
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 13; j++) {
            if (d2[i][j] == 1) {
                num[k] = j;
                k++;
            }
        }
    }
    bubble(num, 6);
    temp = num[0];
    for (i = 1; i < 5; i++) {
        if (num[i] != temp + 1) return 0;
        temp = num[i];
    }
    return 6;
}

int threeOfAKind(const int d2[][13]) {
    int i, j, incr, cont;
    for (incr = 0; incr < 13; incr++) {
        cont = 0;
        for (i = 0; i < 4; i++) {
            for (j = incr; j <= incr; j++) {
                if (d2[i][j] == 1) cont++;
            }
        }
        if (cont == 3) return 7;
    }
    return 0;
}

int twoPair(const int d2[][13]) {
    int i, j, incr, cont, pairCont = 0;
    for (incr = 0; incr < 13; incr++) {
        cont = 0;
        for (i = 0; i < 4; i++) {
            for (j = incr; j <= incr; j++) {
                if (d2[i][j] == 1) cont++;
            }
        }
        if (cont == 2) pairCont++;
    }
    if (pairCont == 2) return 8;
    return 0;
}

int pair(const int d2[][13]) {
    int i, j, incr, cont;
    for (incr = 0; incr < 13; incr++) {
        cont = 0;
        for (i = 0; i < 4; i++) {
            for (j = incr; j <= incr; j++) {
                if (d2[i][j] == 1) cont++;
            }
        }
        if (cont == 2) return 9;
    }
    return 0;
}

void point(int p) {
    char opcion = 'z';
    int resultado;
    printf("Tu mano es ");
    switch (p) {
        case 1: printf("ESCALERA REAL!!!!!!");
            bet = bet * 10;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 2: printf("Escalera de color");
            bet = bet * 8;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 3: printf("Poker");
            bet = bet * 7;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 4: printf("Full House");
            bet = bet * 6;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 5: printf("Color");
            bet = bet * 5;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 6: printf("Escalera");
            bet = bet * 4;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 7: printf("Trio");
            bet = bet * 3;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 8: printf("Dobles Parejas");
            bet = bet * 2;
            pokedolar = pokedolar + bet;
            win = win + 1;
            break;
        case 9: printf("Pareja");
            bet = bet * 1;
            pokedolar = pokedolar - bet;
            lose = lose + 1;
            break;
        default: printf("Carta Alta");
            bet = bet * 2;
            pokedolar = pokedolar - bet;
            lose = lose + 1;
            break;
    }
    printf("Ahora tienes %d Pokedolares", pokedolar);
    if (pokedolar > 0) printf("Pulse 0 si desea dejar de jugar o cualquier otra tecla para continuar");
    fflush(stdin);
    opcion = getchar();
    if (opcion == '0') {
        pokedolar = -1;
        system("cls");
        printf("Gracias por jugar en Poker Azulona");
    }
    getch();
}

void bubble(int n[], int size) {
    int i, j, temp;
    for (i = 0; i < size - 1; i++) {
        for (j = i + 1; j < size; j++) {
            if (n[i] > n[j]) {
                temp = n[i];
                n[i] = n[j];
                n[j] = temp;
            }
        }
    }
}
Saludos
 

cosarara97

Dejad de cambiar de nick
Miembro de honor
¿Teoricamente?
¿Qué clase de main() es ese? ¡Pero si no hace mas que tirar cosas a la consola! ¿Qué hacen ese printf y getchar fuera de toda función? ¿"color"? ¿Qué clase de aberración platform-dependent es esa? ¿Forward declarations dentro de una función?

(entonces cosarara recuerda eso que le decia su madre de no ser repelente)


Me da como que te falta un buen main, y que hay algún que otro error básico en tu código, además de no ser compatible con cosas no-windows. De todos modos, siempre está bien ver gente escribiendo en C.

Keep it up!
 

Pum

GBCero
Ugh, ordenación por burbuja... No pudiste intentar aplicar QuickSort? Lo digo porque usar un algoritmo de complejidad cuadrática O(n²) cuando puedes usar uno del orden O(n log n)...

A menos que se me escape algo y en este programa específico QuickSort siempre sería Peor Caso (También O(n²)).

Aún así está bastante interesante y le echaré una ojeada cuando pueda.
 

PabloGTD

WiiU developer
La verdad es que tenéis razón. Lo he hecho rápidamente y ni me he molestado en optimizarlo como Dios manda XD
 
Estado
Cerrado para nuevas respuestas.
Arriba