Advertisement
Guest User

C Bingo Game

a guest
Apr 24th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <Windows.h>
  6.  
  7. #define BOARD_SIZE 5
  8. #define NUMBER_RANGE_MIN 1
  9. #define NUMBER_RANGE_MAX 75
  10. #define PLAYER_NAME_LENGTH 20
  11. #define NUMBERS_IN_BOARD BOARD_SIZE * BOARD_SIZE
  12. #define HIT 'X'
  13. #define FALSE 0
  14. #define TRUE !FALSE
  15. #define SLEEP_DURATION 500
  16.  
  17. int m = 0;
  18. int round = 0;
  19.  
  20. typedef struct bingoPlayer
  21. {
  22.     char name[PLAYER_NAME_LENGTH];
  23.     char board[BOARD_SIZE][BOARD_SIZE];
  24.  
  25. } bingoPlayer;
  26.  
  27. typedef struct bingoGame
  28. {
  29.     int numsDrawn[NUMBER_RANGE_MAX];
  30.     int numOfNumsDrawn;
  31.     bingoPlayer* players;
  32.     int numPlayers;
  33.  
  34. } bingoGame;
  35.  
  36. void initGame(bingoGame* game, int players);
  37. void initBingoPlayer(bingoPlayer* pPlayer);
  38. int updateGame(bingoGame* game, int num);
  39. int updatePlayer(bingoPlayer* player, int num);
  40. void printBoard(bingoPlayer player);
  41. void printBoards(bingoGame game);
  42. void deleteGame(bingoGame game);
  43. int hasWon(bingoPlayer player);
  44. void generateRandomBoard(int* arr);
  45.  
  46. int main(void)
  47. {
  48.     int players = 0, winner = 0;
  49.     bingoGame game = { { 0 }, 0, 0, 0 };
  50.  
  51.     srand(time(NULL));
  52.  
  53.     printf("Welcome to BINGO!\nTo start playing, enter number of players: ");
  54.     scanf("%d", &players);
  55.     getchar();
  56.  
  57.     initGame(&game, players);
  58.  
  59.     do
  60.     {
  61.         winner = updateGame(&game, rand() % NUMBER_RANGE_MAX + NUMBER_RANGE_MIN);
  62.     } while (!winner);
  63.  
  64.     deleteGame(game);
  65.  
  66.     getchar();
  67.     return 0;
  68. }
  69.  
  70. /*
  71. This function updates the player details it and checks if this number is in the player board,
  72. if it is, it switches the place in the board to 'X', also it returns 1 if the player got a hit
  73. Input: bingoPlayer struct pointer and number randomized
  74. Output: 1 if the player got a hit, 0 if he didnt
  75. */
  76. int updatePlayer(bingoPlayer* player, int num)
  77. {
  78.     int i = 0, j = 0, gotHit = 0;
  79.  
  80.     for (i = 0; i < BOARD_SIZE; i++)
  81.     {
  82.         for (j = 0; j < BOARD_SIZE; j++)
  83.         {
  84.             if ((int)player->board[i][j] == num)
  85.             {
  86.                 gotHit = 1;
  87.                 printf("%s, you got a hit! Press any key to continue...", player->name);
  88.                 getchar();
  89.                 player->board[i][j] = HIT;
  90.                 printBoard(*player);
  91.             }
  92.         }
  93.     }
  94.     return gotHit;
  95. }
  96.  
  97. /*
  98. This function prints a player board
  99. Input: bingoPlayer
  100. Output: None
  101. */
  102. void printBoard(bingoPlayer player)
  103. {
  104.     int i = 0, j = 0;
  105.  
  106.     for (i = 0; i < BOARD_SIZE; i++)
  107.     {
  108.         for (j = 0; j < BOARD_SIZE; j++)
  109.         {
  110.             if (player.board[i][j] == HIT)
  111.             {
  112.                 printf(" X  ");
  113.             }
  114.             else
  115.             {
  116.                 printf(" %2d ", (int)player.board[i][j]);
  117.             }
  118.         }
  119.         printf("\n\n");
  120.     }
  121. }
  122.  
  123. /*
  124. This function prints all players' boards
  125. Input: bingoPlayer
  126. Output: None
  127. */
  128. void printBoards(bingoGame game)
  129. {
  130.     int i = 0;
  131.     for (i = 0; i < game.numPlayers; i++)
  132.     {
  133.         printf("%s's board:\n", game.players[i].name);
  134.         printBoard(game.players[i]);
  135.     }
  136. }
  137.  
  138. /*
  139. The function initializes a bingoPlayer structure whilist scanning needed values from the user
  140. input: a pointer to the instance of the structure
  141. output: none
  142. */
  143. void initBingoPlayer(bingoPlayer* pPlayer)
  144. {
  145.     int i = 0, j = 0, k = 0, randomNumbers[NUMBERS_IN_BOARD] = { 0 };
  146.  
  147.     generateRandomBoard(randomNumbers);
  148.  
  149.     printf("enter your name: ");
  150.     fgets(pPlayer->name, PLAYER_NAME_LENGTH, stdin);
  151.     pPlayer->name[strcspn(pPlayer->name, "\n")] = 0;
  152.  
  153.     for (i = 0; i < BOARD_SIZE; i++)
  154.     {
  155.         for (j = 0; j < BOARD_SIZE; j++, k++)
  156.         {
  157.             pPlayer->board[i][j] = randomNumbers[k];
  158.         }
  159.     }
  160.  
  161.  
  162. }
  163.  
  164. /*
  165. The function initializes a bingoGame structure whilist scanning needed values for the players
  166. input: a pointer to the instance of the structure, amount of players
  167. output: none
  168. */
  169. void initGame(bingoGame* game, int players)
  170. {
  171.     int i = 0;
  172.  
  173.     game->numPlayers = players;
  174.     game->numOfNumsDrawn = 0;
  175.     game->players = (bingoPlayer*)malloc(sizeof(bingoPlayer) * players); m++;
  176.  
  177.     for (i = 0; i < NUMBER_RANGE_MAX; i++)
  178.     {
  179.         game->numsDrawn[i] = 0;
  180.     }
  181.  
  182.     for (i = 0; i < players; i++)
  183.     {
  184.         printf("Player %d ", i + 1);
  185.         initBingoPlayer(&game->players[i]);
  186.         printf("Hello %s", game->players[i].name);
  187.  
  188.         if (players - i > 1)
  189.         {
  190.             printf(" please wait for %d more friend(s)", players - i - 1);
  191.         }
  192.         printf("!\n");
  193.         printf("This is your board:\n");
  194.         printBoard(game->players[i]);
  195.     }
  196.  
  197. }
  198.  
  199. /*
  200. The function frees the memory of the players array inside a bingoGame structure
  201. input: the instance of the structure
  202. output: none
  203. */
  204. void deleteGame(bingoGame game)
  205. {
  206.     free(game.players); m--;
  207. }
  208.  
  209. /*
  210. The function generates random unique values for the bingo board
  211. input: array to put the values inside
  212. output: none
  213. */
  214. void generateRandomBoard(int* arr)
  215. {
  216.     int numbersArr[NUMBER_RANGE_MAX] = { 0 }, i = 0, place = 0, temp = 0;
  217.  
  218.     for (i = 0; i < NUMBER_RANGE_MAX; i++)
  219.     {
  220.         numbersArr[i] = i + 1;
  221.     }
  222.  
  223.     for (i = 0; i < NUMBERS_IN_BOARD; i++)
  224.     {
  225.         place = rand() % (NUMBER_RANGE_MAX - i);
  226.         arr[i] = numbersArr[place];
  227.  
  228.         temp = numbersArr[NUMBER_RANGE_MAX - i - 1];
  229.         numbersArr[NUMBER_RANGE_MAX - i - 1] = numbersArr[place];
  230.         numbersArr[place] = temp;
  231.  
  232.     }
  233. }
  234.  
  235. /*
  236. The function checks if a player has won the game
  237. input: the instance of a bingoPlayer structure
  238. output: 1 if he won, 0 otherwise
  239. */
  240. int hasWon(bingoPlayer player)
  241. {
  242.     int i = 0, j = 0, won = 0;
  243.  
  244.     for (i = 0; i < BOARD_SIZE && won < BOARD_SIZE; i++)
  245.     {
  246.         won = 0;
  247.         for (j = 0; j < BOARD_SIZE; j++)
  248.         {
  249.             if (player.board[i][j] == HIT) won++;
  250.         }
  251.     }
  252.  
  253.     return won == BOARD_SIZE;
  254. }
  255.  
  256.  
  257. /*
  258. The function updates all players in the game and checks if a player has won and prints it
  259. input: a pointer to the game structure, random number rolled
  260. output: 1 if a player has won, 0 otherwise
  261. */
  262. int updateGame(bingoGame* game, int num)
  263. {
  264.     int i = 0, winner = 0, gotHit = 0;
  265.  
  266.     game->numOfNumsDrawn++;
  267.     game->numsDrawn[game->numOfNumsDrawn] = num;
  268.  
  269.     Sleep(SLEEP_DURATION);
  270.     printf("\n--- Round #%d: The number rolled this round is %d ---\n", ++round, num);
  271.  
  272.     for (i = 0; i < game->numPlayers; i++)
  273.     {
  274.         if (gotHit == 0)
  275.         {
  276.             gotHit = updatePlayer(&game->players[i], num);
  277.         }
  278.         else
  279.         {
  280.             updatePlayer(&game->players[i], num);
  281.         }
  282.  
  283.         if (i >= game->numPlayers - 1 && !gotHit)
  284.         {
  285.             printf("No one got a hit this round!\n");
  286.         }
  287.         if (hasWon(game->players[i]))
  288.         {
  289.             printf("\n%s has won!\n", game->players[i].name);
  290.             printBoard(game->players[i]);
  291.             winner = 1;
  292.         }
  293.  
  294.  
  295.     }
  296.  
  297.     return winner;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement