Skip to content

Instantly share code, notes, and snippets.

@Scrivener07
Created May 2, 2017 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scrivener07/18eb294c5ca8d576ef39b33d381d8f78 to your computer and use it in GitHub Desktop.
Save Scrivener07/18eb294c5ca8d576ef39b33d381d8f78 to your computer and use it in GitHub Desktop.
A papyrus port of a simple blackjack game written in C. Original C code written by TheWhiteCollarPlayers.
/*
============================================================================
Name : HW.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function prototypes
void printintro(void); // printing the instructions and start options for the game
void printerror(void); //function to print error report
void playgame(void); //function to start black jack game
int main(void) {
//variable declarations
int input;
srand(time(NULL)); //seed random number generator
printintro(); //Display title and ask if user wants to play.
//take user input
fflush(stdin);
scanf("%d",&input);
//if statement to decide if game should start or to exit game, includes error checking
if (input==2){ //selected to quit, so exit program
printf("\n\nProgram terminated.\n\n\n\n");
fflush(stdout);
}
else if (input==1){ //chose to play, so start game
playgame(); //function starts game
}
else { //invalid input, terminate program
printerror(); //function prints error and terminates program
}
return EXIT_SUCCESS;
}
void playgame(void) //defining function for running the black jack game
{
//variable declarations
int card; //integer holds numeric value of "dealt" card
int total=0; //player total
int stand=0; //essentially a boolean which determines if the player wants to stand or not
int error=0; //essentially a boolean which determines if an error occured
int hitcheck; //essentially a boolean which determines if the player wants to hit or not
printf("\n\nGame started! Dealing cards!\n");
fflush(stdout);
card=(rand()%9+1); //gets a random number between 1 and 10
total=(total+card); //adds random card value to player total
while ((total<21) && (stand==0) && (error==0) ) { //while loop for dealing cards, runs while the player's total is less than 21, and while the player has not indicated to stand, and as long as the player has not entered invalid input
printf("You were dealt a %d\n",card); //displays the card value the player was "dealt"
fflush(stdout);
printf("Your total is now: %d\n",total); //displays the player's current total
fflush(stdout);
printf("Would you like to Hit (enter '1') or Stand (enter '2')?\n"); //asks to hit or stand
fflush(stdout);
fflush(stdin); //takes input
scanf("%d",&hitcheck);
if (hitcheck==1){ //user said hit, so get a card, add it, and continue loop
card=(rand()%9+1); //gets a random number between 1 and 10
total=(total+card); //adds random card value to player total
}
else if (hitcheck==2){ //user chose to stand, so stop loop
stand=1;
}
else { //invalid input, report error and quit
printf("\n\nError: Invalid input, please try again.\nProgram Terminated.\n");
fflush(stdout);
error=1;
}
}
//exited while loop, check if user busted, or they chose to stand, or they landed on 21
if (total==21){ //player has blackjack, report result
printf("You were dealt a %d\n",card);
fflush(stdout);
printf("Your final total is %d\n",total);
fflush(stdout);
printf("Congratulations! You have BlackJack! You Win!\nProgram terminated.\n");
fflush(stdout);
}
else if (total>21){ //player has busted, report result
printf("You were dealt a %d\n",card);
fflush(stdout);
printf("Your final total is %d\n",total);
fflush(stdout);
printf("Uh oh! You've busted! Better luck next time!\nProgram terminated.\n");
fflush(stdout);
}
else if ((total < 21) && (stand==1)){ //user chose to stand with less than 21
printf("\nYou have chosen to stand with %d\nProgram terminated.\n",total);
fflush(stdout);
}
else if (error==1){ //player inserted invalid input
printf("\nInvalid input. Please try again.\n Program terminated.\n");
fflush(stdout);
}
}
void printintro(void) //defining function for printing game instructions
{
printf("Welcome to Blackjack!\n");
fflush(stdout);
printf("The goal of this game is to get as close to 21 without going over, or 'busting'.\n");
fflush(stdout);
printf("Would you like to play?\n");
fflush(stdout);
printf("Type '1' for 'Yes'\n");
fflush(stdout);
printf("Type '2' for 'No'\n");
fflush(stdout);
printf("Your response: ");
fflush(stdout);
}
void printerror(void) //defining function for printing error report
{
printf("\n\nError! Invalid input!\n Program terminated.\n\n\n");
fflush(stdout);
}
ScriptName Gambling:BlackJack:MyScript extends ScriptObject
int Invalid = -1 const
int OptionStart = 0 const
int OptionExit = 1 const
int OptionHit = 0 const
int OptionStand = 1 const
; Events
;---------------------------------------------
Event OnInit()
int input = Invalid
; take user input
input = Gambling_BlackJack_MessageStart.Show()
; if statement to decide if game should start or to exit game, includes error checking
If (input == OptionExit)
; selected to quit, so exit program
Debug.MessageBox("Program terminated.");
ElseIf (input == OptionStart)
; chose to play, so start game
PlayGame() ; function starts game
Else
; invalid input, terminate program
PrintError() ; function prints error and terminates program
EndIf
EndEvent
; Functions
;---------------------------------------------
Function PlayGame()
int card ; integer holds numeric value of "dealt" card
int total = 0 ; player total
int stand = 0 ; essentially a boolean which determines if the player wants to stand or not
int error = 0 ; essentially a boolean which determines if an error occured
int hitcheck ; essentially a boolean which determines if the player wants to hit or not
Debug.MessageBox("Game started! Dealing cards!")
card = GetCard()
total = total + card ; adds random card value to player total
While (total < 21 && stand == 0 && error == 0)
; while loop for dealing cards, runs while the player's total is less than 21,
; and while the player has not indicated to stand,
; and as long as the player has not entered invalid input
hitcheck = Gambling_BlackJack_MessagePlayTurn.Show(card, total)
If (hitcheck == OptionHit)
; user said hit, so get a card, add it, and continue loop
card = GetCard() ; gets a random number between 1 and 10
total = (total + card) ; adds random card value to player total
ElseIf (hitcheck == OptionStand)
; user chose to stand, so stop loop
stand = 1
Else
; invalid input, report error and quit
PrintError()
error = 1
EndIf
EndWhile
; exited while loop, check if user busted, or they chose to stand, or they landed on 21
If (total == 21)
; player has blackjack, report result
Debug.MessageBox("You were dealt a "+card+" card.")
Debug.MessageBox("Your final total is "+total+".")
Debug.MessageBox("Congratulations! You have BlackJack! You Win!\nProgram terminated.\n");
ElseIf (total > 21)
; player has busted, report result
Debug.MessageBox("You were dealt a "+card+" card")
Debug.MessageBox("Your final total is "+total)
Debug.MessageBox("Uh oh! You've busted! Better luck next time!\nProgram terminated.\n")
ElseIf (total < 21 && stand == 1)
; user chose to stand with less than 21
Debug.MessageBox("You have chosen to stand with "+total+"\nProgram terminated.")
ElseIf (error == 1)
; player inserted invalid input
PrintError()
EndIf
EndFunction
int Function GetCard()
{gets a random number between 1 and 10}
return Utility.RandomInt(1, 10)
EndFunction
Function PrintError()
{defining function for printing error report}
Debug.MessageBox("Error! Invalid input!\n Program terminated.")
EndFunction
; Properties
;---------------------------------------------
Group Properties
Message Property Gambling_BlackJack_MessageStart Auto Const Mandatory
Message Property Gambling_BlackJack_MessagePlayTurn Auto Const Mandatory
EndGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment