In: Computer Science
Would you make separated this code by one .h file and two .c file?
**********code*************
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include<time.h>
// Prints out the rules of the game of "craps".
void print_game_rule(void)
{
printf("Rules of the game of CRAPS\n");
printf("--------------------------\n");
printf("A player rolls two dice.Each die has six faces.\n");
printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n");
printf("After the dice have come to rest, the sum of the spots\n on
the two upward faces is calculated.\n");
printf("If the sum is 7 or 11 on the first throw, the player
wins.\n");
printf("If the sum is 2, 3, or 12 on the first throw (called
craps), \nthe player loses(i.e.the house wins).\n");
printf("If the sum is 4, 5, 6, 8, 9, or 10 on the first throw,
\nthen the sum becomes the player's point.\n");
printf("To win, you must continue rolling the dice until you make
your point.\n");
printf("The player Loses by Rolling a 7 before making the
Point.\n\n");
}
//function definition of get_bank_balance()
double get_bank_balance(void)
{
//Prompt the player for an initial bank balance
printf("Enter initial bank balance: ");
double balance;
scanf("%lf", &balance);
return balance;
}
//function definition of get_wager_amount method
double get_wager_amount(void)
{
double wager;
//Prompt the player for a wager on a particular roll.
printf("Enter the wager: ");
scanf("%lf", &wager);
return wager;
}
//function definition of check_wager_amount method
int check_wager_amount(double wager, double balance)
{
// to check if the wager is within the limits of player's available
balance
if (wager > balance)
return 0;
else
return 1;
}
//function definition of roll_die method
int roll_die(void)
{
int random;
// to generate random number between 1 and 6
srand(time(0));
random = rand() % 6 + 1;
return random;
}
//function definition of calculate_sum_dice method
int calculate_sum_dice(int die1_value, int die2_value)
{
return die1_value + die2_value;
}
//function definition of is_win_loss_or method
int is_win_loss_or_point(int sum_dice)
{
// to check if the sum is 7 or 11 on the roll
if (sum_dice == 7 || sum_dice == 11)
return 1;
// to check if the sum is 2, 3, or 12
else if (sum_dice == 2 || sum_dice == 3 || sum_dice == 12)
return 0;
//if the sum is 4, 5, 6, 8, 9, or 10
else
return -1;
}
//function definition of is_point_loss_neither method
int is_point_loss_neither(int sum_dice, int point_value)
{
//to check if the sum of the roll is the point_value
if (sum_dice == point_value)
return 1;
//to check if the sum of the roll is a 7,
else if (sum_dice == 7)
return 0;
else
return -1;
}
//function definition of adjust_bank_balance method
double adjust_bank_balance(double bank_balance, double
wager_amount, int add_or_subtract)
{
// to check if add_or_subtract is 1
if (add_or_subtract == 1)
bank_balance += wager_amount;
// to check if add_or_subtract is 0,
else if (add_or_subtract == 0)
bank_balance -= wager_amount;
return bank_balance;
}
//function definition of chatter_message method
void chatter_message(int number_rolls, int win_loss_neither, double
initial_bank_balance,
double current_bank_balance)
{
printf("Number of Rolls: %d\n", number_rolls);
printf("Current Balance: %lf\n", current_bank_balance);
if (initial_bank_balance - current_bank_balance > 0)
printf("Amount lost: %lf\n", initial_bank_balance -
current_bank_balance);
else
printf(" Amount %lf\n", current_bank_balance -
initial_bank_balance);
if (win_loss_neither == 1)
printf("Congratulations!!! You WON\n");
else if (win_loss_neither == 0)
printf("Sorry!!! You LOSS\n");
else if (win_loss_neither == -1)
printf("Sorry!!! It is neither win or loss\n");
}
//main function
int main(void)
{
// variable declaration
double balance, initial_balance;
bool play = true;
int point, rollCount = 1;;
//call function print_game_rule to print the rules of the
game
print_game_rule();
//call function get_bank_balance()
balance = get_bank_balance();
initial_balance = balance;
while (play && balance > 0)
{
double wager = get_wager_amount();
// to check if wager is less than balance
if (!check_wager_amount(wager, balance))
{
printf("Insufficient balance.\n");
continue;
}
// set the dice values by calling function roll_die()
int dice1 = roll_die();
int dice2 = roll_die();
//calculate sum of dices by calling function
calculate_sum_dice()
int sum = calculate_sum_dice(dice1, dice2);
printf("Sum of dices: %d\n", sum);
if (rollCount == 1)
{
int check = is_win_loss_or_point(sum);
if (check == 1)
{
printf("Congratulations!!!You WON\n");
// call function adjust_bank_balance() to calculate new
balance
balance = adjust_bank_balance(balance, wager, 1);
// call function chatter_message() to display appropriate
messages
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else if (check == 0)
{
printf("Sorry!!! House wins.\n");
balance = adjust_bank_balance(balance, wager, 0);
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else
{
point = sum;
}
chatter_message(rollCount, check, initial_balance, balance);
}
else
{
int check = is_point_loss_neither(sum, point);
if (check == 1)
{
printf("Congratulations!!! You WON\n");
balance = adjust_bank_balance(balance, wager, 1);
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else if (check == 0)
{
printf("Sorry!!! House wins.\n");
balance = adjust_bank_balance(balance, wager, 1);
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else
{
point = sum;
}
chatter_message(rollCount, check, initial_balance, balance);
}
}
return 0;
}
// except main all function definition are place in header file
game.h:
// Prints out the rules of the game of "craps".
void print_game_rule(void){
printf("Rules of the game of CRAPS\n");
printf("--------------------------\n");
printf("A player rolls two dice.Each die has six faces.\n");
printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n");
printf("After the dice have come to rest, the sum of the spots\n on
the two upward faces is calculated.\n");
printf("If the sum is 7 or 11 on the first throw, the player
wins.\n");
printf("If the sum is 2, 3, or 12 on the first throw (called
craps), \nthe player loses(i.e.the house wins).\n");
printf("If the sum is 4, 5, 6, 8, 9, or 10 on the first throw,
\nthen the sum becomes the player's point.\n");
printf("To win, you must continue rolling the dice until you make
your point.\n");
printf("The player Loses by Rolling a 7 before making the
Point.\n\n");
}
//function definition of get_bank_balance()
double get_bank_balance(void){
//Prompt the player for an initial bank balance
printf("Enter initial bank balance: ");
double balance;
scanf("%lf", &balance);
return balance;
}
//function definition of get_wager_amount method
double get_wager_amount(void){
double wager;
//Prompt the player for a wager on a particular roll.
printf("Enter the wager: ");
scanf("%lf", &wager);
return wager;
}
//function definition of check_wager_amount method
int check_wager_amount(double wager, double balance){
// to check if the wager is within the limits of player's available
balance
if (wager > balance)
return 0;
else
return 1;
}
//function definition of roll_die method
int roll_die(void){
int random;
// to generate random number between 1 and 6
srand(time(0));
random = rand() % 6 + 1;
return random;
}
//function definition of calculate_sum_dice method
int calculate_sum_dice(int die1_value, int die2_value){
return die1_value + die2_value;
}
//function definition of is_win_loss_or method
int is_win_loss_or_point(int sum_dice){
// to check if the sum is 7 or 11 on the roll
if (sum_dice == 7 || sum_dice == 11)
return 1;
// to check if the sum is 2, 3, or 12
else if (sum_dice == 2 || sum_dice == 3 || sum_dice == 12)
return 0;
//if the sum is 4, 5, 6, 8, 9, or 10
else
return -1;
}
//function definition of is_point_loss_neither method
int is_point_loss_neither(int sum_dice, int point_value){
//to check if the sum of the roll is the point_value
if (sum_dice == point_value)
return 1;
//to check if the sum of the roll is a 7,
else if (sum_dice == 7)
return 0;
else
return -1;
}
//function definition of adjust_bank_balance method
double adjust_bank_balance(double bank_balance, double
wager_amount, int add_or_subtract){
// to check if add_or_subtract is 1
if (add_or_subtract == 1)
bank_balance += wager_amount;
// to check if add_or_subtract is 0,
else if (add_or_subtract == 0)
bank_balance -= wager_amount;
return bank_balance;
}
//function definition of chatter_message method
void chatter_message(int number_rolls, int win_loss_neither, double
initial_bank_balance,
double current_bank_balance){
printf("Number of Rolls: %d\n", number_rolls);
printf("Current Balance: %lf\n", current_bank_balance);
if (initial_bank_balance - current_bank_balance > 0)
printf("Amount lost: %lf\n", initial_bank_balance -
current_bank_balance);
else
printf(" Amount %lf\n", current_bank_balance -
initial_bank_balance);
if (win_loss_neither == 1)
printf("Congratulations!!! You WON\n");
else if (win_loss_neither == 0)
printf("Sorry!!! You LOSS\n");
else if (win_loss_neither == -1)
printf("Sorry!!! It is neither win or loss\n");
}
main.c:
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include<time.h>
#include"game.h"
//main function
int main(void){
// variable declaration
double balance, initial_balance;
bool play = true;
int point, rollCount = 1;;
//call function print_game_rule to print the rules of the
game
print_game_rule();
//call function get_bank_balance()
balance = get_bank_balance();
initial_balance = balance;
while (play && balance > 0){
double wager = get_wager_amount();
// to check if wager is less than balance
if (!check_wager_amount(wager, balance)){
printf("Insufficient balance.\n");
continue;
}
// set the dice values by calling function roll_die()
int dice1 = roll_die();
int dice2 = roll_die();
//calculate sum of dices by calling function
calculate_sum_dice()
int sum = calculate_sum_dice(dice1, dice2);
printf("Sum of dices: %d\n", sum);
if (rollCount == 1){
int check = is_win_loss_or_point(sum);
if (check == 1){
printf("Congratulations!!!You WON\n");
// call function adjust_bank_balance() to calculate new
balance
balance = adjust_bank_balance(balance, wager, 1);
// call function chatter_message() to display appropriate
messages
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else if (check == 0){
printf("Sorry!!! House wins.\n");
balance = adjust_bank_balance(balance, wager, 0);
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else{
point = sum;
}
chatter_message(rollCount, check, initial_balance, balance);
}
else{
int check = is_point_loss_neither(sum, point);
if (check == 1){
printf("Congratulations!!! You WON\n");
balance = adjust_bank_balance(balance, wager, 1);
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else if (check == 0){
printf("Sorry!!! House wins.\n");
balance = adjust_bank_balance(balance, wager, 1);
chatter_message(rollCount, check, initial_balance, balance);
break;
}
else{
point = sum;
}
chatter_message(rollCount, check, initial_balance, balance);
}
}
return 0;
}
after sepearting into .h and .c files
output: