Hogy-Bear icon

Q6

Hogy-Bear | PRO | 02/08/18 12:48:05 PM UTC | 0 ⭐ | 227 👁️ | Never ⏰ | []
C |

1.87 KB

|

None

|

0 👍

/

0 👎

// made by :  Eoghan Hogan
//             17335293
//
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
//function prototypes
int biggestNumber(int a, int b);
int rollDice(void);
int getState(int num, int playerNum);
 
 
int main(int argc, char const *argv[]) {
 
  srand(time(NULL)); //initiallize srand function
 
  int gameState = 1; //0 is lost 1 is playing and 2 is won
  int numberToBeat = biggestNumber(rollDice(), rollDice()); //get a number for the player to beat.
  int playersRoll;
 
  while (gameState == 1) { //while the player is still playing
 
    printf("the number to beat is %d\n", numberToBeat);
    printf("hit enter to roll two dice\n");
    while ( getchar() != '\n' ); //look for the player to press enter
    playersRoll = biggestNumber(rollDice(), rollDice()); //generate a roll for the player
    printf("you rolled %d\n", playersRoll);
    gameState = getState(numberToBeat, playersRoll); //get what state the game should be in now.
 
  }
 
  if (gameState == 2) {
    return 1;
  } else {
    return 0;
  }
 
}
 
int rollDice(void){
  return ( 1 + ( rand() % 6 ) ); //return a dice number
}
 
int biggestNumber(int a, int b){ //get the biggest number from two iputted dice numbers
  int pos1 = ( 10 * a ) + b;
  int pos2 = ( 10 * b ) + a;
  //return which ever is the biggest
  if ( pos1 > pos2 ) {
    return pos1;
  } else {
    return pos2;
  }
}
 
int getState(int num, int playerNum){ //decide the games state based on the numbers
  //return a number based on the players roll and the first roll.
  if ( num > playerNum ) {
    printf("%d is bigger than %d, you lose!\n", num, playerNum);
    return 0;
  } else if( playerNum > num ) {
    printf("%d is bigger than %d, you win!\n", playerNum, num);
    return 2;
  } else {
    printf("%d is equal to %d, you get another go\n", playerNum, num);
    return 1;
  }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎