Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <Windows.h>
- #define BOARD_SIZE 5
- #define NUMBER_RANGE_MIN 1
- #define NUMBER_RANGE_MAX 75
- #define PLAYER_NAME_LENGTH 20
- #define NUMBERS_IN_BOARD BOARD_SIZE * BOARD_SIZE
- #define HIT 'X'
- #define FALSE 0
- #define TRUE !FALSE
- #define SLEEP_DURATION 500
- int m = 0;
- int round = 0;
- typedef struct bingoPlayer
- {
- char name[PLAYER_NAME_LENGTH];
- char board[BOARD_SIZE][BOARD_SIZE];
- } bingoPlayer;
- typedef struct bingoGame
- {
- int numsDrawn[NUMBER_RANGE_MAX];
- int numOfNumsDrawn;
- bingoPlayer* players;
- int numPlayers;
- } bingoGame;
- void initGame(bingoGame* game, int players);
- void initBingoPlayer(bingoPlayer* pPlayer);
- int updateGame(bingoGame* game, int num);
- int updatePlayer(bingoPlayer* player, int num);
- void printBoard(bingoPlayer player);
- void printBoards(bingoGame game);
- void deleteGame(bingoGame game);
- int hasWon(bingoPlayer player);
- void generateRandomBoard(int* arr);
- int main(void)
- {
- int players = 0, winner = 0;
- bingoGame game = { { 0 }, 0, 0, 0 };
- srand(time(NULL));
- printf("Welcome to BINGO!\nTo start playing, enter number of players: ");
- scanf("%d", &players);
- getchar();
- initGame(&game, players);
- do
- {
- winner = updateGame(&game, rand() % NUMBER_RANGE_MAX + NUMBER_RANGE_MIN);
- } while (!winner);
- deleteGame(game);
- getchar();
- return 0;
- }
- /*
- This function updates the player details it and checks if this number is in the player board,
- if it is, it switches the place in the board to 'X', also it returns 1 if the player got a hit
- Input: bingoPlayer struct pointer and number randomized
- Output: 1 if the player got a hit, 0 if he didnt
- */
- int updatePlayer(bingoPlayer* player, int num)
- {
- int i = 0, j = 0, gotHit = 0;
- for (i = 0; i < BOARD_SIZE; i++)
- {
- for (j = 0; j < BOARD_SIZE; j++)
- {
- if ((int)player->board[i][j] == num)
- {
- gotHit = 1;
- printf("%s, you got a hit! Press any key to continue...", player->name);
- getchar();
- player->board[i][j] = HIT;
- printBoard(*player);
- }
- }
- }
- return gotHit;
- }
- /*
- This function prints a player board
- Input: bingoPlayer
- Output: None
- */
- void printBoard(bingoPlayer player)
- {
- int i = 0, j = 0;
- for (i = 0; i < BOARD_SIZE; i++)
- {
- for (j = 0; j < BOARD_SIZE; j++)
- {
- if (player.board[i][j] == HIT)
- {
- printf(" X ");
- }
- else
- {
- printf(" %2d ", (int)player.board[i][j]);
- }
- }
- printf("\n\n");
- }
- }
- /*
- This function prints all players' boards
- Input: bingoPlayer
- Output: None
- */
- void printBoards(bingoGame game)
- {
- int i = 0;
- for (i = 0; i < game.numPlayers; i++)
- {
- printf("%s's board:\n", game.players[i].name);
- printBoard(game.players[i]);
- }
- }
- /*
- The function initializes a bingoPlayer structure whilist scanning needed values from the user
- input: a pointer to the instance of the structure
- output: none
- */
- void initBingoPlayer(bingoPlayer* pPlayer)
- {
- int i = 0, j = 0, k = 0, randomNumbers[NUMBERS_IN_BOARD] = { 0 };
- generateRandomBoard(randomNumbers);
- printf("enter your name: ");
- fgets(pPlayer->name, PLAYER_NAME_LENGTH, stdin);
- pPlayer->name[strcspn(pPlayer->name, "\n")] = 0;
- for (i = 0; i < BOARD_SIZE; i++)
- {
- for (j = 0; j < BOARD_SIZE; j++, k++)
- {
- pPlayer->board[i][j] = randomNumbers[k];
- }
- }
- }
- /*
- The function initializes a bingoGame structure whilist scanning needed values for the players
- input: a pointer to the instance of the structure, amount of players
- output: none
- */
- void initGame(bingoGame* game, int players)
- {
- int i = 0;
- game->numPlayers = players;
- game->numOfNumsDrawn = 0;
- game->players = (bingoPlayer*)malloc(sizeof(bingoPlayer) * players); m++;
- for (i = 0; i < NUMBER_RANGE_MAX; i++)
- {
- game->numsDrawn[i] = 0;
- }
- for (i = 0; i < players; i++)
- {
- printf("Player %d ", i + 1);
- initBingoPlayer(&game->players[i]);
- printf("Hello %s", game->players[i].name);
- if (players - i > 1)
- {
- printf(" please wait for %d more friend(s)", players - i - 1);
- }
- printf("!\n");
- printf("This is your board:\n");
- printBoard(game->players[i]);
- }
- }
- /*
- The function frees the memory of the players array inside a bingoGame structure
- input: the instance of the structure
- output: none
- */
- void deleteGame(bingoGame game)
- {
- free(game.players); m--;
- }
- /*
- The function generates random unique values for the bingo board
- input: array to put the values inside
- output: none
- */
- void generateRandomBoard(int* arr)
- {
- int numbersArr[NUMBER_RANGE_MAX] = { 0 }, i = 0, place = 0, temp = 0;
- for (i = 0; i < NUMBER_RANGE_MAX; i++)
- {
- numbersArr[i] = i + 1;
- }
- for (i = 0; i < NUMBERS_IN_BOARD; i++)
- {
- place = rand() % (NUMBER_RANGE_MAX - i);
- arr[i] = numbersArr[place];
- temp = numbersArr[NUMBER_RANGE_MAX - i - 1];
- numbersArr[NUMBER_RANGE_MAX - i - 1] = numbersArr[place];
- numbersArr[place] = temp;
- }
- }
- /*
- The function checks if a player has won the game
- input: the instance of a bingoPlayer structure
- output: 1 if he won, 0 otherwise
- */
- int hasWon(bingoPlayer player)
- {
- int i = 0, j = 0, won = 0;
- for (i = 0; i < BOARD_SIZE && won < BOARD_SIZE; i++)
- {
- won = 0;
- for (j = 0; j < BOARD_SIZE; j++)
- {
- if (player.board[i][j] == HIT) won++;
- }
- }
- return won == BOARD_SIZE;
- }
- /*
- The function updates all players in the game and checks if a player has won and prints it
- input: a pointer to the game structure, random number rolled
- output: 1 if a player has won, 0 otherwise
- */
- int updateGame(bingoGame* game, int num)
- {
- int i = 0, winner = 0, gotHit = 0;
- game->numOfNumsDrawn++;
- game->numsDrawn[game->numOfNumsDrawn] = num;
- Sleep(SLEEP_DURATION);
- printf("\n--- Round #%d: The number rolled this round is %d ---\n", ++round, num);
- for (i = 0; i < game->numPlayers; i++)
- {
- if (gotHit == 0)
- {
- gotHit = updatePlayer(&game->players[i], num);
- }
- else
- {
- updatePlayer(&game->players[i], num);
- }
- if (i >= game->numPlayers - 1 && !gotHit)
- {
- printf("No one got a hit this round!\n");
- }
- if (hasWon(game->players[i]))
- {
- printf("\n%s has won!\n", game->players[i].name);
- printBoard(game->players[i]);
- winner = 1;
- }
- }
- return winner;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement