Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // siebzehnundvier.c
- //
- // This program is free software; you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation; either version 2 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- // MA 02110-1301, USA.
- //
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define DECKSIZE 52
- #define NAMELENGTH 30
- #define MAIN 0
- #define DEALER 1
- #define PLAYER 2
- // bullshit
- #define FANCYSHIT "\n====================\n"
- struct deck {
- int count;
- int card[DECKSIZE];
- int type;
- char owner[NAMELENGTH];
- };
- int deckvalue(struct deck *target_deck) {
- int value = 0;
- for(int i = 0; i < target_deck->count; i++) {
- value += target_deck->card[i];
- }
- return value;
- }
- void showdeck(struct deck *target_deck) {
- if(target_deck->type == PLAYER) {
- printf("\nYour deck: ");
- for(int i = 0; i < target_deck->count; i++) {
- printf("%d ",target_deck->card[i]);
- }
- printf("\nHand value: %d\n",deckvalue(target_deck));
- }
- else if(target_deck->type == DEALER) {
- printf("Dealer's deck: ");
- for(int i = 0; i < target_deck->count; i++) {
- printf("%d ",target_deck->card[i]);
- }
- }
- else if(target_deck->type == MAIN) {
- printf("\n(DEBUG) Okay, so here's the full main deck: ");
- for(int i = 0; i < DECKSIZE; i++) {
- printf("%d ",target_deck->card[i]);
- }
- }
- else {
- printf("\n(ERROR) This is fucking impossible. Exiting ...\n");
- exit(EXIT_FAILURE);
- }
- }
- struct deck *createdeck(int type, struct deck *mdeck) {
- if(type == MAIN) {
- struct deck *maindeck;
- maindeck = malloc(sizeof(struct deck));
- for(int i = 0; i < DECKSIZE; i++) {
- maindeck->card[i] = rand () % (11 - 2 + 1) + 2; // (max - min + 1) + min
- }
- maindeck->count = 0;
- strcpy(maindeck->owner,"nobody");
- maindeck->type = MAIN;
- printf("\n(DEBUG) Created main deck\n");
- return maindeck;
- }
- else if(type == DEALER) {
- struct deck *dealerdeck;
- dealerdeck = malloc(sizeof(struct deck));
- // dealer's first card = third card in the main deck
- dealerdeck->card[0] = mdeck->card[2];
- dealerdeck->count = 1;
- // now the dealer has the third card, so set its value to 0
- mdeck->card[2] = 0;
- mdeck->count++;
- strcpy(dealerdeck->owner,"dealer");
- dealerdeck->type = DEALER;
- printf("\n(DEBUG) Created dealer's deck\n");
- return dealerdeck;
- }
- else if(type == PLAYER) {
- struct deck *playerdeck;
- playerdeck = malloc(sizeof(struct deck));
- // the player gets the first two cards out of the main deck
- memcpy(playerdeck->card,mdeck->card,2 * sizeof(int));
- playerdeck->count = 2;
- // now the player has the first two cards from the main deck, so set their value to 0
- mdeck->card[0] = 0;
- mdeck->card[1] = 0;
- mdeck->count += 2;
- playerdeck->type = PLAYER;
- strcpy(playerdeck->owner,"player");
- printf("\n(DEBUG) Created player's deck\n");
- return playerdeck;
- }
- else {
- printf("\n(ERROR) No deck type given, exit ...\n");
- exit(EXIT_FAILURE);
- }
- }
- void give(struct deck *target_deck,struct deck *maindeck) {
- if(maindeck->count < DECKSIZE - 1) {
- if(strcmp(target_deck->owner,"nobody") == 0) {
- printf("\n(ERROR) So, who's touching the main deck?\n");
- exit(EXIT_FAILURE);
- }
- else {
- target_deck->card[target_deck->count] = maindeck->card[maindeck->count];
- //printf("\n(DEBUG) Deck: %s; Card value: %d\n",target_deck->owner,target_deck->card[target_deck->count]);
- target_deck->count++;
- //printf("\n(DEBUG) Deck: %s; Number of cards before/after draw: %d/%d\n",target_deck->owner,target_deck->count - 1,target_deck->count);
- maindeck->card[maindeck->count] = 0;
- maindeck->count++;
- if(target_deck->type == PLAYER) {
- printf("You drew a card: %d. New hand value: %d\n",target_deck->card[target_deck->count - 1],deckvalue(target_deck));
- }
- else if(target_deck->type == DEALER) {
- printf("\nThe dealer drew a card: %d.\n",target_deck->card[target_deck->count - 1]);
- showdeck(target_deck);
- printf("\nDealer's new hand value: %d\n",deckvalue(target_deck));
- }
- else {
- printf("\n(ERROR) Unknown deck type: %d, exiting ...\n",target_deck->type);
- }
- }
- }
- else {
- printf("\n(ERROR) Main deck is ... emtpy?\n");
- exit(EXIT_FAILURE);
- }
- }
- void dealer(struct deck *dealerdeck, struct deck *playerdeck, struct deck *maindeck) {
- // I'LL WIN LOLOLO
- if(deckvalue(dealerdeck) < 21) {
- give(dealerdeck,maindeck);
- dealer(dealerdeck,playerdeck,maindeck);
- }
- else if(deckvalue(dealerdeck) == 21) {
- if(deckvalue(dealerdeck) > deckvalue(playerdeck)) {
- printf(FANCYSHIT);
- printf("Your hand value (%d) is less than the dealer's hand value (%d), you lose!\n",deckvalue(playerdeck),deckvalue(dealerdeck));
- showdeck(dealerdeck);
- exit(EXIT_SUCCESS);
- }
- }
- else {
- printf(FANCYSHIT);
- printf("fuck this shit I lost THE GAME ;_;\n");
- printf("The dealer's hand value (%d) ist greater than 21, you win!\n",deckvalue(dealerdeck));
- showdeck(dealerdeck);
- exit(EXIT_SUCCESS);
- }
- }
- void game(struct deck *dealerdeck, struct deck *playerdeck, struct deck *maindeck) {
- if(deckvalue(playerdeck) > 21) {
- printf(FANCYSHIT);
- printf("Your hand value (%d) is greater than 21, you lose!\n",deckvalue(playerdeck));
- showdeck(dealerdeck);
- printf("\nThe dealer's hand value is %d\n",deckvalue(dealerdeck));
- exit(EXIT_SUCCESS);
- }
- else {
- //printf("\n(press enter)\n");
- int choice;
- while ((choice = getchar()) != '\n' && choice != EOF);
- printf(FANCYSHIT);
- printf("Choose:\n");
- printf("\t(1) Show your deck\n\t(2) Draw a card\n\t(3) Finish drawing\n");
- printf(FANCYSHIT);
- char input[3];
- fgets(input,3,stdin);
- choice = (int) strtol(input,NULL,10);
- switch(choice) {
- case 1: showdeck(playerdeck);
- game(dealerdeck,playerdeck,maindeck);
- break;
- case 2: give(playerdeck,maindeck);
- game(dealerdeck,playerdeck,maindeck);
- break;
- case 3: printf("Dealer's first card: %d\n",dealerdeck->card[0]); // AI goes here :3
- dealer(dealerdeck,playerdeck,maindeck);
- break;
- default: printf("Something went wrong, please choose again!\n");
- game(dealerdeck,playerdeck,maindeck);
- break;
- }
- }
- }
- int main(void) {
- // random numbers
- srand((unsigned)time(NULL));
- struct deck *maindeck;
- struct deck *playerdeck;
- struct deck *dealerdeck;
- // create decks
- maindeck = createdeck(MAIN,NULL);
- playerdeck = createdeck(PLAYER,maindeck);
- showdeck(playerdeck);
- dealerdeck = createdeck(DEALER,maindeck);
- //showdeck(maindeck);
- // start the game :3
- game(dealerdeck,playerdeck,maindeck);
- // clean up (oh hai jerry)
- free(dealerdeck);
- free(playerdeck);
- free(maindeck);
- printf("THE END (actually you should never see this)\n");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement