Download presentation
Presentation is loading. Please wait.
Published byLionel Cross Modified over 9 years ago
2
1 CSE1301 Computer Programming: Lecture 23 Algorithm Design (Part 1)
3
2 Topics Software quality Design principles Production principles Sample Problem: Bingo
4
3 The A-Team: Sample Problem: Bingo I want a program for playing "Bingo"! Amy Dave ??? Mr. Grouch
5
4 Software Quality Features Correctness and Reliability –satisfying the requirements, level of trust Efficiency –obtaining the desired goal with a limited use of resources (such as time or hardware) Modifiability –possibility of adapting software to changes in requirements, and to correct errors Reusability –having modules which may be useful in other applications, thus avoiding repetition
6
5 Design Principles Modular –Divide the system into manageable units. Highly cohesive –Elements inside a module should be highly- related in purpose. Loosely coupled –Maximize independence between modules. –Avoid implicit coupling (such as the use of global variables).
7
6 Production Principles Design top-down –Break problem down into manageable sub- problems. Build bottom-up –Code and test the simplest components first. –Test each component before using them to build more complex components. (continued...)
8
7 Development incrementally –Make simplified versions first. –Add and test one functionality at a time, so that it evolves into a complete system. Document as you go along –System documentation: describes the software's internal composition (e.g. Pre- and Post- conditions of functions, struct definitions, etc.) –User documentation: describes how to use the software. Production Principles
9
8 Write a Program to Play Bingo... Amy Dave ??? How do we start?
10
9 Recall: Software Development Cycle AnalysisDesignImplementTest Maintain documentation of the system throughout the entire cycle.
11
10 Problem specification: What are the requirements of the system? Analysis How do you play the game Bingo? What are the program's tasks in the game?
12
11 What's involved? –Bingo Game Boards –A Jar of numbered balls Who's involved? –The Game Master –An N number of Players (Note: There can be a number of variations of the Bingo game. The A-Team will have to follow Mr. Grouch's version.) The Game of Bingo
13
12 A 5 x 5 grid. Each square is a cell. Bingo: The Game Board "cell" The central cell is marked initially.
14
13 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 The boards have random numbers assigned to cells in the ranges shown (each number can appear at most once only.) Range for each column
15
14 Contains 75 numbered balls. Bingo: The Jar 1 234 5 etc...
16
15 At the start of game: –Puts all balls into the jar. –Shakes the jar to mix. During the game: –Takes a randomly-selected ball from the jar. –Calls out the number on the ball. –Note: The balls taken out are not returned back to the jar until the next game. Bingo: The Game Master
17
16 At the start of game: –Fills the game board randomly (according to rules). During the game: –Marks a cell on the game board if it contains the number called out by the Game Master. –Checks if the board has winning positions-- i.e. if there is a row, column or main diagonal where all the cells have been marked. –Calls out "Bingo!" if it is a winning board. Bingo: The Player
18
17 Bingo: Sample Winning Boards 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
19
18 Winner is the first player who has a winning game board. –Q: What if several players have winning boards? –A: It depends. Usually, the first player to call out "Bingo!" is the winner. Another option is to divide the prize equally among those with winning boards. The game ends when either: –there is a winner, or... –the jar is empty. Bingo: End of Game
20
19 Draw a 5x5 grid on a piece of paper. "Randomly" fill in your board, with numbers from the correct ranges. Swap boards with your neighbour. Example Game
21
20 is winning board? Player Jar true or false rows and columns Game Board pick ball Game Master number call out number mark board number marked cell Data-flow diagram during game:
22
21 ask for N (i.e. the number of players) ask for names of N players initialize player scores to 0 What are the program's tasks? Initialization: At the start of the program...
23
22 loop { ask what the user wants to do if ( user wants to start all over again ) { re-initialize program } else if ( user wants to play ) { play game here } else if ( user wants to see scores so far ) { print all scores } else if ( user wants to quit ) end program /* say goodbye and all that */ } What are the program's tasks?
24
23 main Structure Chart (Draft): Main (shows control coupling only) initialize program print all scores end program options menu play game
25
24 main Structure Chart (Draft): Main (shows control coupling only) initialize program print all scores end program options menu play game
26
25 place and mix all numbers in jar fill N game boards randomly print out all N game boards while ( ( no player has won yet ) and ( the jar is not yet empty ) ) { call out a random number from jar update game boards for all players print out all N game boards } if ( there is a winner ) { announce winner add one to winner’s score } Algorithm to Play Game
27
26 Write a Program to Play Bingo... Amy Dave ??? Help! We’re overwhelmed by the task!
28
27 Write a Program to Play Bingo... Amy Dave ??? Start off with a simpler version of the problem!
29
28 Number of boards per player. –Assume: Only one board per player. Filling game boards randomly. –Should each player have unique boards? –Assume: No need to verify that the players fill their boards out differently. Multiple winning boards. –Assume: All players with winning boards get a score each. Assumptions and Limitations
30
29 Limited functionality. –Version 1: Only one player can play. –Version 2: Allow N players. Possible future enhancements. –Allow several boards per player. –Players can play over network (so that first player with winning board to press “Bingo!” wins). –Graphic user interface so players can mark board themselves. Assumptions and Limitations
31
30 place and mix all numbers in jar fill game board randomly print out game board while ( ( player has not won yet ) and ( the jar is not yet empty ) ) { call out a random number from jar update player’s game board print out game board } if ( player has won ) { announce winner add one to player’s score } Version 1: Algorithm
32
31 Structure Chart (Draft): Version 1 (shows control coupling only) main start play print board call out number update player initialize jar fill board
33
32 update player Structure Chart (Draft): Version 1 (shows control coupling only) mark board check diagonals check rows check columns
34
33 mark board update player check rows check columns check diagonals Structure Chart (Draft): Version 1 (shows control coupling only) Er... I’m doing these modules ???? To be continued...
35
34 Readings Brookshear (5/e or 6/e): Chapter 6 Additional: –S. Ceri, D. Mandrioli and L. Sbattella. The Art and Craft of Computing. Addison-Wesley, 1998. Chapters 20 to 24.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.