Title: CSE1301 Computer Programming: Lecture 24 Supplement Teddys Modules
1CSE1301Computer ProgrammingLecture 24 -
SupplementTeddys Modules
2Algorithm Update Player
- 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
-
3Structure Chart Update Player
4Structure Chart Update Player
player
player
update player
board
true or false
board
board
true or false
true or false
check diagonals
check rows
check columns
5Module to mark cell updateBoard()
- /
- 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.
- /
bingo-teddy/player.c
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 (row0row lt BOARD_DIM row)
-
- if (board-gtcellrowcol number)
-
- board-gtcellrowcol 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. /
7Test program for updateBoard()
- include ltstdio.hgt
- include ltstdlib.hgt
- 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
bingo-teddy/test-markcell.c
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")
9Structure Chart Update Player
player
player
update player
called number,board
board
board
true or false
board
mark board
board
true or false
true or false
check diagonals
check rows
check columns
10Module checkBoardRows()
bingo-teddy/player.c
- /
- 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. -
- /
11- int checkBoardRows (BingoBoard board)
-
- int row, col
- int count
- for (row0 row lt BOARD_DIM row)
-
- / Count the number of marked cells in
current row. / -
- count 0
- for (col0 col lt BOARD_DIM col)
-
- if (board-gtcellrowcol MARKED_VAL)
-
- count
-
-
continued...
12continuation...
-
- if (count BOARD_DIM)
-
- return 1
-
-
-
- return 0
13Test program for checkBoardRows()
- include ltstdio.hgt
- include ltstdlib.hgt
- 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
bingo-teddy/test-checkrow.c
14- / Case 1 The board has no winning row. /
- printBoard(theBoard)
- printf("\n")
-
- printf("Result is d\n", checkBoardRows(theBoar
d)) -
- / 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(theBoar
d)) -
- return 0
15Structure Chart Update Player
player
player
update player
checkBoardColums is similar to checkBoardRows.
called number,board
board
board
true or false
board
mark board
board
true or false
true or false
check diagonals
check rows
check columns
16Structure Chart Update Player
I still have to give checkBoardDiagonals some
thought, but updatePlayer needs to call this
module. How can I continue?
player
player
update player
called number,board
board
board
true or false
board
mark board
board
true or false
true or false
check diagonals
check rows
check columns
17Module checkDiagonals()
- /
- 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. -
bingo-teddy/player.c
18- int
- checkBoardDiagonals (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
-
19Structure Chart Update Player
Now I can work on updatePlayer even when
checkDiagonals is not yet finished.
player
player
update player
called number,board
board
board
true or false
board
mark board
board
true or false
true or false
check diagonals
check rows
check columns
20Module updatePlayer()
- /
- 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
bingo-teddy/player.c
21- void
- updatePlayer (int number, PlayerInfo player)
-
- updateBoard(number, (player-gtboard))
-
- if (checkBoardRows((player-gtboard))
- checkBoardColumns((player-gtboard))
- checkBoardDiagonals((player-gtboard)))
-
- player-gtisWinner 1
-
-
22Test program for updatePlayer()
bingo-teddy/test-player.c
- include ltstdio.hgt
- include ltstdlib.hgt
- include "bingo.h"
- include "board.c"
- include "player.c / Teddys functions /
- const int X MARKED_VAL
- int main()
-
-
continued...
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...
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)
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.cell03 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")
26-
- / Case 4 Winning upper-left to lower-right
diagonal. / -
- / EXERCISE /
-
- / Case 5 Winning lower-left to upper-right
diagonal. / -
- / EXERCISE /
-
- return 0
-