Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Prithvisagar Shivaraman G01111417
- // CS 262, Lab Section 208
- // Project 1
- #include <stdlib.h>
- #include <stdio.h>
- // Constants
- #define BUF_SIZE 20
- #define WIN_POINTS 94
- #define TRUE 1
- #define FALSE 0
- // Jawbone Positions
- #define ERILLAAN_PYSTYYN 0
- #define PITKA_RIVI 1
- #define ONE_POINTS 2
- #define TWO_POINTS 3
- #define FOUR_POINTS 4
- #define SEVEN_POINTS 5
- #define PIDA_JA_ANNA 6
- // Function definitions
- void EnterRandomSeed();
- void PlayGame(int *playerStake, int *computerStake);
- int PlayRoundPlayer(int curBank);
- int PlayRoundComputer(int curBank);
- int AskYesNo(char *formatString);
- int RollJawbone();
- int CalculateRoundPoints(int *roundPoints);
- /**
- * Main function (entry point)
- */
- int main() {
- // Get random seed
- EnterRandomSeed();
- int playGame = TRUE;
- // Give each player 5 stake
- int playerStake = 5;
- int computerStake = 5;
- printf("Welcome to the Porojen Leuka Game!");
- playGame = AskYesNo("Do you want to play? [Y/y/N/n]: \n");
- // Start the game
- while (playGame){
- // Print out stake
- printf("Player Stake: %d markka\n", playerStake);
- printf("Computer Stake: %d markka\n", computerStake);
- // Play Game
- PlayGame(&playerStake, &computerStake);
- if (playerStake == 0 || computerStake == 0){
- playGame = FALSE;
- } else {
- // Ask if player wants to play another game
- playGame = AskYesNo("Do you want to play another game? [Y/y/N/n]: \n");
- }
- }
- // Print end messages
- printf("Game Over!!\n\n");
- printf("Player Stake: %d markka\n", playerStake);
- printf("Computer Stake: %d markka\n", computerStake);
- return 0;
- }
- /**
- * Gets the seed for random() from the user
- */
- void EnterRandomSeed() {
- char buffer[100];
- unsigned int myRandomSeed;
- printf("Please enter a random number seed: ");
- fgets(buffer, 100, stdin);
- sscanf(buffer, "%d", &myRandomSeed);
- srandom(myRandomSeed);
- }
- /**
- * Plays a single game
- */
- void PlayGame(int *playerStake, int *computerStake){
- int playerBank = 0;
- int computerBank = 0;
- int playerFirst = TRUE;
- playerFirst = AskYesNo("Do you (the player) want to play first? [Y/y/N/n]: \n");
- printf("\nLet's Play!!\n\n");
- // Loop until one player wins
- while (playerBank < WIN_POINTS && computerBank < WIN_POINTS){
- printf("\n*** New Round ***");
- // Play round
- if (playerFirst){
- playerBank += PlayRoundPlayer(playerBank);
- if (playerBank < WIN_POINTS){
- computerBank += PlayRoundComputer(computerBank);
- }
- } else {
- computerBank += PlayRoundComputer(computerBank);
- if (computerBank < WIN_POINTS){
- playerBank += PlayRoundPlayer(playerBank);
- }
- }
- printf("\n");
- printf("Player: %d points\n", playerBank);
- printf("Comupter: %d points\n", computerBank);
- printf("\n");
- }
- // Figure out who won
- if (playerBank >= WIN_POINTS){
- printf("Player won!\n");
- (*computerStake)--;
- (*playerStake)++;
- } else {
- printf("Computer won!\n");
- (*playerStake)--;
- (*computerStake)++;
- }
- }
- /**
- * Runs a round for the player
- */
- int PlayRoundPlayer(int curBank){
- int roundPoints = 0;
- int continueRound = TRUE;
- printf("\nPlayer's Turn--- \n");
- while (continueRound){
- continueRound = CalculateRoundPoints(&roundPoints);
- printf("\tPoints this round: %d\n\n", roundPoints);
- // Check if bank + roundPoints >= WIN_POINTS
- if (curBank + roundPoints >= WIN_POINTS){
- continueRound = FALSE;
- }
- if (continueRound){
- continueRound = AskYesNo("\tDo you want to roll again? [Y/y/N/n]: \n");
- }
- }
- return roundPoints;
- }
- /**
- * Runs a round for the computer
- */
- int PlayRoundComputer(int curBank){
- int roundPoints = 0;
- int continueRound = TRUE;
- printf("\nComputer's Turn--- \n");
- while (continueRound){
- continueRound = CalculateRoundPoints(&roundPoints);
- printf("\tPoints this round: %d\n\n", roundPoints);
- // Check if bank + roundPoints >= WIN_POINTS or roundPoints >= 18
- if (curBank + roundPoints >= WIN_POINTS || roundPoints >= 18){
- continueRound = FALSE;
- }
- }
- return roundPoints;
- }
- /**
- * Prompts the user for a yes/no answer
- */
- int AskYesNo(char *formatString){
- char buffer[BUF_SIZE];
- char chosenChar = ' ';
- int i;
- // Give 10 chances to enter valid input.
- for (i = 0; i < 10; i++){
- printf(formatString);
- fgets(buffer, BUF_SIZE, stdin);
- sscanf(buffer, "%c", &chosenChar);
- switch (chosenChar){
- case 'Y':
- case 'y':
- return TRUE; // Don't need break since we're returning
- case 'N':
- case 'n':
- return FALSE; // Don't need break since we're returning
- default:
- printf("Invalid input! Try again!\n");
- }
- }
- // If we get here then the user can't read and we will just return 0 or 'No'
- return FALSE;
- }
- /**
- * Rolls the jawbone
- */
- int RollJawbone(){
- int randomNumber = random() % 100;
- // Figure out which position was rolled and return
- if (randomNumber <= 15){
- return ERILLAAN_PYSTYYN;
- }
- if (randomNumber <= 30){
- return PITKA_RIVI;
- }
- if (randomNumber <= 54){
- return ONE_POINTS;
- }
- if (randomNumber <= 69){
- return TWO_POINTS;
- }
- if (randomNumber <= 82){
- return FOUR_POINTS;
- }
- if (randomNumber <= 91){
- return SEVEN_POINTS;
- }
- return PIDA_JA_ANNA;
- }
- /**
- * Calculates the points to add in a round
- */
- int CalculateRoundPoints(int *roundPoints){
- int jawbonePos = RollJawbone();
- switch (jawbonePos){
- case ERILLAAN_PYSTYYN:
- printf("\tRolled Erillään Pystyyn. All points for this round lost.\n");
- *roundPoints = 0;
- return FALSE;
- case PITKA_RIVI:
- printf("\tRolled Pitkä Rivi. All round points transferred to bank.\n");
- return FALSE;
- case ONE_POINTS:
- printf("\tRolled One (1). Gained one point.\n");
- *roundPoints += 1;
- return TRUE;
- case TWO_POINTS:
- printf("\tRolled Two (2). Gained two points.\n");
- *roundPoints += 2;
- return TRUE;
- case FOUR_POINTS:
- printf("\tRolled Four (4). Gained four points.\n");
- *roundPoints += 4;
- return TRUE;
- case SEVEN_POINTS:
- printf("\tRolled Seven (7). Gained seven points.\n");
- *roundPoints += 7;
- return TRUE;
- case PIDA_JA_ANNA:
- printf("\tRolled Pidä ja anna. Gained eleven points. All round points transferred to bank.\n");
- *roundPoints += 11;
- return FALSE;
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement