Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- bingo_game_5x5_v1.c author: Dragan Milicev
- https://www.facebook.com/dmilicev
- Program is tested on Windows 7 Ultimate.
- https://www.ultraboardgames.com/bingo/game-rules.php
- Download program from:
- bingo_game_5x5_v1.c
- https://mega.nz/file/j84XTYhB#KMmN3jbtAjNZE7FZS-pO-pIfUb1V8pnOhbLUxu1-FoU
- bingo_game_5x5_v1.exe
- https://mega.nz/file/epxD3YIC#k2EInTMp6VapGCDBwbLeeIpjYk4HNRQqlPR6EWmvjqE
- bingo_game_5x5_v1.zip ( source C and exe )
- https://mega.nz/file/X9xgFQpJ#gElQZN5RZav_sn2v24gznjFiwYwyUjrizdDl6nVu-mE
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- // I know that the library conio.h is outdated and should not be used, but ...
- #include <conio.h> // it is used here only for function _getch() in function is_game_over()
- #include <stdio.h>
- #include<stdlib.h> // for rand()
- #include<time.h> // for random number generator
- #include <windows.h> // tracking cursor all the time, for cursor position functions
- // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
- #define DELAY_TIME 3000 // delay time in milliseconds, 0 for no delay time
- #define MAX_BINGO_NUMBER 75 // array of unique integers from 1 to MAX_BINGO_NUMBER
- #define BINGO_CARD_ROWS 5 // bingo card matrix BC[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS]
- #define BINGO_CARD_COLUMNS 5
- #define C0_MIN 1 // possible numbers for bingo card columns
- #define C0_MAX 15 // B I N G O
- #define C1_MIN 16 // 0 1 2 3 4 bingo card columns
- #define C1_MAX 30 // 1-15 16-30 31-45 46-60 61-75 possible numbers
- #define C2_MIN 31
- #define C2_MAX 45
- #define C3_MIN 46
- #define C3_MAX 60
- #define C4_MIN 61
- #define C4_MAX 75
- // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
- // make console cursor invisible
- void hidecursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = FALSE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // make console cursor invisible
- void showcursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = TRUE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // place cursor at position ( x, y ) = ( row, column )
- void gotoxy( int x, int y )
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
- }
- // return x coordinate (column) of current cursor position
- // on failure return -1
- int wherex()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.X;
- }
- // return y coordinate (row) of current cursor position
- // on failure return -1
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.Y;
- }
- // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
- // wait (do nothing) for time in milliseconds
- void wait( int milliseconds )
- {
- clock_t start_time = clock(); // get start time
- // looping (do nothing) till required time is not acheived
- while ( clock() < start_time + milliseconds )
- ; // do nothing
- } // wait()
- // generate and return random integer number between lower and upper, including them.
- int get_random_integer_from_lower_to_uppper(int lower, int upper)
- {
- // generate and return random number between 0 and upper-1
- //return( rand() % upper );
- // generate and return random number between 1 and upper
- //return( rand() % upper + 1 );
- // generate and return random integer number between lower and upper
- return( (rand() % (upper - lower + 1)) + lower );
- } // get_random_integer_from_lower_to_uppper()
- // Returns unique random integer between lower and upper,
- // different from numbers in array[] of n integers
- int get_unique_number( int array[], int n, int lower, int upper )
- {
- int i, number, unique;
- if( (upper-lower)< n ) {
- printf("\n Can't find %d unique elements from %d numbers ! \n", n+1, upper-lower+1);
- exit(1);
- }
- if( lower >= upper ) {
- printf("\n Lower is greater than or equal than upper ! \n", n+1, upper-lower+1);
- exit(1);
- }
- while( 1 ) {
- i=0; // reset i
- unique = 1; // reset flag unique
- number = get_random_integer_from_lower_to_uppper(lower, upper);
- while( i<n && unique ) { // for all array elements, while unique!=0
- if( array[i] == number ) {
- unique = 0; // than number isn't unique in the array[]
- }
- i++; // move on to the next element of array[]
- } // while( i<n && unique )
- if( unique ) // if number is unique in array[]
- return number;
- else // if it isn't unique, get another random integer
- number = get_random_integer_from_lower_to_uppper(lower,upper);
- } // while(1)
- } // get_unique_number()
- // Fills bingo array BN[] with unique random integers between 1 and MAX_BINGO_NUMBER
- // These are drawn bingo numbers.
- void fill_bingo_array( int BN[MAX_BINGO_NUMBER] )
- {
- int i;
- for(i=0; i<MAX_BINGO_NUMBER; i++)
- BN[i] = get_unique_number( BN, i, 1, MAX_BINGO_NUMBER );
- } // fill_bingo_array()
- // Fills the bingo card matrix BC[][]
- // with possible unique random integers between 1 and MAX_BINGO_NUMBER
- // Possible numbers for bingo card columns
- // B I N G O
- // 0 1 2 3 4 bingo card columns
- // 1-15 16-30 31-45 46-60 61-75 possible numbers
- void fill_bingo_card( int BC[][BINGO_CARD_COLUMNS] )
- {
- int i, r, c; // i iterator, r row, c column
- int i0=0; // index for array0 of elements in column 0
- int i1=0;
- int i2=0;
- int i3=0;
- int i4=0;
- int array0[BINGO_CARD_ROWS]; // array0 of elements in column 0
- int array1[BINGO_CARD_ROWS];
- int array2[BINGO_CARD_ROWS];
- int array3[BINGO_CARD_ROWS];
- int array4[BINGO_CARD_ROWS];
- // fill column arrays with possible unique random integers
- for(i=0; i<BINGO_CARD_ROWS; i++) {
- array0[i] = get_unique_number( array0, i, C0_MIN, C0_MAX );
- array1[i] = get_unique_number( array1, i, C1_MIN, C1_MAX );
- array2[i] = get_unique_number( array2, i, C2_MIN, C2_MAX );
- array3[i] = get_unique_number( array3, i, C3_MIN, C3_MAX );
- array4[i] = get_unique_number( array4, i, C4_MIN, C4_MAX );
- }
- // fill bingo card matrix BC[][] from column arrays
- for(r=0; r<BINGO_CARD_ROWS; r++)
- for(c=0; c<BINGO_CARD_COLUMNS; c++)
- {
- if( c == 0 )
- BC[r][c] = array0[i0++];
- else if( c == 1 )
- BC[r][c] = array1[i1++];
- else if( c == 2 )
- BC[r][c] = array2[i2++];
- else if( c == 3 )
- BC[r][c] = array3[i3++];
- else if( c == 4 )
- BC[r][c] = array4[i4++];
- }
- // In center of bingo card matrix is free number.
- // We consider that it has already been drawned and therefore is 0.
- BC[2][2] = 0;
- } // fill_bingo_card()
- // Counts maximum number of zeros in a row, column, or diagonal of a given bingo card.
- // Returns the maximum number of zeros.
- int count_zeros_in_bingo_card( int BC[][BINGO_CARD_COLUMNS] )
- {
- int r, c, counter=0, max_num_of_zeros=0;
- // Count zeros in rows
- for(r=0;r<BINGO_CARD_ROWS;r++) { // count zeros in row r
- counter = 0; // reset counter for next row
- for(c=0;c<BINGO_CARD_COLUMNS;c++) // count zeros in all columns of row r
- if( BC[r][c] == 0 ) // if it is zero
- counter++; // increase counter
- if( max_num_of_zeros < counter )
- max_num_of_zeros = counter; // remember max value
- }
- // Count zeros in columns ( be careful )
- // https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-25.php
- // ( Attention: not a mistake BC[c][r] )
- for(r=0;r<BINGO_CARD_ROWS;r++) { // for all rows
- counter = 0; // reset counter for next column
- for(c=0;c<BINGO_CARD_COLUMNS;c++) // // for all columns
- if( BC[c][r] == 0 ) // if it is zero ( Attention: not a mistake BC[c][r] )
- counter++; // increase counter
- if( max_num_of_zeros < counter )
- max_num_of_zeros = counter; // remember max value
- }
- // Count zeros on main diagonal
- // Elements of the main diagonal have the property: r == c
- counter = 0; // reset counter for main diagonal
- for(r=0;r<BINGO_CARD_ROWS;r++) {
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- if( r == c && BC[r][c] == 0 ) // if it is zero element on main diagonal
- counter++; // increase counter
- if( max_num_of_zeros < counter )
- max_num_of_zeros = counter; // remember max value
- }
- // Count zeros on side diagonal
- // Elements of the side diagonal have the property: r+c == BINGO_CARD_ROWS-1
- counter = 0; // reset counter for side diagonal
- for(r=0;r<BINGO_CARD_ROWS;r++) {
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- if( r+c == BINGO_CARD_ROWS-1 && BC[r][c] == 0 ) // if it is zero element on side diagonal
- counter++; // increase counter
- if( max_num_of_zeros < counter )
- max_num_of_zeros = counter; // remember max value
- }
- return max_num_of_zeros;
- } // count_zeros_in_bingo_card()
- // Displays 4 bingo cards with the current number of hits
- void print_bingo_cards( int BC1[][BINGO_CARD_COLUMNS],
- int BC2[][BINGO_CARD_COLUMNS],
- int BC3[][BINGO_CARD_COLUMNS],
- int BC4[][BINGO_CARD_COLUMNS] )
- {
- int r, c, x, y;
- // 1. Player
- x = 0;
- y = 1;
- gotoxy(x,y);
- printf(" 1. Player (%d)", count_zeros_in_bingo_card(BC1) );
- gotoxy(x,wherey()+2);
- for(r=0;r<BINGO_CARD_ROWS;r++) {
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- printf("%4d", BC1[r][c] );
- gotoxy(x,wherey()+2); // after 5 numbers, go to next line
- }
- // 2. Player
- x = 57;
- y = 1;
- gotoxy(x,y);
- printf(" 2. Player (%d)", count_zeros_in_bingo_card(BC2) );
- gotoxy(x,wherey()+2);
- for(r=0;r<BINGO_CARD_ROWS;r++) {
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- printf("%4d", BC2[r][c] );
- gotoxy(x,wherey()+2); // after 5 numbers, go to next line
- }
- // 3. Player
- x = 0;
- y = 14;
- gotoxy(x,y);
- printf(" 3. Player (%d)", count_zeros_in_bingo_card(BC3) );
- gotoxy(x,wherey()+2);
- for(r=0;r<BINGO_CARD_ROWS;r++) {
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- printf("%4d", BC3[r][c] );
- gotoxy(x,wherey()+2); // after 5 numbers, go to next line
- }
- // 4. Player
- x = 57;
- y = 14;
- gotoxy(x,y);
- printf(" 4. Player (%d)", count_zeros_in_bingo_card(BC4) );
- gotoxy(x,wherey()+2);
- for(r=0;r<BINGO_CARD_ROWS;r++) {
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- printf("%4d", BC4[r][c] );
- gotoxy(x,wherey()+2); // after 5 numbers, go to next line
- }
- // delay to slow down the game, the DELAY_TIME is set at the beginning of the program
- wait(DELAY_TIME);
- } // print_bingo_cards()
- // If he finds a bingo card with a maximum of 5 hits, it is game over.
- void is_game_over( int BC1[][BINGO_CARD_COLUMNS],
- int BC2[][BINGO_CARD_COLUMNS],
- int BC3[][BINGO_CARD_COLUMNS],
- int BC4[][BINGO_CARD_COLUMNS] )
- {
- int p1=0, p2=0, p3=0, p4=0;
- if( count_zeros_in_bingo_card(BC1) == 5 ) // winner is 1. player
- p1=1;
- if( count_zeros_in_bingo_card(BC2) == 5 ) // winner is 2. player
- p2=1;
- if( count_zeros_in_bingo_card(BC3) == 5 ) // winner is 3. player
- p3=1;
- if( count_zeros_in_bingo_card(BC4) == 5 ) // winner is 4. player
- p4=1;
- if ( p1 || p2 || p3 || p4 ) { // if we have at least one winner
- gotoxy(0,27);
- printf(" G A M E O V E R , WINNER: ");
- if ( p1 == 1 ) // print winners
- printf(" 1. Player ");
- if ( p2 == 1 )
- printf(" 2. Player ");
- if ( p3 == 1 )
- printf(" 3. Player ");
- if ( p4 == 1 )
- printf(" 4. Player ");
- _getch(); // used only here to stop the program
- printf("\n");
- exit(0); // end of program
- }
- } // is_game_over()
- // On bingo cards BC1, BC2, BC3 and BC4 if a given bingo number exists,
- // it is replaced with 0 and bingo cards are printed.
- // On each bingo card, it counts the maximum number of hits in
- // horizontal lines, vertical lines and on both diagonals.
- // If he finds a bingo card with a maximum of 5 hits, game over.
- void check_bingo_number( int number,
- int BC1[][BINGO_CARD_COLUMNS],
- int BC2[][BINGO_CARD_COLUMNS],
- int BC3[][BINGO_CARD_COLUMNS],
- int BC4[][BINGO_CARD_COLUMNS] )
- {
- int r, c, printed1=0, printed2=0, printed3=0, printed4=0;
- // On bingo cards BC1, BC2, BC3 and BC4 if a given bingo number exists,
- // it is replaced with 0 and bingo cards are printed.
- for(r=0;r<BINGO_CARD_ROWS;r++)
- for(c=0;c<BINGO_CARD_COLUMNS;c++)
- {
- if( BC1[r][c] == number )
- BC1[r][c] = 0;
- if( BC2[r][c] == number )
- BC2[r][c] = 0;
- if( BC3[r][c] == number)
- BC3[r][c] = 0;
- if( BC4[r][c] == number )
- BC4[r][c] = 0;
- } // for(c=0;c<BINGO_CARD_COLUMNS;c++)
- print_bingo_cards( BC1, BC2, BC3, BC4 ); // bingo cards are printed
- // If he finds a bingo card with a maximum of 5 hits, it is game over.
- is_game_over( BC1, BC2, BC3, BC4 );
- } // check_bingo_number()
- int playBingo( int BC1[][BINGO_CARD_COLUMNS],
- int BC2[][BINGO_CARD_COLUMNS],
- int BC3[][BINGO_CARD_COLUMNS],
- int BC4[][BINGO_CARD_COLUMNS],
- int BN[MAX_BINGO_NUMBER] )
- {
- int i, x, y, x1=30; // x1 is column of first drawn bingo number in line
- gotoxy(32,2); // coordinates of title
- printf("%s", "B I N G O" );
- gotoxy(30,22); // coordinates for author
- printf("%s", "Author: Dragan Milicev" );
- gotoxy(24,24); // coordinates for author address
- printf("%s", "https://www.facebook.com/dmilicev" );
- print_bingo_cards( BC1, BC2, BC3, BC4 );
- x = x1; // coordinates of first drawn bingo number
- y = 4;
- gotoxy(x,y);
- for(i=0;i<MAX_BINGO_NUMBER;i++) {
- gotoxy(42,2); // coordinates of drawn bingo number counter in title
- printf("(%2d)", i+1 ); // drawn bingo number counter in title
- if( i%5 == 0 ) { // if there is 5 numbers in line
- x = x1; // column of first number in line
- y++; // next line
- }
- gotoxy(x,y); // go to the particular number position
- printf("%2d", BN[i] ); // display drawn bingo number
- x += 4;
- check_bingo_number( BN[i], BC1, BC2, BC3, BC4 );
- }
- } // playBingo()
- int main(void)
- {
- int i;
- int BC1[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS]; // Bingo card matrix
- int BC2[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];
- int BC3[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];
- int BC4[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];
- int BN[MAX_BINGO_NUMBER]; // Array of drawn bingo numbers.
- time_t t; // for random number generator
- // Intializes random number generator, should only be called once.
- srand((unsigned) time(&t));
- system("MODE CON: COLS=80 LINES=33"); // select MODE (size) of Console Window
- fill_bingo_card(BC1); // fill Bingo card matrices
- fill_bingo_card(BC2);
- fill_bingo_card(BC3);
- fill_bingo_card(BC4);
- fill_bingo_array(BN); // fill array of drawn bingo numbers
- playBingo( BC1, BC2, BC3, BC4, BN ); // let's play
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment