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