Anunţ

Caută printre mesajele de pe forum înainte de a scrie unul nou!
Formulează corect întrebarea sau problema pe care o ai.
Respectă regulile forumului și Codul de Conduită!

#1 21 Mar 2015 11:08:37

TARA24.
Membru
Înregistrat: 23 Dec 2010
Mesaje: 968

Joc BlackJack

Salutare tuturor,

am facut un joc in C de tip BlackJack pentru Consola momentan urmeaza sa fie scris in GTK+.
Momentan este in stadiul BETA.
Daca este cineva interesat sa de-a un feedback in cazul in care gaseste erori, il rog sa posteze aici:

Cod:

nano blackjack.c

si introduceti codul de mai jos:
.

Cod:

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

#define BLACKJACK 21
#define GAMEPOINTS 3
#define TRUE 1
#define FALSE 0
#define CLEAN 0

#define KRED  "\x1B[31m"
#define KGRN  "\x1B[32m"
#define KYEL  "\x1B[33m"
#define KMAG  "\x1B[35m"
#define KCYN  "\x1B[36m"
#define RESET "\033[0m"

void clear_stdin(){
    int c;
    while ((c = getchar()) != 0 && c != '\n');
}

struct Scores{
    int start;
    int roundScore;
    int newScores;
    int newRecord;
    int rounds;
}record;

struct Players{
    char playerName[20];
    int year;
}player;

void setUpGameStats(){
    record.start = 0;
    record.rounds = 0;
}

void gameRules(){
    printf("\tlet me explain you aboud this game and how it works:\n\n");
    printf("\tTo win this game, you have 2 chanses:\n");
    printf("\tfirst chanse:  make BLACKJACK in the first Round\n");
    printf("\tsecond chanse: reset your Score to 0\n\n\n");

    printf("\tIf you make BLACKJACK after the second Round begins, your score will reset to 0 \n\n\n");

    printf("\tyou will loose if you don't make BlackJack in 10 Rounds\n");
    printf("\tYou will have to reset your score to 0 Points\n\n\n");

    printf("\tIf you decide to STOP at 17, you will make 12 Points\n");
    printf("\t12 points means, 21 - 17 = 4\n\n");
    printf("\t4 * 3 = 12\n\n");

    printf("\tIf you got 100, you lost the game\n\n");
    printf("\n\n\n\n");
    printf(KCYN "Please hit" RESET KYEL" \"ENTER\" " RESET KCYN"to continue:"RESET);
    getchar();
}

int checkInput();

void showPlayerName(){
    printf("%s\n",player.playerName);
}

int checkInput(int min, int max){
    int option;
    char c;

    do{
        printf(KCYN"\nPlease choose an Option between" RESET KYEL " %d " RESET KCYN "and " RESET KYEL "%d" RESET KCYN " >\t" RESET,min,max);

        if (scanf("%d%c",&option,&c) == 0 || c != '\n'){
            clear_stdin();
            printf("\nPlease type a Number:\n\n\n");
        }else if (option < min || option > max){
            printf("The number has to be between %d and %d\n\n\n",min,max);
        }else{
            printf("\n\n");
            break;
        }
    }while(TRUE);

    return option;
}

int newCard(int min, int max){
    int card;
    srand(time(NULL));
    sleep(2);
    card = (rand() % min) + max;

    return card;
}

int roundStart(){

    int firstCard, secondCard, score;
    firstCard  = newCard(11,1);
        printf(KGRN "The first card is:" RESET  "\t%d\n",firstCard);

    sleep(1);
    secondCard = newCard(10,1);
        printf(KGRN "The Second card is:" RESET  "\t%d\n",secondCard);

    score = firstCard + secondCard;
    return score;
}

void showScore(char name[], int score){
    sleep(1);
    printf("\n\n");
    printf("«««««««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\n");
    printf("\n\n");
    printf(KYEL "\t\t%s\t" RESET KCYN "your score is now:\t"RESET KYEL "%d" RESET "\n", name, score);
    printf("\n\n");
    printf("«««««««««««««««««««««««««««««««««««««««»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»\n");
    sleep(1);
    printf("\n\n");
}

void checkRound(int min, int max){
    if (min < max){
        do{
            int b;
            printf("\n\n\n");
            printf(KCYN "\t\tDo you need a new Card?" RESET "\n");
            printf("1) Yes >\n");
            printf("2) No >\n");
            printf("\n");

            b = checkInput(1,2);

            if (b == 1){
                int a = newCard(11,1);
                    system("clear");
                    system("clear");
                    system("clear");
                    printf("\t\t ________________________\n");
                    printf("\t\t| You choosed a new Card |\n");
                    printf("\t\t|________________________|");
                    printf("\n\n");
                    printf(KGRN "Your New Card is:" RESET  "\t%d\n",a);
                    record.roundScore += a;
                    printf("\n\n\n\n");
            }else if(b == 2){
                break;
            }

            showScore(player.playerName, record.roundScore);

                if (record.roundScore == max){
                        if(record.newRecord != CLEAN){
                            record.newRecord +=1;
                            break;
                        }
                    break;
                }else if (record.roundScore > max){
                    record.newRecord = 0;
                    break;
                }
        }while(record.roundScore != max || record.roundScore < max);
    }
}

int pointsCheck(int min, int max){
    int score;
    if(min > max){
        score = min - max;
    }else{
        score = max - min;
    }
    return score * GAMEPOINTS;
}

void gameOver(){
    printf("\n\n\t\t\t»»»» -= Game Over =- ««««\n\n");
}

void goodBye(){
    printf(KCYN "\n\n\t\t\t-=GoodBye=-\n\n" RESET);
}

int main(){
    int startGame;
    system("clear");
    setUpGameStats();

    do{
        int checkName;
        char c;

        if (record.start == 0){
            do{

                printf(KCYN "\tPlease type your NickName: \t" RESET  "");
                scanf("%s",player.playerName);
                clear_stdin();

                    if ((checkName = strtol(player.playerName, NULL, 10))){
                        printf(KRED "\n\n\t\tPlease type a Real NickName\n" RESET  "\n");
                    }

        }while(checkName != FALSE);

                    do{
                        printf(KCYN "\tPlease type your Age:\t" RESET  "");
                            if (scanf("%d%c",&player.year,&c) == 0 || c != '\n'){
                                clear_stdin();
                                printf(KRED "\n\n\t\tPlease type your real Age\n" RESET  "\n");
                             }else if (player.year > 75 && player.year < 115){
                                    printf("\n\n\n\t\t\tYou are to old for this game LoL\n");
                                    printf("\t\t\t\tAre you still Alive?\n\n");
                                    goodBye();
                                    return 1;
                            }else if (player.year >= 115){
                                    printf("\n\n\n\t\t\tAlliens and Predators are not alowed in here.\n");
                                    goodBye();
                                    return 1;
                            }else if(player.year < 18){
                                printf(KRED "\n\n\n\t\tSorry, you are to Young for this Game\n" RESET);
                                goodBye();
                                return 1;
                            }else{
                                break;
                            }
                    }while(TRUE);

                printf(KCYN "\n\n\n\t\t\t\tHallo" RESET KYEL" %s " RESET KCYN",welcome to BlackJack\n\n\n\n"RESET,player.playerName);
                        gameRules();

    }else if(record.start != 0){
        do{
            int b;

                if (record.rounds != 0){
                    if(record.rounds == 10 && record.roundScore != BLACKJACK ){
                        printf("\n\n\tYou lost %d Rounds\n",record.rounds);
                        gameOver();
                        return 1;
                    }

                    printf("\n\n\n\n");
                    printf(KYEL "Do you wanna play another Round?" RESET "\n");
                    printf("\t\t\t\t1) Yes >\n");
                    printf("\t\t\t\t2) No  >\n");
                    printf("\n\n\n");
                }else{
                    printf("\n\n\n\n");
                    printf(KYEL "\t\t\tDo you wanna play Again?" RESET "\n");
                    printf("1) Yes >\n");
                    printf("2) No  >\n");
                    printf("\n");
                }

                b = checkInput(1,2);

                if ( b == 2){
                    printf("Goodbye.");
                    return 1;
                }
        }while(FALSE);
    }
        printf("\n\n");

        system("clear");

        printf("\n\t\t\t\tRound %d\n\n",record.rounds+=1);
            if(record.rounds == 8){
                printf("\n\n\t\t\t   Hurry up, you are in Round %d\n\n\n",record.rounds);
            }else if(record.rounds == 9){
                printf("\n\n\t\t\tHurry up, you are in Round %d\n\n\n",record.rounds);
            }else if(record.rounds == 10){
                printf("\n\n\t\t\tYou are in Round %d, you have to Win\n\n\n",record.rounds);
            }

        printf(KYEL "\t\t%s\t" RESET KCYN "you get your first two Cards\n\n" RESET,player.playerName);
        sleep(3);
        startGame = roundStart();
        record.roundScore = startGame;



        showScore(player.playerName, record.roundScore);

        checkRound(record.roundScore, BLACKJACK);

        if(record.roundScore < BLACKJACK){
            int a = pointsCheck(record.roundScore, BLACKJACK);
            record.newScores += a;
            printf(KMAG"\n\n\t\t\t\tTotal points are now" RESET KYEL"\t%d\n"RESET,record.newScores);
        }else if(record.roundScore == BLACKJACK){
            if (record.newScores == 0){
                printf("\n\n\n\t\t\t\tBlackJack\n");
                        printf("\n\n\t\t\t-=You are the Winner=-\n");
                        return 1;
            }else if(record.newScores != CLEAN){
                record.newScores = CLEAN;
                printf("You have just reseted your Score, you have now %d Points\n", record.newScores);
                record.rounds = 0;
            }
        }else if(record.roundScore > BLACKJACK){
            int a = pointsCheck(record.roundScore, BLACKJACK);
            record.newScores += a;
            printf(KMAG"\n\n\t\t\t\tTotal points are now" RESET KYEL"\t%d\n"RESET,record.newScores);
            printf("\n\n");
            printf("\t\t\t\t\t\t" KRED "You lost this Round.\n"RESET);
            sleep(3);
        }

        if (record.newScores > 100){
            printf("\n\n\n");
            printf("You lost this game\n");
            gameOver();
            return 1;
        }
        printf("\n\n\n");
        record.start = 1;
    }while(TRUE);

    printf("\n\n");

    return 0;
}

.
se compileaza cu:

Cod:

gcc -o blackjack  -W -Wall -Wextra -pedantic -Werror blackjack.c

.
Apoi rulati cu:

Cod:

./blackjack

.
Toate bune.

Editat ultima oară de TARA24. (21 Mar 2015 11:51:01)


Orice decizie pe care esti pe cale sa o iei, gandeste-o de cel putin 3 ori.
Din momentul in care ai luat-o nu mai conteaza de cate ori ai gandit-o.
Fi destept, foloseste ce ti se potriveste nu ce ti se impune.

Offline

 

#2 21 Mar 2015 11:21:15

Alexandru.Cucu
Administrator
Locaţie: -21.8021247, -154.6975949
Înregistrat: 17 Jun 2008
Mesaje: 6682
Site web

Re: Joc BlackJack

Să îți vând un pont: dacă vrei să partajezi mai frumos codul sursă poți folosi gist.github.com.

Ai în dreapta butonul „raw” care iți dă direct fișierul fără să mai iei cu copy-paste.
https://gist.github.com/anonymous/c9244365a7e5b0550b2c

Offline

 

#3 21 Mar 2015 11:31:22

TARA24.
Membru
Înregistrat: 23 Dec 2010
Mesaje: 968

Re: Joc BlackJack

Am luat la cunostinta smile).
.
Ai apucat deja sa pui codul si are un mic defect...eu ca sa il testez setasem #define GAMEPOINTS 15 si trebuie sa fie  #define GAMEPOINTS 3, apoi observasem ca uitasem sa remodific.
.
Pana am reeditat codul..ai postat tu smile).
Daca cineva i-a codul din postul lui @Alex..sa  se asigure ca este #define GAMEPOINTS 3
Toate bune.
.
.
L.E:
Am descoperit un bug singur la tastarea Varstei...daca se tasta o litera sau in sir de caractere in loc de cifre.
Acum Codul este ok.
Cine a apucat sa ia Jocul inainte de acest FIX il rog sa il ia din nou.
.
Toate Bune.

Editat ultima oară de TARA24. (21 Mar 2015 11:52:22)


Orice decizie pe care esti pe cale sa o iei, gandeste-o de cel putin 3 ori.
Din momentul in care ai luat-o nu mai conteaza de cate ori ai gandit-o.
Fi destept, foloseste ce ti se potriveste nu ce ti se impune.

Offline

 

#4 21 Mar 2015 12:05:33

Alexandru.Cucu
Administrator
Locaţie: -21.8021247, -154.6975949
Înregistrat: 17 Jun 2008
Mesaje: 6682
Site web

Re: Joc BlackJack

Poti sa ii faci fork si sa modifici -> https://gist.github.com/cracknel/2a64ec … 55cfb76829
Astfel poti urmari si modificarile in timp:
https://gist.github.com/cracknel/2a64ec … /revisions

Bine, ideal ar fi sa tii un repository. Gist e mai mult pentru chestii temporare.
Cand am timp, scriu un mic ghid smile

Offline

 

#5 21 Mar 2015 12:18:21

TARA24.
Membru
Înregistrat: 23 Dec 2010
Mesaje: 968

Re: Joc BlackJack

@Alex  e bine ?
Nu am avut pana acum de-aface cu gitHub


Orice decizie pe care esti pe cale sa o iei, gandeste-o de cel putin 3 ori.
Din momentul in care ai luat-o nu mai conteaza de cate ori ai gandit-o.
Fi destept, foloseste ce ti se potriveste nu ce ti se impune.

Offline

 
Feed

Antet forum

Powered by FluxBB