Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.26 KB | None | 0 0
  1. // Prithvisagar Shivaraman G01111417
  2. // CS 262, Lab Section 208
  3. // Project 1
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. // Constants
  9. #define BUF_SIZE 20
  10. #define WIN_POINTS 94
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. // Jawbone Positions
  15. #define ERILLAAN_PYSTYYN 0
  16. #define PITKA_RIVI 1
  17. #define ONE_POINTS 2
  18. #define TWO_POINTS 3
  19. #define FOUR_POINTS 4
  20. #define SEVEN_POINTS 5
  21. #define PIDA_JA_ANNA 6
  22.  
  23. // Function definitions
  24. void EnterRandomSeed();
  25. void PlayGame(int *playerStake, int *computerStake);
  26. int PlayRoundPlayer(int curBank);
  27. int PlayRoundComputer(int curBank);
  28. int AskYesNo(char *formatString);
  29. int RollJawbone();
  30. int CalculateRoundPoints(int *roundPoints);
  31.  
  32. /**
  33.  * Main function (entry point)
  34.  */
  35. int main() {
  36.     // Get random seed
  37.     EnterRandomSeed();
  38.    
  39.     int playGame = TRUE;
  40.  
  41.     // Give each player 5 stake
  42.     int playerStake = 5;
  43.     int computerStake = 5;
  44.  
  45.     printf("Welcome to the Porojen Leuka Game!");
  46.    
  47.     playGame = AskYesNo("Do you want to play? [Y/y/N/n]: \n");
  48.  
  49.     // Start the game
  50.     while (playGame){
  51.         // Print out stake
  52.         printf("Player Stake:   %d markka\n", playerStake);
  53.         printf("Computer Stake: %d markka\n", computerStake);
  54.        
  55.         // Play Game
  56.         PlayGame(&playerStake, &computerStake);
  57.        
  58.         if (playerStake == 0 || computerStake == 0){
  59.             playGame = FALSE;
  60.         } else {
  61.             // Ask if player wants to play another game
  62.             playGame = AskYesNo("Do you want to play another game? [Y/y/N/n]: \n");
  63.         }
  64.     }
  65.    
  66.     // Print end messages
  67.     printf("Game Over!!\n\n");
  68.     printf("Player Stake:   %d markka\n", playerStake);
  69.     printf("Computer Stake: %d markka\n", computerStake);
  70.     return 0;
  71. }
  72.  
  73. /**
  74.  * Gets the seed for random() from the user
  75.  */
  76. void EnterRandomSeed() {
  77.        char buffer[100];
  78.        unsigned int myRandomSeed;
  79.        printf("Please enter a random number seed: ");
  80.        fgets(buffer, 100, stdin);
  81.        sscanf(buffer, "%d", &myRandomSeed);
  82.        srandom(myRandomSeed);
  83. }
  84.  
  85. /**
  86.  * Plays a single game
  87.  */
  88. void PlayGame(int *playerStake, int *computerStake){
  89.     int playerBank = 0;
  90.     int computerBank = 0;
  91.     int playerFirst = TRUE;
  92.  
  93.     playerFirst = AskYesNo("Do you (the player) want to play first? [Y/y/N/n]: \n");
  94.  
  95.     printf("\nLet's Play!!\n\n");
  96.    
  97.     // Loop until one player wins
  98.     while (playerBank < WIN_POINTS && computerBank < WIN_POINTS){
  99.         printf("\n*** New Round ***");
  100.        
  101.         // Play round
  102.         if (playerFirst){
  103.             playerBank += PlayRoundPlayer(playerBank);
  104.             if (playerBank < WIN_POINTS){
  105.                 computerBank += PlayRoundComputer(computerBank);
  106.             }
  107.         } else {
  108.             computerBank += PlayRoundComputer(computerBank);
  109.             if (computerBank < WIN_POINTS){
  110.                 playerBank += PlayRoundPlayer(playerBank);
  111.             }
  112.         }
  113.         printf("\n");
  114.         printf("Player:   %d points\n", playerBank);
  115.         printf("Comupter: %d points\n", computerBank);
  116.         printf("\n");
  117.     }
  118.  
  119.     // Figure out who won
  120.     if (playerBank >= WIN_POINTS){
  121.         printf("Player won!\n");
  122.         (*computerStake)--;
  123.         (*playerStake)++;
  124.     } else {
  125.         printf("Computer won!\n");
  126.         (*playerStake)--;
  127.         (*computerStake)++;
  128.     }
  129. }
  130.  
  131. /**
  132.  * Runs a round for the player
  133.  */
  134. int PlayRoundPlayer(int curBank){
  135.     int roundPoints = 0;
  136.     int continueRound = TRUE;
  137.  
  138.     printf("\nPlayer's Turn--- \n");
  139.  
  140.     while (continueRound){
  141.         continueRound = CalculateRoundPoints(&roundPoints);
  142.        
  143.         printf("\tPoints this round: %d\n\n", roundPoints);
  144.  
  145.         // Check if bank + roundPoints >= WIN_POINTS
  146.         if (curBank + roundPoints >= WIN_POINTS){
  147.             continueRound = FALSE;
  148.         }
  149.        
  150.         if (continueRound){
  151.             continueRound = AskYesNo("\tDo you want to roll again? [Y/y/N/n]: \n");
  152.         }
  153.     }
  154.     return roundPoints;
  155. }
  156.  
  157. /**
  158.  * Runs a round for the computer
  159.  */
  160. int PlayRoundComputer(int curBank){
  161.     int roundPoints = 0;
  162.     int continueRound = TRUE;
  163.  
  164.     printf("\nComputer's Turn--- \n");
  165.  
  166.     while (continueRound){
  167.         continueRound = CalculateRoundPoints(&roundPoints);
  168.        
  169.         printf("\tPoints this round: %d\n\n", roundPoints);
  170.  
  171.         // Check if bank + roundPoints >= WIN_POINTS or roundPoints >= 18
  172.         if (curBank + roundPoints >= WIN_POINTS || roundPoints >= 18){
  173.             continueRound = FALSE;
  174.         }
  175.     }
  176.     return roundPoints;
  177. }
  178.  
  179. /**
  180.  * Prompts the user for a yes/no answer
  181.  */
  182. int AskYesNo(char *formatString){
  183.     char buffer[BUF_SIZE];
  184.     char chosenChar = ' ';
  185.     int i;
  186.  
  187.     // Give 10 chances to enter valid input.
  188.     for (i = 0; i < 10; i++){
  189.         printf(formatString);
  190.         fgets(buffer, BUF_SIZE, stdin);
  191.         sscanf(buffer, "%c", &chosenChar);
  192.  
  193.         switch (chosenChar){
  194.             case 'Y':
  195.             case 'y':
  196.                 return TRUE; // Don't need break since we're returning
  197.             case 'N':
  198.             case 'n':
  199.                 return FALSE; // Don't need break since we're returning
  200.             default:
  201.                 printf("Invalid input! Try again!\n");
  202.         }
  203.     }
  204.  
  205.     // If we get here then the user can't read and we will just return 0 or 'No'
  206.     return FALSE;
  207. }
  208.  
  209. /**
  210.  * Rolls the jawbone
  211.  */
  212. int RollJawbone(){
  213.     int randomNumber = random() % 100;
  214.    
  215.     // Figure out which position was rolled and return
  216.     if (randomNumber <= 15){
  217.         return ERILLAAN_PYSTYYN;
  218.     }
  219.     if (randomNumber <= 30){
  220.         return PITKA_RIVI;
  221.     }
  222.     if (randomNumber <= 54){
  223.         return ONE_POINTS;
  224.     }
  225.     if (randomNumber <= 69){
  226.         return TWO_POINTS;
  227.     }
  228.     if (randomNumber <= 82){
  229.         return FOUR_POINTS;
  230.     }
  231.     if (randomNumber <= 91){
  232.         return SEVEN_POINTS;
  233.     }
  234.     return PIDA_JA_ANNA;
  235. }
  236.  
  237. /**
  238.  * Calculates the points to add in a round
  239.  */
  240. int CalculateRoundPoints(int *roundPoints){
  241.     int jawbonePos = RollJawbone();
  242.     switch (jawbonePos){
  243.         case ERILLAAN_PYSTYYN:
  244.             printf("\tRolled Erillään Pystyyn. All points for this round lost.\n");
  245.             *roundPoints = 0;
  246.             return FALSE;
  247.         case PITKA_RIVI:
  248.             printf("\tRolled Pitkä Rivi. All round points transferred to bank.\n");
  249.             return FALSE;
  250.         case ONE_POINTS:
  251.             printf("\tRolled One (1). Gained one point.\n");
  252.             *roundPoints += 1;
  253.             return TRUE;
  254.         case TWO_POINTS:
  255.             printf("\tRolled Two (2). Gained two points.\n");
  256.             *roundPoints += 2;
  257.             return TRUE;
  258.         case FOUR_POINTS:
  259.             printf("\tRolled Four (4). Gained four points.\n");
  260.             *roundPoints += 4;
  261.             return TRUE;
  262.         case SEVEN_POINTS:
  263.             printf("\tRolled Seven (7). Gained seven points.\n");
  264.             *roundPoints += 7;
  265.             return TRUE;
  266.         case PIDA_JA_ANNA:
  267.             printf("\tRolled Pidä ja anna. Gained eleven points. All round points transferred to bank.\n");
  268.             *roundPoints += 11;
  269.             return FALSE;
  270.     }
  271.     return TRUE;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement