Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * bj_main.c
- *
- * Copyright 2002 Marc Sylvestre <marc.sylvestre@manhydra.com>
- *
- * 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 3 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, see <http://www.gnu.org/licenses/>,
- * or if prefer good old fashion postal mail, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- /*
- * This is a Black Jack game I wrote when I was learning C (and C++, of which a version of this
- * exist). In it, you'll find examples of pointers, structs, and gambling addictions, lol.
- * Have fun!
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- typedef struct
- {
- char *playerName;
- int hand;
- int card;
- int bet;
- int loot;
- } bjPlayer;
- void Compare(bjPlayer*, bjPlayer*, int);
- void Game(bjPlayer*, bjPlayer*);
- void Compare(bjPlayer * bjp, bjPlayer * bjd, int additionalCards)
- {
- int play;
- printf(
- "\n===================================================="
- "\n========= %s: %d ============= %s: %d ============="
- "\n====================================================\n",
- bjp->playerName,bjp->hand,bjd->playerName,bjd->hand);
- if ((bjp->hand <= 21)&&(additionalCards==3))
- {
- printf("You have 5 cards without going over! You Win!\t");
- bjp->loot *= 3;
- printf("$%d\n\n",bjp->loot);
- }
- else if (bjp->hand > 21)
- {
- printf("\t\tYou Lose! \t");
- bjp->loot -= bjp->bet;
- printf("$%d\n\n",bjp->loot);
- }
- else if (bjd->hand > 21)
- {
- printf("\t\tYou Win! \t");
- bjp->loot += bjp->bet;
- printf("$%d\n\n",bjp->loot);
- }
- else if (bjp->hand > bjd->hand)
- {
- printf("\t\tYou Win! \t");
- bjp->loot += bjp->bet;
- printf("$%d\n\n",bjp->loot);
- }
- else if (bjp->hand < bjd->hand)
- {
- printf("\t\tYou Lose! \t");
- bjp->loot -= bjp->bet;
- printf("$%d\n\n",bjp->loot);
- }
- else printf("\t\tIt's a draw!\n\n");
- if (bjp->loot<=0)
- {
- printf("\t\tYou've lost all your winnings. Game over!\n");
- }
- else
- {
- printf("Press key \"1\" only to play again: ");
- scanf("%d",&play);
- if (play==1) Game(bjp,bjd);
- else printf("Thanks for playing!\n");
- }
- }
- void Game(bjPlayer * bjp, bjPlayer * bjd)
- {
- int deal, x;
- srand( (unsigned)time( NULL ) );
- bjp->hand = (rand() % 18)+4;
- bjd->hand = (rand() % 18)+4;
- for (x=0; x<3; x++)
- {
- bjp->card = (rand() % 11)+1;
- bjd->card = (rand() % 11)+1;
- if (bjd->hand < 17) bjd->hand += bjd->card;
- /* system("cls"); <-- See this? Yes, Even I was using this.
- This let's you know how old this game is, lol */
- printf(
- "\n===================================================="
- "\n============= %s, your hand reads: %d =============="
- "\n===================================================="
- "\n===================================================="
- "\n=============== Hit[1] or Stand[2]: ================"
- "\n===================================================="
- "\n>>>>>>>>>>>>>>>>>>>> ",bjp->playerName,bjp->hand);
- scanf("%d",&deal);
- if (deal==2)
- {
- break;
- Compare(bjp,bjd,x);
- }
- else bjp->hand += bjp->card;
- if (bjp->hand > 21)
- {
- break;
- Compare(bjp,bjd,x);
- }
- }
- Compare(bjp,bjd,x);
- }
- int main(int argc, char *argv[])
- {
- int antiup;
- char player_name[20];
- printf(
- "****************************************************\n"
- "*********** BLACK JACK ***********\n"
- "****************************************************\n"
- "****************************************************\n"
- "***** by Marc Sylvestre (manhydra@gmail.com) *******\n"
- "****************************************************\n"
- "======>>>> What's your name? ");
- scanf("%s", player_name);
- printf("Hello, %s\n",player_name);
- printf("======>>>> Place your bet: $");
- scanf("%d",&antiup);
- bjPlayer player;
- bjPlayer dealer;
- player.playerName = player_name;
- player.bet = antiup;
- dealer.playerName = "DEADLER";
- while(player.bet <= 0)
- {
- printf("\nPlease enter an amount greater than 0. ");
- scanf("%d",&antiup);
- player.bet = antiup;
- }
- printf("\n\nLet the games begin!\n\n");
- player.loot = player.bet;
- Game(&player,&dealer);
- /* system("pause"); <-- See this? Yes, Even I back then was
- using this. But only on Windows. */
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement