dmilicev

bingo_game_5x5_v1.c

Apr 14th, 2021 (edited)
586
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.94 KB
  1. /*
  2.  
  3.     bingo_game_5x5_v1.c     author: Dragan Milicev
  4.                             https://www.facebook.com/dmilicev
  5.  
  6.     Program is tested on Windows 7 Ultimate.
  7.  
  8.     https://www.ultraboardgames.com/bingo/game-rules.php
  9.  
  10.     Download program from:
  11.  
  12.     bingo_game_5x5_v1.c
  13.     https://mega.nz/file/j84XTYhB#KMmN3jbtAjNZE7FZS-pO-pIfUb1V8pnOhbLUxu1-FoU
  14.  
  15.     bingo_game_5x5_v1.exe
  16.     https://mega.nz/file/epxD3YIC#k2EInTMp6VapGCDBwbLeeIpjYk4HNRQqlPR6EWmvjqE
  17.  
  18.     bingo_game_5x5_v1.zip   ( source C and exe )
  19.     https://mega.nz/file/X9xgFQpJ#gElQZN5RZav_sn2v24gznjFiwYwyUjrizdDl6nVu-mE
  20.  
  21.     You can find all my C programs at Dragan Milicev's pastebin:
  22.  
  23.     https://pastebin.com/u/dmilicev
  24.  
  25. */
  26.  
  27. // I know that the library conio.h is outdated and should not be used, but ...
  28. #include <conio.h>          // it is used here only for function _getch() in function is_game_over()
  29. #include <stdio.h>
  30. #include<stdlib.h>          // for rand()
  31. #include<time.h>            // for random number generator
  32. #include <windows.h>        // tracking cursor all the time, for cursor position functions
  33.                             // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
  34.  
  35. #define DELAY_TIME 3000     // delay time in milliseconds, 0 for no delay time
  36. #define MAX_BINGO_NUMBER 75 // array of unique integers from 1 to MAX_BINGO_NUMBER
  37. #define BINGO_CARD_ROWS 5   // bingo card matrix BC[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS]
  38. #define BINGO_CARD_COLUMNS 5
  39. #define C0_MIN  1           // possible numbers for bingo card columns
  40. #define C0_MAX 15           //  B     I     N     G     O
  41. #define C1_MIN 16           //  0     1     2     3     4       bingo card columns
  42. #define C1_MAX 30           // 1-15 16-30 31-45 46-60 61-75     possible numbers
  43. #define C2_MIN 31
  44. #define C2_MAX 45
  45. #define C3_MIN 46
  46. #define C3_MAX 60
  47. #define C4_MIN 61
  48. #define C4_MAX 75
  49.  
  50.  
  51. // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
  52.  
  53. // make console cursor invisible
  54. void hidecursor()
  55. {
  56.    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  57.    CONSOLE_CURSOR_INFO info;
  58.    info.dwSize = 20;
  59.    info.bVisible = FALSE;
  60.    SetConsoleCursorInfo(consoleHandle, &info);
  61. }
  62.  
  63. // make console cursor invisible
  64. void showcursor()
  65. {
  66.    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  67.    CONSOLE_CURSOR_INFO info;
  68.    info.dwSize = 20;
  69.    info.bVisible = TRUE;
  70.    SetConsoleCursorInfo(consoleHandle, &info);
  71. }
  72.  
  73. // place cursor at position ( x, y ) = ( row, column )
  74. void gotoxy( int x, int y )
  75. {
  76.     COORD coord;
  77.  
  78.     coord.X = x;
  79.     coord.Y = y;
  80.  
  81.     SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
  82. }
  83.  
  84. // return x coordinate (column) of current cursor position
  85. // on failure return -1
  86. int wherex()
  87. {
  88.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  89.  
  90.     if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
  91.         return -1;
  92.  
  93.     return csbi.dwCursorPosition.X;
  94. }
  95.  
  96. // return y coordinate (row) of current cursor position
  97. // on failure return -1
  98. int wherey()
  99. {
  100.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  101.  
  102.     if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
  103.         return -1;
  104.  
  105.     return csbi.dwCursorPosition.Y;
  106. }
  107.  
  108. // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
  109.  
  110. // wait (do nothing) for time in milliseconds
  111. void wait( int milliseconds )
  112. {
  113.     clock_t start_time = clock();   // get start time
  114.  
  115.     // looping (do nothing) till required time is not acheived
  116.     while ( clock() < start_time + milliseconds )
  117.         ;                           // do nothing
  118. } // wait()
  119.  
  120. // generate and return random integer number between lower and upper, including them.
  121. int get_random_integer_from_lower_to_uppper(int lower, int upper)
  122. {
  123.     // generate and return random number between 0 and upper-1
  124.     //return( rand() % upper );
  125.  
  126.     // generate and return random number between 1 and upper
  127.     //return( rand() % upper + 1 );
  128.  
  129.     // generate and return random integer number between lower and upper
  130.     return( (rand() % (upper - lower + 1)) + lower );
  131. } // get_random_integer_from_lower_to_uppper()
  132.  
  133.  
  134. // Returns unique random integer between lower and upper,
  135. // different from numbers in array[] of n integers
  136. int get_unique_number( int array[], int n, int lower, int upper )
  137. {
  138.     int i, number, unique;
  139.  
  140.     if( (upper-lower)< n ) {
  141.         printf("\n Can't find %d unique elements from %d numbers ! \n", n+1, upper-lower+1);
  142.         exit(1);
  143.     }
  144.  
  145.     if( lower >= upper ) {
  146.         printf("\n Lower is greater than or equal than upper ! \n", n+1, upper-lower+1);
  147.         exit(1);
  148.     }
  149.  
  150.     while( 1 ) {
  151.         i=0;                        // reset i
  152.         unique = 1;                 // reset flag unique
  153.         number = get_random_integer_from_lower_to_uppper(lower, upper);
  154.  
  155.         while( i<n && unique ) {    // for all array elements, while unique!=0
  156.             if( array[i] == number ) {
  157.                 unique = 0;         // than number isn't unique in the array[]
  158.             }
  159.             i++;                    // move on to the next element of array[]
  160.         } // while( i<n && unique )
  161.  
  162.         if( unique )                // if number is unique in array[]
  163.             return number;
  164.         else                        // if it isn't unique, get another random integer
  165.             number = get_random_integer_from_lower_to_uppper(lower,upper);
  166.     } // while(1)
  167. } // get_unique_number()
  168.  
  169. // Fills bingo array BN[] with unique random integers between 1 and MAX_BINGO_NUMBER
  170. // These are drawn bingo numbers.
  171. void fill_bingo_array( int BN[MAX_BINGO_NUMBER] )
  172. {
  173.     int i;
  174.     for(i=0; i<MAX_BINGO_NUMBER; i++)
  175.         BN[i] = get_unique_number( BN, i, 1, MAX_BINGO_NUMBER );
  176. } // fill_bingo_array()
  177.  
  178. // Fills the bingo card matrix BC[][]
  179. // with possible unique random integers between 1 and MAX_BINGO_NUMBER
  180. // Possible numbers for bingo card columns
  181. //  B     I     N     G     O
  182. //  0     1     2     3     4       bingo card columns
  183. // 1-15 16-30 31-45 46-60 61-75     possible numbers
  184. void fill_bingo_card( int BC[][BINGO_CARD_COLUMNS] )
  185. {
  186.     int i, r, c;                    // i iterator, r row, c column
  187.     int i0=0;                       // index for array0 of elements in column 0
  188.     int i1=0;
  189.     int i2=0;
  190.     int i3=0;
  191.     int i4=0;
  192.     int array0[BINGO_CARD_ROWS];    // array0 of elements in column 0
  193.     int array1[BINGO_CARD_ROWS];
  194.     int array2[BINGO_CARD_ROWS];
  195.     int array3[BINGO_CARD_ROWS];
  196.     int array4[BINGO_CARD_ROWS];
  197.  
  198.     // fill column arrays with possible unique random integers
  199.     for(i=0; i<BINGO_CARD_ROWS; i++) {
  200.         array0[i] = get_unique_number( array0, i, C0_MIN, C0_MAX );
  201.         array1[i] = get_unique_number( array1, i, C1_MIN, C1_MAX );
  202.         array2[i] = get_unique_number( array2, i, C2_MIN, C2_MAX );
  203.         array3[i] = get_unique_number( array3, i, C3_MIN, C3_MAX );
  204.         array4[i] = get_unique_number( array4, i, C4_MIN, C4_MAX );
  205.     }
  206.  
  207.     // fill bingo card matrix BC[][] from column arrays
  208.     for(r=0; r<BINGO_CARD_ROWS; r++)
  209.         for(c=0; c<BINGO_CARD_COLUMNS; c++)
  210.         {
  211.             if( c == 0 )
  212.                 BC[r][c] = array0[i0++];
  213.             else if( c == 1 )
  214.                 BC[r][c] = array1[i1++];
  215.             else if( c == 2 )
  216.                 BC[r][c] = array2[i2++];
  217.             else if( c == 3 )
  218.                 BC[r][c] = array3[i3++];
  219.             else if( c == 4 )
  220.                 BC[r][c] = array4[i4++];
  221.         }
  222. // In center of bingo card matrix is free number.
  223. // We consider that it has already been drawned and therefore is 0.
  224.     BC[2][2] = 0;
  225. } // fill_bingo_card()
  226.  
  227. // Counts maximum number of zeros in a row, column, or diagonal of a given bingo card.
  228. // Returns the maximum number of zeros.
  229. int count_zeros_in_bingo_card( int BC[][BINGO_CARD_COLUMNS] )
  230. {
  231.     int r, c, counter=0, max_num_of_zeros=0;
  232.  
  233.     // Count zeros in rows
  234.     for(r=0;r<BINGO_CARD_ROWS;r++) {        // count zeros in row r
  235.         counter = 0;                        // reset counter for next row
  236.         for(c=0;c<BINGO_CARD_COLUMNS;c++)   // count zeros in all columns of row r
  237.             if( BC[r][c] == 0 )             // if it is zero
  238.                 counter++;                  // increase counter
  239.  
  240.         if( max_num_of_zeros < counter )
  241.             max_num_of_zeros = counter;     // remember max value
  242.     }
  243.  
  244.     // Count zeros in columns ( be careful )
  245. // https://www.w3resource.com/c-programming-exercises/array/c-array-exercise-25.php
  246.     // ( Attention: not a mistake BC[c][r] )
  247.     for(r=0;r<BINGO_CARD_ROWS;r++) {        // for all rows
  248.         counter = 0;                        // reset counter for next column
  249.         for(c=0;c<BINGO_CARD_COLUMNS;c++)   // // for all columns
  250.             if( BC[c][r] == 0 )             // if it is zero ( Attention: not a mistake BC[c][r] )
  251.                 counter++;                  // increase counter
  252.  
  253.         if( max_num_of_zeros < counter )
  254.             max_num_of_zeros = counter;     // remember max value
  255.     }
  256.  
  257.     // Count zeros on main diagonal
  258.     // Elements of the main diagonal have the property: r == c
  259.     counter = 0;                            // reset counter for main diagonal
  260.     for(r=0;r<BINGO_CARD_ROWS;r++) {
  261.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  262.             if( r == c && BC[r][c] == 0 )   // if it is zero element on main diagonal
  263.                 counter++;                  // increase counter
  264.  
  265.         if( max_num_of_zeros < counter )
  266.             max_num_of_zeros = counter;     // remember max value
  267.     }
  268.  
  269.     // Count zeros on side diagonal
  270.     // Elements of the side diagonal have the property: r+c == BINGO_CARD_ROWS-1
  271.     counter = 0;                            // reset counter for side diagonal
  272.     for(r=0;r<BINGO_CARD_ROWS;r++) {
  273.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  274.             if( r+c == BINGO_CARD_ROWS-1 && BC[r][c] == 0 ) // if it is zero element on side diagonal
  275.                 counter++;                  // increase counter
  276.  
  277.         if( max_num_of_zeros < counter )
  278.             max_num_of_zeros = counter;     // remember max value
  279.     }
  280.  
  281.     return max_num_of_zeros;
  282. } // count_zeros_in_bingo_card()
  283.  
  284. // Displays 4 bingo cards with the current number of hits
  285. void print_bingo_cards( int BC1[][BINGO_CARD_COLUMNS],
  286.                         int BC2[][BINGO_CARD_COLUMNS],
  287.                         int BC3[][BINGO_CARD_COLUMNS],
  288.                         int BC4[][BINGO_CARD_COLUMNS] )
  289. {
  290.     int r, c, x, y;
  291.  
  292.     // 1. Player
  293.     x = 0;
  294.     y = 1;
  295.     gotoxy(x,y);
  296.     printf("     1. Player (%d)", count_zeros_in_bingo_card(BC1) );
  297.     gotoxy(x,wherey()+2);
  298.     for(r=0;r<BINGO_CARD_ROWS;r++) {
  299.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  300.             printf("%4d", BC1[r][c] );
  301.         gotoxy(x,wherey()+2);   // after 5 numbers, go to next line
  302.     }
  303.  
  304.     // 2. Player
  305.     x = 57;
  306.     y = 1;
  307.     gotoxy(x,y);
  308.     printf("     2. Player (%d)", count_zeros_in_bingo_card(BC2) );
  309.     gotoxy(x,wherey()+2);
  310.     for(r=0;r<BINGO_CARD_ROWS;r++) {
  311.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  312.             printf("%4d", BC2[r][c] );
  313.         gotoxy(x,wherey()+2);   // after 5 numbers, go to next line
  314.     }
  315.  
  316.     // 3. Player
  317.     x = 0;
  318.     y = 14;
  319.     gotoxy(x,y);
  320.     printf("     3. Player (%d)", count_zeros_in_bingo_card(BC3) );
  321.     gotoxy(x,wherey()+2);
  322.     for(r=0;r<BINGO_CARD_ROWS;r++) {
  323.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  324.             printf("%4d", BC3[r][c] );
  325.         gotoxy(x,wherey()+2);   // after 5 numbers, go to next line
  326.     }
  327.  
  328.     // 4. Player
  329.     x = 57;
  330.     y = 14;
  331.     gotoxy(x,y);
  332.     printf("     4. Player (%d)", count_zeros_in_bingo_card(BC4) );
  333.     gotoxy(x,wherey()+2);
  334.     for(r=0;r<BINGO_CARD_ROWS;r++) {
  335.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  336.             printf("%4d", BC4[r][c] );
  337.         gotoxy(x,wherey()+2);   // after 5 numbers, go to next line
  338.     }
  339.  
  340.     // delay to slow down the game, the DELAY_TIME is set at the beginning of the program
  341.     wait(DELAY_TIME);
  342. } // print_bingo_cards()
  343.  
  344. // If he finds a bingo card with a maximum of 5 hits, it is game over.
  345. void is_game_over( int BC1[][BINGO_CARD_COLUMNS],
  346.                    int BC2[][BINGO_CARD_COLUMNS],
  347.                    int BC3[][BINGO_CARD_COLUMNS],
  348.                    int BC4[][BINGO_CARD_COLUMNS] )
  349. {
  350.     int p1=0, p2=0, p3=0, p4=0;
  351.  
  352.     if( count_zeros_in_bingo_card(BC1) == 5 )   // winner is 1. player
  353.         p1=1;
  354.     if( count_zeros_in_bingo_card(BC2) == 5 )   // winner is 2. player
  355.         p2=1;
  356.     if( count_zeros_in_bingo_card(BC3) == 5 )   // winner is 3. player
  357.         p3=1;
  358.     if( count_zeros_in_bingo_card(BC4) == 5 )   // winner is 4. player
  359.         p4=1;
  360.  
  361.     if ( p1 || p2 || p3 || p4 ) {               // if we have at least one winner
  362.         gotoxy(0,27);
  363.         printf(" G A M E   O V E R , WINNER:  ");
  364.  
  365.         if ( p1 == 1 )                          // print winners
  366.             printf("  1. Player ");
  367.         if ( p2 == 1 )
  368.             printf("  2. Player ");
  369.         if ( p3 == 1 )
  370.             printf("  3. Player ");
  371.         if ( p4 == 1 )
  372.             printf("  4. Player ");
  373.  
  374.         _getch();                               // used only here to stop the program
  375.         printf("\n");
  376.         exit(0);                                // end of program
  377.     }
  378. } // is_game_over()
  379.  
  380. // On bingo cards BC1, BC2, BC3 and BC4 if a given bingo number exists,
  381. // it is replaced with 0 and bingo cards are printed.
  382. // On each bingo card, it counts the maximum number of hits in
  383. // horizontal lines, vertical lines and on both diagonals.
  384. // If he finds a bingo card with a maximum of 5 hits, game over.
  385. void check_bingo_number( int number,
  386.                          int BC1[][BINGO_CARD_COLUMNS],
  387.                          int BC2[][BINGO_CARD_COLUMNS],
  388.                          int BC3[][BINGO_CARD_COLUMNS],
  389.                          int BC4[][BINGO_CARD_COLUMNS] )
  390. {
  391.     int r, c, printed1=0, printed2=0, printed3=0, printed4=0;
  392.  
  393. // On bingo cards BC1, BC2, BC3 and BC4 if a given bingo number exists,
  394. // it is replaced with 0 and bingo cards are printed.
  395.     for(r=0;r<BINGO_CARD_ROWS;r++)
  396.         for(c=0;c<BINGO_CARD_COLUMNS;c++)
  397.         {
  398.             if( BC1[r][c] == number )
  399.                 BC1[r][c] = 0;
  400.             if( BC2[r][c] == number )
  401.                 BC2[r][c] = 0;
  402.             if( BC3[r][c] == number)
  403.                 BC3[r][c] = 0;
  404.             if( BC4[r][c] == number )
  405.                 BC4[r][c] = 0;
  406.         } // for(c=0;c<BINGO_CARD_COLUMNS;c++)
  407.  
  408.         print_bingo_cards( BC1, BC2, BC3, BC4 );    // bingo cards are printed
  409.  
  410.         // If he finds a bingo card with a maximum of 5 hits, it is game over.
  411.         is_game_over( BC1, BC2, BC3, BC4 );
  412. } // check_bingo_number()
  413.  
  414. int playBingo( int BC1[][BINGO_CARD_COLUMNS],
  415.                int BC2[][BINGO_CARD_COLUMNS],
  416.                int BC3[][BINGO_CARD_COLUMNS],
  417.                int BC4[][BINGO_CARD_COLUMNS],
  418.                int BN[MAX_BINGO_NUMBER]       )
  419. {
  420.     int i, x, y, x1=30;         // x1 is column of first drawn bingo number in line
  421.  
  422.     gotoxy(32,2);               // coordinates of title
  423.     printf("%s", "B I N G O" );
  424.  
  425.     gotoxy(30,22);              // coordinates for author
  426.     printf("%s", "Author: Dragan Milicev" );
  427.     gotoxy(24,24);              // coordinates for author address
  428.     printf("%s", "https://www.facebook.com/dmilicev" );
  429.  
  430.     print_bingo_cards( BC1, BC2, BC3, BC4 );
  431.  
  432.     x = x1;                     // coordinates of first drawn bingo number
  433.     y = 4;
  434.     gotoxy(x,y);
  435.     for(i=0;i<MAX_BINGO_NUMBER;i++) {
  436.         gotoxy(42,2);           // coordinates of drawn bingo number counter in title
  437.         printf("(%2d)", i+1 );  // drawn bingo number counter in title
  438.  
  439.         if( i%5 == 0 ) {        // if there is 5 numbers in line
  440.             x = x1;             // column of first number in line
  441.             y++;                // next line
  442.         }
  443.         gotoxy(x,y);            // go to the particular number position
  444.         printf("%2d", BN[i] );  // display drawn bingo number
  445.         x += 4;
  446.  
  447.         check_bingo_number( BN[i], BC1, BC2, BC3, BC4 );
  448.     }
  449. } // playBingo()
  450.  
  451.  
  452. int main(void)
  453. {
  454.     int i;
  455.     int BC1[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];   // Bingo card matrix
  456.     int BC2[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];
  457.     int BC3[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];
  458.     int BC4[BINGO_CARD_ROWS][BINGO_CARD_COLUMNS];
  459.     int BN[MAX_BINGO_NUMBER];                       // Array of drawn bingo numbers.
  460.     time_t t;                                       // for random number generator
  461.  
  462.     // Intializes random number generator, should only be called once.
  463.     srand((unsigned) time(&t));
  464.  
  465.     system("MODE CON: COLS=80 LINES=33");           // select MODE (size) of Console Window
  466.  
  467.     fill_bingo_card(BC1);                           // fill Bingo card matrices
  468.     fill_bingo_card(BC2);
  469.     fill_bingo_card(BC3);
  470.     fill_bingo_card(BC4);
  471.  
  472.     fill_bingo_array(BN);                           // fill array of drawn bingo numbers
  473.  
  474.     playBingo( BC1, BC2, BC3, BC4, BN );            // let's play
  475.  
  476.     return 0;
  477.  
  478. } // main()
  479.  
RAW Paste Data Copied