CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo
Topics Problem Specification Top-down Design Structure Chart Random number generation Reading: D&D: 5.9
Bingo: The Board Cards have random numbers assigned to cells in the ranges shown (each number can appear at most once only.) The central card is marked initially
The Game of Bingo The game master calls out numbers 1-75 randomly. The player places a marker on top of a number that is called if it is on the card. The winner is the first player whose card has a straight line (row, column or main diagonal) filled with markers.
Winning Positions
Example Game Use 5x5 grid in your lecture notes "Randomly" fill in your card, with numbers from the correct ranges. Swap cards with your neighbour.
Bingo Program Requirements To play Bingo with ?? –N players and a game master (the program). Each player has ?? –a bingo card whose entries have been chosen randomly. The game master (program) - does what??? –calls out a number in the range 1-75 at random
Bingo Program Requirements Each player - does what? –covers a cells with a marker if it contains the number called by the game master; –done by the program. The game ends when ?? –one player has markers covering all cells in a row, column, or main diagonal; –checked by the program. At the end of the game ?? –The winner is announced. –The prize is awarded
Main Task - Algorithm fill N game boards randomly print out N game boards while no player has won yet { call out a random number update game boards for all players printout N game boards } announce winner
Main Task - Structure Chart
Data Structures - the Board How do we organise the board? –Hint: how will we access the board? Organise the board around columns and rows. int board [5][5]; /*if cell value is 0, then it is marked*/
Printing a Board (algorithm) FUNCTION print Board (board) { print header for each row { for each column { printcell for that row and column } } }
Printing a cell (code) const int MARKED=0; printcell(int cellValue) { if (cellValue == MARKED) { printf (" **") } else { printf ("%4d", cellValue); } }
Creating the Board FUNCTION Create Board { for each column { generate 5 random numbers in range place them in that column } mark the cell at row 3 in column 3 } Q. Anyone see a problem with this algorithm? A. Random numbers must be unique.
FUNCTION Create Board { for each column { for each row from 1 to 5 { generate a random number in range for that column while random number is a duplicate { generate another number } } place random number in the row } mark the cell at row 3 in column 3 }
Checking for duplicates Use an integer array which server as flags: int numberFlags[75]; All elements are initialised to 0. If a number i is chosen, flag the number as “used” by setting value of numberFlags[i-1] to 1.
Checking for duplicates IsDuplicate (number, flags array) { if (content of flags array at position (number -1) is equal to 1) { return true /* duplicate found */ } return false }
Generating a random number Use C function rand (). rand() generates an integer between 0 and RAND_MAX.
Generating a random number int randomNumberInRange (int min, int max) { return (rand () % (max - min + 1) + min); }
Example column range (min 16, max 30) Suppose rand() returns 403 max - min + 1 = % 15 = = 29 int randomNumberInRange (int min, int max) { return (rand () % (max - min + 1) + min); }
Data structure for players N players: an array with N elements. What is each element to be? struct playerRec { char name[NAMELENGTH]; int board[5][5]; }; typedef struct playerRec Player; Player players[N];
Unique Called Numbers How to ensure that the number has been called out at most once? A boolean array is a good solution. int calledNumbers [75]; int i; /* initialise all to false */ for (i=0; i<75; i++) { calledNumbers[i] = 0; }
Playing one round (algorithm) playOneRound { generate a random number between 1 and MaxBingoInt while the random number has already been called { generate a random number } mark the number as called }
Updating board (algorithm) updateBoard (board, number) { for each row and column { if cell at row, column is equal to number { set cell to MARKED } } }
Summary First, analyse the problem - structure chart - data structures and actions on data Construct modules - main program loop : top-down - I/O: bottom up Bingo program available on the web: