Question

In: Computer Science

Would you make separated this code by one .h file and two .c file? **********code************* #include...

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;
}

Solutions

Expert Solution

// 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:


Related Solutions

This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
IN C PROGRAMMING LINUX how to use makefile to include .a static library and .h file...
IN C PROGRAMMING LINUX how to use makefile to include .a static library and .h file from another directory in C? I have a header file myheader.h and a static library libmylib.a file in directory1. In directory2, I'm writing a program which uses them. Suppose I have main.c in directory2 which uses myheader.h and libmylib.a. How do I create a Makefile to compile and link them? LIBB = -L/../directory1/libmylib.a HEADER = -L/../directory1/myheader.h main: main.o gcc $(HEADER) $(LIBB) main.o: main.c gcc...
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argb, char *argv[]){ char ch; int upper=1; if(argb>1){ if(strcmp(argv[1],"-s")==0){ upper=1; } else if(strcmp(argv[1],"-w")==0){ upper=0; } } while((ch=(char)getchar())!=EOF){ if(upper){ printf("%c",toupper(ch)); } else{ printf("%c",tolower(ch)); } } return 0; } ======================================================== #include <stdio.h> #include <stdlib.h> int calcRange(int array[],int size){ int max=array[1]; int min=array[0]; int a=0; for(a=1;a<size;a++){ if(array[a]>max){ max=array[a]; } } for(a=1;a<size;a++){ if(array[a]<min){ min=array[a]; } } printf("%d-%d=%d\n",max,min,max-min); } int main(int arga, char **argv){...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
With the code below a) Perform two-pass hand assembly to produce the list file. b) Make...
With the code below a) Perform two-pass hand assembly to produce the list file. b) Make a list of all the labels and give the value assigned to each label c) Write the addressing mode and effective address or target address for branch instructions, for the first time each instruction executes ORG $0080 MaxVal: DS.B 1 ;# ROM Section ORG $FC00 ; Start: ldhx #DataX ; clr MaxVal ; Top: cphx #DataY ; beq Done ; lda 0,X ; cmp...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT