Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo.

Similar presentations


Presentation on theme: "CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo."— Presentation transcript:

1 CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo

2 Topics Problem Specification Top-down Design Structure Chart Random number generation Reading: D&D: 5.9

3 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. 1-1516-3031-4546-6061-75 6 15 5 13 9 18 23 17 29 27 31 40 42 34 58 47 51 59 57 70 71 66 62 67

4 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.

5 Winning Positions 6 15 5 13 9 18 23 17 29 27 31 40 42 34 58 47 51 59 57 70 71 66 62 67 6 15 5 13 9 18 23 17 29 27 31 40 42 34 58 47 51 59 57 70 71 66 62 67 6 15 5 13 9 18 23 17 29 27 31 40 42 34 58 47 51 59 57 70 71 66 62 67

6 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.

7 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

8 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

9 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

10 Main Task - Structure Chart

11 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*/

12 Printing a Board (algorithm) FUNCTION print Board (board) { print header for each row { for each column { printcell for that row and column } } }

13 Printing a cell (code) const int MARKED=0; printcell(int cellValue) { if (cellValue == MARKED) { printf (" **") } else { printf ("%4d", cellValue); } }

14 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.

15 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 }

16 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.

17 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 }

18 Generating a random number Use C function rand (). rand() generates an integer between 0 and RAND_MAX.

19 Generating a random number int randomNumberInRange (int min, int max) { return (rand () % (max - min + 1) + min); }

20 Example column range 16-30 (min 16, max 30) Suppose rand() returns 403 max - min + 1 = 15 403 % 15 = 13 13 + 16 = 29 int randomNumberInRange (int min, int max) { return (rand () % (max - min + 1) + min); }

21 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];

22 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; }

23 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 }

24 Updating board (algorithm) updateBoard (board, number) { for each row and column { if cell at row, column is equal to number { set cell to MARKED } } }

25 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: http://www.csse.monash.edu.au/courseware/cse1301


Download ppt "CSE1301 Computer Programming: Lecture 27 Game Programming: Bingo."

Similar presentations


Ads by Google