1 cse1301 computer programming: lecture 24 - supplement teddy’s modules

26
1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

Upload: austen-conley

Post on 13-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

1

CSE1301Computer Programming:Lecture 24 - Supplement

Teddy’s Modules

Page 2: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

2

module to mark/update

board

find called number on the board if ( number is on the board ){ mark the cell}

if ( ( there is a row of marked cells )or ( there is a column of marked cells )or ( there is a diagonal of marked cells ) )

{ set flag to indicate player is a winner}

Algorithm : Update Player

Page 3: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

3

Structure Chart : Update Player

mark board

check diagonals

check rows

check columns

board

called number,

boardtr

ue o

r fa

lse

boar

d

true or falseboard

true or false

board

pla

yer

update player

pla

yer

Page 4: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

4

Structure Chart : Update Player

mark board

check diagonals

check rows

check columns

board

called number,

boardtr

ue o

r fa

lse

boar

d

true or falseboard

true or false

board

pla

yer

update player

pla

yer

Page 5: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

5

/* * NAME: * void updateBoard (int number, BingoBoard *board) * * DESCRIPTION: * Marks the position of `number' on the board, if it is on * the board. * * PRE: * It assumes that `number' is within range 1 to MAX_VAL, * and that `board' points to a struct which has been * initialised appropriately so that every number appears * only at most once on the board. * * POST: * The instance pointed to by `board' is changed. * The cell which used to contain `number' is changed * to the special value MARKED_VAL. */

Module to mark cell: updateBoard()bingo-teddy/player.c

Page 6: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

6

void updateBoard (int number, BingoBoard *board){ int row, col; /* Determine in which column the number would be. */ col = number / ( MAX_VAL / BOARD_DIM );

/* Look for the number in that column. */ for (row=0;row < BOARD_DIM; row++) { if (board->cell[row][col] == number) { board->cell[row][col] = MARKED_VAL;

/* Since a number can appear only at most once on the board, * we can return to the calling function as soon as we have * found and marked the cell. */

return; } }}

Page 7: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

7

#include <stdio.h>#include <stdlib.h>

#include "bingo.h"#include "board.c"#include "player.c"

const int X = MARKED_VAL;

int main(){ /* We'll fill the board up just for testing. */

BingoBoard theBoard = {{ {1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65} }};

Test program for updateBoard()bingo-teddy/test-markcell.c

Page 8: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

8

/* Case 1: The called number is on the board. */

updateBoard(3, &theBoard); printBoard(&theBoard); printf("\n"); updateBoard(31, &theBoard); printBoard(&theBoard); printf("\n"); /* Case 2: The called number is NOT on the board. */

updateBoard(6, &theBoard); printBoard(&theBoard); printf("\n"); updateBoard(30, &theBoard); printBoard(&theBoard); printf("\n"); return 0;}

Page 9: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

9

Structure Chart : Update Player

mark board

check diagonals

check rows

check columns

board

called number,

boardtr

ue o

r fa

lse

boar

d

true or falseboard

true or false

board

pla

yer

update player

pla

yer

Page 10: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

10

/* * NAME: * int checkBoardRows (BingoBoard *board) * * DESCRIPTION: * Check the board for a winning row; i.e. a row where * all the cells have been marked. * * PRE: * Assumes `board' contains only valid values, and that * a cell with value MARKED_VAL indicates that the cell * has been marked. `board' should be pointing to an * instance of a BingoBoard struct. * * POST: * Returns 1 if there is a winning row; 0 otherwise. * */

Module: checkBoardRows()bingo-teddy/player.c

Page 11: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

11

int checkBoardRows (BingoBoard *board){ int row, col; int count;

for (row=0; row < BOARD_DIM; row++) { /* Count the number of marked cells in current row. */ count = 0;

for (col=0; col < BOARD_DIM; col++) { if (board->cell[row][col] == MARKED_VAL) { count++; } }

continued...

Page 12: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

12

if (count == BOARD_DIM) { return 1; } } return 0; }

continuation...

Page 13: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

13

#include <stdio.h>#include <stdlib.h>

#include "bingo.h"#include "board.c"#include "player.c"

const int X = MARKED_VAL;

int main(){ /* We'll fill the board up just for testing. */

BingoBoard theBoard = {{ {1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65} }};

Test program for checkBoardRows()bingo-teddy/test-checkrow.c

Page 14: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

14

/* Case 1: The board has no winning row. */

printBoard(&theBoard);

printf("\n");

printf("Result is %d\n", checkBoardRows(&theBoard));

/* Case 2: The board has one winning row. */

updateBoard(3, &theBoard);

updateBoard(18, &theBoard);

updateBoard(48, &theBoard);

updateBoard(63, &theBoard);

printBoard(&theBoard);

printf("\n");

printf("Result is %d\n", checkBoardRows(&theBoard));

return 0;

}

Page 15: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

15

Structure Chart : Update Player

mark board

check diagonals

check rows

check columns

board

called number,

boardtr

ue o

r fa

lse

boar

d

true or falseboard

true or false

board

pla

yer

update player

pla

yer

checkBoardColums is similar to

checkBoardRows.

Page 16: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

16

Structure Chart : Update Player

mark board

check diagonals

check rows

check columns

board

called number,

boardtr

ue o

r fa

lse

boar

d

true or falseboard

true or false

board

pla

yer

update player

pla

yer I still have to give

checkBoardDiagonals some thought, but

updatePlayer needs to call this module.

How can I continue?

Page 17: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

17

/* * NAME: * int checkBoardDiagonals (BingoBoard *board) * * DESCRIPTION: * Check the board for a winning main diagonal; i.e. a * main diagonal (either upper-left to lower-right, or * lower-left to upper-right) where all the cells have * been marked. * * PRE: * Assumes `board' contains only valid values, and that * a cell with value MARKED_VAL indicates that the cell * has been marked. `board' should be pointing to * an instance of BingoBoard. * * POST: * Returns 1 if there is a winning diagonal; 0 otherwise. * * */

Module: checkDiagonals()bingo-teddy/player.c

Page 18: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

18

intcheckBoardDiagonals (BingoBoard *board){ /* ** This is the dummy version of this function. ** ** Teddy hasn't figured this one out yet, so ** at the moment, this function always returns false. ** ** EXERCISE: Write the code for this function. */ return 0;}

Page 19: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

19

Structure Chart : Update Player

mark board

check diagonals

check rows

check columns

board

called number,

boardtr

ue o

r fa

lse

boar

d

true or falseboard

true or false

board

pla

yer

update player

pla

yer

Now I can work on

updatePlayer even when

checkDiagonals is not yet

finished.

Page 20: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

20

/* * NAME: * void updatePlayer (int number, PlayerInfo *player) * * DESCRIPTION: * Given the number called by the Game Master, the player's * board is marked. It then checks if the board has a * winning row, column or diagonal. If so, a flag is set * to indicate that this player is a winner, and adds one * to the player's score. * * PRE: * `number' is assumed to be in the range 1 to MAX_VAL. * `player' should be a pointer to an instance of * PlayerInfo, which should have been initialised * using newPlayer(). * * POST: * Changes the record pointed to by `player' with the * player's updated board, win flag, and score. */

Module: updatePlayer()bingo-teddy/player.c

Page 21: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

21

voidupdatePlayer (int number, PlayerInfo *player){ updateBoard(number, &(player->board)); if (checkBoardRows(&(player->board)) || checkBoardColumns(&(player->board)) || checkBoardDiagonals(&(player->board))) { player->isWinner = 1; }}

Page 22: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

22

#include <stdio.h>#include <stdlib.h>

#include "bingo.h"#include "board.c"#include "player.c” /* Teddy’s functions */

const int X = MARKED_VAL;

int main(){

Test program for updatePlayer()

continued...

bingo-teddy/test-player.c

Page 23: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

23

/* We'll fill the player info up just for testing. */

PlayerInfo thePlayer = { /* board */ { {{1, 16, 31, 46, 61}, {2, 17, 32, 47, 62}, {3, 18, X, 48, 63}, {4, 19, 33, X, 64}, {5, 20, 34, 50, 65}} },

/* name */ "Teddy",

/* isWinner */ 0 };

continued...

Page 24: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

24

/* Case 1: The board has no winning row, column or diagonal. */ updatePlayer(48, &thePlayer); printBoard(&(thePlayer.board)); printf("\n");

printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner); /* Case 2: The board has one winning column, no winning row

or diagonal. */

updateBoard(46, &(thePlayer.board)); updateBoard(47, &(thePlayer.board));

updatePlayer(50, &thePlayer); printBoard(&(thePlayer.board)); printf("\n");

printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner);

Page 25: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

25

/* In preparation for testing the next case, change row 1 * column 4 back to 46 so we don't have a winning column, * and reset value of isWinner. */ thePlayer.board.cell[0][3] = 46; thePlayer.isWinner = 0; printBoard(&(thePlayer.board)); printf("\n"); printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner); /* Case 3: The board has one winning row, no winning column

or diagonal. */

updateBoard(3, &(thePlayer.board)); updateBoard(18, &(thePlayer.board)); updatePlayer(63, &thePlayer); printBoard(&(thePlayer.board)); printf("\n");

printf("thePlayer.isWinner is %d\n\n", thePlayer.isWinner);

Page 26: 1 CSE1301 Computer Programming: Lecture 24 - Supplement Teddy’s Modules

26

/* Case 4: Winning upper-left to lower-right diagonal. */ /*** EXERCISE ***/ /* Case 5: Winning lower-left to upper-right diagonal. */ /*** EXERCISE ***/ return 0;}