Hogy-Bear

Q6

Feb 8th, 2018
281
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. // made by :  Eoghan Hogan
  2. //             17335293
  3. //
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. //function prototypes
  9. int biggestNumber(int a, int b);
  10. int rollDice(void);
  11. int getState(int num, int playerNum);
  12.  
  13.  
  14. int main(int argc, char const *argv[]) {
  15.  
  16.   srand(time(NULL)); //initiallize srand function
  17.  
  18.   int gameState = 1; //0 is lost 1 is playing and 2 is won
  19.   int numberToBeat = biggestNumber(rollDice(), rollDice()); //get a number for the player to beat.
  20.   int playersRoll;
  21.  
  22.   while (gameState == 1) { //while the player is still playing
  23.  
  24.     printf("the number to beat is %d\n", numberToBeat);
  25.     printf("hit enter to roll two dice\n");
  26.     while ( getchar() != '\n' ); //look for the player to press enter
  27.     playersRoll = biggestNumber(rollDice(), rollDice()); //generate a roll for the player
  28.     printf("you rolled %d\n", playersRoll);
  29.     gameState = getState(numberToBeat, playersRoll); //get what state the game should be in now.
  30.  
  31.   }
  32.  
  33.   if (gameState == 2) {
  34.     return 1;
  35.   } else {
  36.     return 0;
  37.   }
  38.  
  39. }
  40.  
  41. int rollDice(void){
  42.   return ( 1 + ( rand() % 6 ) ); //return a dice number
  43. }
  44.  
  45. int biggestNumber(int a, int b){ //get the biggest number from two iputted dice numbers
  46.   int pos1 = ( 10 * a ) + b;
  47.   int pos2 = ( 10 * b ) + a;
  48.   //return which ever is the biggest
  49.   if ( pos1 > pos2 ) {
  50.     return pos1;
  51.   } else {
  52.     return pos2;
  53.   }
  54. }
  55.  
  56. int getState(int num, int playerNum){ //decide the games state based on the numbers
  57.   //return a number based on the players roll and the first roll.
  58.   if ( num > playerNum ) {
  59.     printf("%d is bigger than %d, you lose!\n", num, playerNum);
  60.     return 0;
  61.   } else if( playerNum > num ) {
  62.     printf("%d is bigger than %d, you win!\n", playerNum, num);
  63.     return 2;
  64.   } else {
  65.     printf("%d is equal to %d, you get another go\n", playerNum, num);
  66.     return 1;
  67.   }
  68. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment