SHARE
TWEET

Untitled

a guest May 24th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************
  2. * Class: MAGSHIMIM C2            *
  3. * Week:  6                       *
  4. * Name:  Yakov Shuhmacher        *
  5. * Credits:                       *
  6. **********************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <string.h>
  12.  
  13. #define BOARD_SIZE 5
  14. #define MAX_VAL 75
  15. #define FALSE 0
  16. #define TRUE !FALSE
  17. #define HIT 'X'
  18.  
  19. #define STR_LEN 20
  20. #define NO_WINNER -1
  21. //First bug: missing typedef...... bingoPlayer
  22. typedef struct bingoPlayer
  23. {
  24.     char name[STR_LEN];
  25.     char board[BOARD_SIZE][BOARD_SIZE];
  26. }bingoPlayer;
  27. //Second bug: both struct had starting values
  28. typedef struct bingoGame
  29. {
  30.     int* numsDrawn;
  31.     int numOfNumsDrawn;
  32.     bingoPlayer * players;
  33.     int numPlayers;
  34. } bingoGame;
  35.  
  36. void initGame(bingoGame * game, int players);
  37. void initBingoPlayer(bingoPlayer* pPlayer);
  38. int updateGame(bingoGame* game, int num);
  39. int numInArr(char arr[][BOARD_SIZE], int num);
  40. void updateBoard(bingoPlayer* player, int num);
  41. void printBoard(bingoPlayer player);
  42. void printBoards(bingoGame game);
  43. int hasWon(bingoPlayer player);
  44. void myFgets(char str[], int n);
  45.  
  46. int main(void)
  47. {
  48.     int numPlayers = 0;
  49.     int winner = NO_WINNER;
  50.     int drawnAlready = TRUE;
  51.     int num = 0;
  52.     int i = 0;
  53.     bingoGame game = { 0, 0, 0, 0 };
  54.  
  55.     srand((unsigned int)time(NULL));
  56.  
  57.     printf("How many players? ");
  58.     scanf("%d", &numPlayers);
  59.     getchar(); // clean buffer
  60.  
  61.     initGame(&game, numPlayers);
  62.  
  63.     printBoards(game);
  64.  
  65.     while (winner == NO_WINNER)
  66.     {
  67.         while (drawnAlready)
  68.         {
  69.             num = (rand() % MAX_VAL) + 1;
  70.             drawnAlready = FALSE;
  71.             for (i = 0; i < game.numOfNumsDrawn; i++)
  72.             {
  73.                 drawnAlready = drawnAlready || (num == game.numsDrawn[i]);
  74.             }
  75.         }
  76.         // num now contains a new drawn number!
  77.         winner = updateGame(&game, num);
  78.         drawnAlready = TRUE;
  79.     }
  80.     printf("%s won the match! The winning board:\n", game.players[winner].name);
  81.     printBoard(game.players[winner]);
  82.     free(game.numsDrawn);//added free()
  83.     free(game.players);//added free()
  84.     getchar();
  85.     return 0;
  86. }
  87.  
  88. /*
  89. Function inits a bingo game
  90. input: game and number of players
  91. output: none
  92. */
  93. void initGame(bingoGame * game, int players)
  94. {
  95.     int i = 0;
  96.     game->numsDrawn = (int*)malloc(sizeof(int) * 0); //no nums drawn yet...realloc will be used later
  97.     game->numPlayers = players;
  98.     game->players = (bingoPlayer*)malloc(sizeof(bingoPlayer) * players);
  99.     for (i = 0; i < game->numPlayers; i++)
  100.     {
  101.         initBingoPlayer(&(game->players[i]));
  102.     }
  103. }
  104.  
  105. /*
  106. This function initializes bingoPlayer struct
  107. Input: pointer to bingoPlayer struct
  108. Output: None
  109. */
  110. void initBingoPlayer(bingoPlayer* pPlayer)
  111. {
  112.     int i = 0, j = 0;
  113.     int num = 0;
  114.  
  115.     printf("What is the player's name?\n");
  116.     myFgets(pPlayer->name, STR_LEN);//changed to ->
  117.  
  118.     for (i = 0; i < BOARD_SIZE; i++)
  119.     {
  120.         for (j = 0; j < BOARD_SIZE; j++)
  121.         {
  122.             pPlayer->board[i][j] = 0;// changed to ->
  123.         }
  124.     }
  125.  
  126.     for (i = 0; i < BOARD_SIZE; i++)
  127.     {
  128.         for (j = 0; j < BOARD_SIZE; j++)
  129.         {
  130.             num = (rand() % MAX_VAL) + 1;
  131.  
  132.             while (numInArr(pPlayer->board, num))
  133.             {
  134.                 num = (rand() % MAX_VAL) + 1;
  135.             }
  136.  
  137.             pPlayer->board[i][j] = (char)num;
  138.         }
  139.     }
  140. }
  141.  
  142. /*
  143. This function checks if a number is in an array
  144. returns 1 if it is and 0 otherwise
  145. Input: char array, int - number
  146. Output: 1 - num in array, 0 - otherwise
  147. */
  148. int numInArr(char arr[][BOARD_SIZE], int num)
  149. {
  150.     int i = 0, j = 0;
  151.     int found = FALSE;
  152.  
  153.     for (i = 0; i < BOARD_SIZE && !found; i++)
  154.     {
  155.         for (j = 0; j < BOARD_SIZE && !found; j++)
  156.         {
  157.             if ((int)arr[i][j] == num)
  158.             {
  159.                 found = TRUE;
  160.             }
  161.         }
  162.     }
  163.     return found;
  164. }
  165.  
  166. /*
  167. This functions updates a board - this includes adding the number drawn to the array of the numbers.
  168. Also, updating all players' boards
  169. input: game and the number drawn
  170. output: index of player who won, or -1 if no winner yet
  171. */
  172. int updateGame(bingoGame* game, int num)
  173. {
  174.     int i = 0;
  175.     int winner = NO_WINNER;
  176.  
  177.     game->numOfNumsDrawn++;
  178.     game->numsDrawn = (int*)realloc(game->numsDrawn, sizeof(int) * game->numOfNumsDrawn);
  179.     game->numsDrawn[game->numOfNumsDrawn - 1] = num;
  180.  
  181.     for (i = 0; i < game->numPlayers && NO_WINNER; i++)
  182.     {
  183.         updateBoard(&(game->players[i]), num);
  184.         if (hasWon(game->players[i]))
  185.         {
  186.             winner = i;
  187.         }
  188.     }
  189.  
  190.     return winner;
  191. }
  192.  
  193.  
  194. /* This function updates the player details
  195. It
  196. and checks if this number is in the player board, if it is, it switches the place in the board to 'X'
  197. Input: bingoPlayer struct pointer and number randomized
  198. Output: None
  199. */
  200. void updateBoard(bingoPlayer* player, int num)
  201. {
  202.     int i = 0, j = 0;
  203.  
  204.     for (i = 0; i < BOARD_SIZE; i++)
  205.     {
  206.         for (j = 0; j < BOARD_SIZE; j++)
  207.         {
  208.             if ((int)player->board[i][j] == num)
  209.             {
  210.                 player->board[i][j] = HIT;
  211.             }
  212.         }
  213.     }
  214. }
  215.  
  216. /* This function prints a player board
  217. Input: bingoPlayer
  218. Output: None
  219. */
  220. void printBoard(bingoPlayer player)
  221. {
  222.     int i = 0, j = 0;
  223.  
  224.     for (i = 0; i < BOARD_SIZE; i++)
  225.     {
  226.         for (j = 0; j < BOARD_SIZE; j++)
  227.         {
  228.             if (player.board[i][j] == HIT)
  229.             {
  230.                 printf(" X  ");
  231.             }
  232.             else
  233.             {
  234.                 printf(" %2d ", (int)player.board[i][j]);
  235.             }
  236.         }
  237.         printf("\n\n");
  238.     }
  239. }
  240.  
  241. /*
  242. This function prints all players' boards
  243. Input: bingoPlayer
  244. Output: None
  245. */
  246. void printBoards(bingoGame game)
  247. {
  248.     int i = 0;
  249.     for (i = 0; i < game.numPlayers; i++) // changed to .(was ->)
  250.     {
  251.         printf("%s's board:\n", game.players[i].name);//changed to .(was ->)
  252.         printBoard(game.players[i]);//changed to .(was ->)
  253.     }
  254. }
  255.  
  256.  
  257. /* This function checks if a player has won (got a row of HITS)
  258. Input: bingo Player
  259. Output: int - 1 if won, 0 otherwise
  260. */
  261. int hasWon(bingoPlayer player)
  262. {
  263.     int won = FALSE;
  264.     int i = 0, j = 0;
  265.     int counter = 0;
  266.     int flag = FALSE;
  267.     for (i = 0; i < BOARD_SIZE && !won; i++)
  268.     {
  269.         for (j = 0; j < BOARD_SIZE && !won && !flag; j++)
  270.         {
  271.             if (player.board[i][j] == HIT)
  272.             {
  273.                 counter++;
  274.             }
  275.             else
  276.             {//removed the break, used flag insteed
  277.                 flag = TRUE; // if this row has no X, certainly it's not a winner! continue to next row
  278.             }
  279.             if (counter == BOARD_SIZE) // a whole row of X's!
  280.             {
  281.                 won = TRUE;//removed the break, it was useless
  282.  
  283.             }
  284.         }
  285.         flag = FALSE;
  286.         counter = 0;
  287.     }
  288.     return won;
  289. }
  290.  
  291.  
  292.  
  293. /*
  294. Function will perform the fgets command and also remove the newline
  295. that might be at the end of the string - a known issue with fgets.
  296. input: the buffer to read into, the number of chars to read
  297. */
  298. void myFgets(char str[], int n)
  299. {
  300.     fgets(str, n, stdin);
  301.     str[strcspn(str, "\n")] = 0;
  302. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top