From Urbana, 1 Year ago, written in C.
Embed
  1. //
  2. //  FinalProject.c
  3. //  Final Project (Craps Game)
  4. //
  5. //  Created by Ryan Kelley on 4/13/17.
  6. //  Last Modified on 5/3/17.
  7. //  Copyright © 2017 Ryan Kelley. All rights reserved.
  8. //  Lets user play a game of craps with multiple menu features.
  9. //
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15.  
  16. #define FILE_NAME "players.txt"
  17. #define MAX_PLAYERS 10
  18.  
  19. /* Player structure to store player info */
  20. typedef struct {
  21.   char name[64];
  22.   int balance;
  23.   int gain;
  24. } Player;
  25.  
  26. /* Functions */
  27. int LoadPlayerData(Player *players);
  28. void PushPlayerData(Player *players, int *totalPlayers);
  29. void PrintPlayerData(Player *players, int *totalPlayers);
  30. int GetPlayerIndex(Player *players, int  *totalPlayers);
  31. void TopBalance(Player *players, int *totalPlayers);
  32. void PrintTopFive(Player *players, int *totalPlayers, int type);
  33. void HandleMenu(Player *players, int *totalPlayers);
  34. void PlayGame(Player *players, int *totalPlayers);
  35. void sSort(Player *playerss, int *totalPlayers, int type);
  36.  
  37.  
  38. int main(void) {
  39.  
  40.   Player players[10];
  41.   int totalPlayers;
  42.  
  43.   /* Load player data from players.txt */
  44.   totalPlayers = LoadPlayerData(players);
  45.   printf("Loaded %d players\n", totalPlayers);
  46.  
  47.   /* Display and process menu choice to user */
  48.   HandleMenu(players, &totalPlayers);
  49.  
  50.   return 0;
  51. }
  52.  
  53. /* Initiates the game */
  54. void PlayGame(Player *players, int *totalPlayers) {
  55.   int playerIndex = GetPlayerIndex(players, totalPlayers);
  56.   getchar();
  57.   srand(time(NULL));
  58.   for (;;) {
  59.     /* Get initial dice roll from user */
  60.     printf("\nPress any key to roll the dice.\n");
  61.     getchar();
  62.     int dice1 = rand() % 6 + 1;
  63.     int dice2 = rand() % 6 + 1;
  64.     int diceSum = dice1 + dice2;
  65.     printf("You rolled %d and %d for a total of %d.\n", dice1, dice2, diceSum);
  66.    
  67.     switch(diceSum) {
  68.       /* If the user won */
  69.       case 7:
  70.       case 11:
  71.         players[playerIndex].balance += 10;
  72.         players[playerIndex].gain += 10;
  73.         printf("You win the game. Your current balance is %d.\n", players[playerIndex].balance);
  74.         break;
  75.       /* If the user lost */
  76.       case 2:
  77.       case 3:
  78.       case 12:
  79.         players[playerIndex].balance -= 1;
  80.         players[playerIndex].gain -= 1;
  81.         printf("The house wins. Your current balance is %d.\n", players[playerIndex].balance);
  82.         break;
  83.       /* User didn't win or lose */
  84.       case 4:
  85.       case 5:
  86.       case 6:
  87.       case 8:
  88.       case 9:
  89.       case 10: {
  90.         int point = diceSum;
  91.         for (;;) {
  92.          
  93.  
  94.           printf("Press enter to roll the dice.\n");
  95.           getchar();
  96.           dice1 = rand() % 6 + 1;
  97.           dice2 = rand() % 6 + 1;
  98.           diceSum = dice1 + dice2;
  99.           printf("You rolled %d and %d for a total of %d.\n", dice1, dice2, diceSum);
  100.          
  101.           /* The user lost */
  102.           if (diceSum == 7) {
  103.             players[playerIndex].balance -= 1;
  104.             players[playerIndex].gain -= 1;
  105.             printf("The house wins. "
  106.                    "Your current balance is %d.\n", players[playerIndex].balance);
  107.             break;
  108.          
  109.           /* The user won */
  110.           } else if (diceSum == point) {
  111.             players[playerIndex].balance += 10;
  112.             players[playerIndex].gain += 10;
  113.             printf("You win the game. Your current balance is %d.\n", players[playerIndex].balance);
  114.             break;
  115.           }
  116.         }
  117.         break;
  118.       }
  119.     }
  120.    
  121.     printf("Would you like to play again (y/n)? ");
  122.     char again = getchar(); getchar();
  123.    
  124.     /* Continue game if yes */
  125.     if (again == 'y') continue;
  126.     else break;
  127.  
  128.   }
  129.  
  130.   HandleMenu(players, totalPlayers);
  131.  
  132. }
  133.  
  134. /* Loads data from players.txt into *p */
  135. int LoadPlayerData(Player *players) {
  136.   FILE *fp = fopen(FILE_NAME, "r");
  137.   int playerIndex = 0;
  138.  
  139.   for (;;) {
  140.    
  141.     // Scan contents from players.txt into *p
  142.     if (fscanf(fp, "%s\t%d\t%d", players[playerIndex].name, &players[playerIndex].balance, &players[playerIndex].gain) != 3) {
  143.       if (feof(fp)) {
  144.         fclose(fp);
  145.         break;
  146.       }
  147.     }
  148.     playerIndex++;
  149.   }
  150.  
  151.   fclose(fp);
  152.   return playerIndex;
  153. }
  154.  
  155. /* Writes data from players into players.txt */
  156. void PushPlayerData(Player *players, int *totalPlayers) {
  157.   FILE *fp = fopen(FILE_NAME, "w+b");
  158.  
  159.   // Write contents of players into players.txt
  160.   for (int playerIndex = 0; playerIndex < *totalPlayers; playerIndex++) {
  161.    
  162.     fprintf(fp, "%s\t%d\t%d\r\n", players[playerIndex].name,
  163.             players[playerIndex].balance, players[playerIndex].gain);
  164.  
  165.   }
  166.  
  167.   fclose(fp);
  168.  
  169. }
  170.  
  171. /* Prints top 5 players by given type */
  172. void PrintTopFive(Player *players, int *totalPlayers, int type) {
  173.  
  174.   /* Sort players by type */
  175.   sSort(players, totalPlayers, type);
  176.  
  177.   if (type == 0) {
  178.    
  179.     printf("__________ Top 5 By Balance _________\n");
  180.    
  181.     /* Print from end of array to 5 from end */
  182.     for (int i = *totalPlayers - 1; i >= *totalPlayers - 5; i--) {
  183.      
  184.       printf("%s\t%d\n", players[i].name, players[i].balance);
  185.    
  186.     }
  187.   } else {
  188.    
  189.     printf("\n___________ Top 5 By Gain ___________\n\n");
  190.    
  191.     for (int j = *totalPlayers - 1; j >= *totalPlayers - 5; j--) {
  192.      
  193.       printf("%s\t%d\n", players[j].name, players[j].gain);
  194.    
  195.     }
  196.  
  197.   }
  198.  
  199.   printf("_____________________________________\n");
  200.  
  201. }
  202.  
  203. /* Selection sort */
  204. void sSort(Player *players, int *totalPlayers, int type){
  205.   int min, indexA, indexB;
  206.  
  207.   for( indexA = 0; indexA < MAX_PLAYERS; indexA++){
  208.    
  209.     min = indexA;
  210.    
  211.     for(indexB = indexA+1; indexB < MAX_PLAYERS; indexB++){
  212.      
  213.       /* Check type to sort by */
  214.       if (type == 0) {
  215.        
  216.        
  217.         if(players[indexB].balance < players[min].balance)
  218.           min = indexB;
  219.      
  220.       }
  221.      
  222.       else {
  223.        
  224.         if(players[indexB].gain < players[min].gain) {
  225.          
  226.           min = indexB;
  227.        
  228.         }
  229.      
  230.       }
  231.     }
  232.    
  233.     // Swap players[indexA] and players[min] */
  234.     Player temp = players[indexA];
  235.     players[indexA] = players[min];
  236.     players[min] = temp;
  237.  
  238.   }
  239. }
  240.  
  241. /* Prints the contents of players.txt */
  242. void PrintPlayerData(Player *players, int *totalPlayers) {
  243.  
  244.   /* Sort players by balance */
  245.   sSort(players, totalPlayers, 0);
  246.  
  247.   printf("\nPlayer Name\tBalance\tGain\n"
  248.          "____________________________\n");
  249.  
  250.  
  251.   for (int playerIndex = 0; playerIndex < *totalPlayers; playerIndex++) {
  252.    
  253.     printf("%s\t\t%d\t%d\n", players[playerIndex].name,
  254.            players[playerIndex].balance, players[playerIndex].gain);
  255.  
  256.   }
  257.  
  258.   printf("___________________________\n\n");
  259.  
  260. }
  261.  
  262. /* Gets the index in players such that players[index].name is that name */
  263. int GetPlayerIndex(Player *players, int *totalPlayers) {
  264.  
  265.   char playerName[64];
  266.  
  267.   for(;;) {
  268.    
  269.     printf("Enter your name: ");
  270.     scanf("%s", playerName);
  271.    
  272.     for (int playerIndex = 0; playerIndex < *totalPlayers; playerIndex++) {
  273.      
  274.       if (strcmp(playerName, players[playerIndex].name) == 0) {
  275.        
  276.         return playerIndex;
  277.      
  278.       }
  279.    
  280.     }
  281.    
  282.     printf("The name \"%s\" was not found, please enter a name from the list below.\n\n", playerName);
  283.    
  284.     for (int knownPlayer = 0; knownPlayer < *totalPlayers; knownPlayer++) {
  285.      
  286.       printf("%s\n", players[knownPlayer].name);
  287.    
  288.     }
  289.    
  290.     printf("\n");
  291.  
  292.   }
  293. }
  294.  
  295. /* Prompts user for name and sets their balance to entered balance */
  296. void TopBalance(Player *players, int *totalPlayers) {
  297.   int bal, playerIndex;
  298.   playerIndex = GetPlayerIndex(players, totalPlayers);
  299.   printf("Enter the balance: ");
  300.   scanf("%d", &bal); getchar();
  301.   players[playerIndex].balance += bal;
  302.   printf("Balance for %s updated to %d!\n", players[playerIndex].name, players[playerIndex].balance);
  303. }
  304.  
  305. /* Displays the game menu, then executes function based on input */
  306. void HandleMenu(Player *players, int *totalPlayers) {
  307.   char keyPressed;
  308.   for (;;) {
  309.     /* Print menu to the screen */
  310.     printf(
  311.     "\n############# Craps Game Menu ############\n"
  312.     "# 0) Top up your balance                 #\n"
  313.     "# 1) Play Game                           #\n"
  314.     "# 2) Top 5 Players by Balance            #\n"
  315.     "# 3) Top 5 Winners by Gain               #\n"
  316.     "# 4) Show Player Information             #\n"
  317.     "# 5) Exit                                #\n"
  318.     "##########################################\n"
  319.     "Please type a number from 0 to 5 to select menu item:\n"
  320.     );
  321.  
  322.     keyPressed = getchar(); getchar(); // Get user input (0-5)
  323.  
  324.     // Validate the input from the user...
  325.     if( keyPressed == '5' ) {
  326.      
  327.       PushPlayerData(players, totalPlayers);
  328.       exit(1);
  329.    
  330.     } else if ( keyPressed < '0' || keyPressed > '5' ) {
  331.      
  332.       printf("\n[Err] Invalid Input... Try again.\n");
  333.       continue; // Unknown input... we can safely loop back around
  334.    
  335.     } else {
  336.     /* Do appropriate action */
  337.       switch(keyPressed) {
  338.         case '0': {
  339.           TopBalance(players, totalPlayers);
  340.           break;
  341.         }
  342.         case '1':
  343.           PlayGame(players, totalPlayers);
  344.           break;
  345.         case '2':
  346.           PrintTopFive(players, totalPlayers, 0);
  347.           break;
  348.         case '3':
  349.           PrintTopFive(players, totalPlayers, 1);
  350.           break;
  351.         case '4':
  352.           PrintPlayerData(players, totalPlayers);
  353.           break;
  354.         default:
  355.           puts("[Err] An un-expected error occured.");
  356.           exit(1);
  357.       }
  358.     }
  359.  
  360.     printf("Press enter to go back to the menu...");
  361.     getchar();
  362.  
  363.   }
  364. }