160 likes | 309 Views
18: Program design. Introduction. When constructing a software solution a number of elements are addressed (These will become more sophisticated as knowledge and experience grows) The user interface (Form1) The program design Class definition / construction / importation
E N D
Introduction When constructing a software solution a number of elements are addressed (These will become more sophisticated as knowledge and experience grows) The user interface (Form1) The program design • Class definition / construction / importation • Module definition / construction / importation • Interface methods (Form1 code) Testing • Identify inputs • Identify outputs Construct test cases and form test plan
Example Consider the game of noughts and crosses played by two people (not against the computer ….yet!) The required program is to provide a platform to play the game by recording the ‘goes’, identifying a winner and keeping score. Accordingly we may envisage something similar to the game played with pencil and paper: Score: Peter John
Form design Requires some knowledge of the implementation (programming language) to be aware of the facilities available. First thoughts! Questions? Score Player 1 Player 2 Crosses Noughts Are players to be identified? Do players take turns to be noughts and then crosses? Who starts? 3 4 Start Game over – Crosses win Draw Finish
Form design – cont’d Label controls and, if significant, indicate names: Labels PictureBox: picbox Score Player 1 Player 2 Crosses Noughts Textboxes:txtPlayer1Score txtPlayer2Score Label:lblResult 3 4 RadioButtons: rdbPlayer2 rdbPlayer1 Start Label (Flashing) Button:cmdFinish Play Abandon Finish Button: cmdPlay Button: cmdAbandon
Analysis Suppose : The players are identified as Player 1 and Player 2, and Player 1 is identify before the program is run. Player 1 - Crosses start Play will alternate during a game and on the start of subsequent games A game can be abandoned by clicking the ‘Abandon’ button Picture design: Use 50 pixel black and white squareslocated by top left vertex
Class design Identify any class definitions to be imported: • Square Identify any classes to be constructed and specify their members • a (chequered) Boarddisplay areashow: to make the board visible • a Game – to record goes and check for winner display areaentries: an integer array to store goes, -1 for nought and 1 for crossturns: to store number of turns takengetEntry: to retrieve a give entry valueshowSymbol: to draw symbol gameOver: to check for winner and indicate winning line or indicate draw
Class definitions • Board
Class definitions – cont’d • Game
Class definitions – cont’d Game – cont’d
Form1 methods Brainstorm what operations need to carried out and order them: • Initialisation • Set initial values of global (?) variables, E.g. scores • Set up interface • Start game • Initialise variables specific to a particular game, E.g. record of goes, number of turns • Display board • Flash prompt • Manage a ‘go’
Form1 methods – cont’d Decide how methods will be invoked: click-handler, call from within some other method? • Initialisation - MyBase.Load • Start game - cmdPlay • Flash prompt - Timer1.Tick • Manage a ‘go’ - picBox.MouseDown • Check for a win - Game method/function • Identify winner - Game method/function In this way the problem is broken down into smaller subtasks. Tackle each of these subtasks; if necessary, develop their solution in separate projects. Assemble the whole and adjust to work. This might mean going back and modifying some of the constituent components. Run
Review code Revisit the code to • Eliminate redundancy • Make more readable/intelligible by restructuring, breaking large methods into smaller parts, consider introducing functions • Check the need for global variables and where possible make local • Introduce comments if not already present. Perform tests to identify problems, deficiencies, etc.Each time you complete a set of changes, re-run all test cases.
Looking for deficiencies Manage a ‘go’
Example - deficiency What happens if a player selects an occupied cell? What will happen if player continues after game has been won? - Need to identify and prohibit these situations. Accordingly, we have
Deficiency – cont’d and: Run