Programming for Computing 07 - 08

Chapter: Design

Module Admin   Contents, Index   Schedule  
1: Intro to VB    2: First Program and Projects    3: Variables, assignment, Strings    4: Type Conversion, InputBox, Constants    5:Built-in Functions    6: If - decisions    7: Loops - while, for    Example - find smallest    8: Scopes: local, global    9: Writing Procedures.Parameters, Functions   
10: Objects    11: Design    12: Testing    13: Graphics    14: Controls and events    15: Listboxes    16: Arrays    17: Files    18: The Command Line   

Appendices(links etc)   Additional Lectures    Tutorials (not in printed notes)     Selected solutions (not in printed notes)     Assignments    Additions and Errata   

new

The  schedule page has info on what we will do each week.

Why do we do this...

 


Section 11.0

Top-down design

Here is an example of top-down design.

  • we break the task up into subtasks
  • where a subtask is large we break the subtask itself up into subtasks
  • each subtask becomes a procedure.

The game of noughts and crosses (human vs computer)

Let us assume a simple board representation - 9 items:


        1    2    3

        4    5    6

        7    8    9


These might be 9 buttons, or labels, or images, or simply 9 variables named:
   b1   b2   b3  etc     ( b for board  )
 


Section 11.1

Pseudocode, Program design language (PDL, structured english)

These terms mean the same thing - a mixture of programming language and english. There is no standard for this. Experienced programmers will understand it, as long as you indent, and use understandable keywords such as if/end-if

First, we try for an intial high-level solution, not yet in a programming language. This is often done on paper, not on a computer - so we don't need to worry about the punctuation yet. We might go for:


version 1

get persons move
won?
get computer move
won?
keep repeating this stuff

This is not very good, but it is a start. We can move it a bit closer to VB, by using a proper loop:
version 2
while game not over
    get persons move
    get computer move
end while

(This pattern is even true for chess)

We could code it in VB, using procedures.

E.g we could put:


while gameNotOver()
    getPersonsMove()
    getComputerMove()
end while

'-------------------------------------
private sub getPersonsMove()
    '... stuff to do here...

end sub
'-------------------------------------

...and the rest of the procedures

but there is still work for us to do in the middle of the procedure.

However, our design is not really good, and it would be a mistake (in this case) to code too early.

What is missing?

  • There could be a draw
  • We need to check for game over (win or draw) after a person move, and after a computer move.
Moving back to the PDL version, we can put:
while game not over
    get persons move
    game over?
    get computer move
    game over?
end while
display a "win" or a "draw" message

This is closer, but there is the area of a legal move. if a position is occupied, we cannot use it. There needs to be a check on this.

We could use a procedure (e.g. checkMove ) to do this. We could call it from within getPersonsMove and getComputerMove, as in:

private sub getPersonsMove()
    '... stuff to do here...
    checkMove()

end sub

private sub getComputerMove()
    '... stuff to do here...
    checkMove
end sub

private sub checkMove()
    '... stuff to do here...

end sub

So, the process of top-down design is:
  • we break the task up into subtasks
  • where a subtask is large we break the subtask itself up into subtasks
  • each subtask becomes a procedure.
We might draw draw it as a tree (a structure chart), where each box is a procedure, and an arrowed line shows a call. So, in the following example, getpersonsMove and getComputerMove both call checkMove.
[structurechart.jpg]
structure chart for game

So, if I gave you the above diagram and nothing else, here is what you could tell me:

  • There are 4 procedures
  • Game calls getPersonsMove and getComputerMove
  • getPersonsMove and getComputerMove both call checkMove

Let us take our design a step further: we replace 'game over? ' with an IF, and we think about the state of the game. It can be drawn, computer wins, human wins, or still running.


game = "running"
while game = "running"
    get persons move
    check state
    if game = "running" then   ' not over yet
        get computer move
        check state
        if game = "running" then
            not over yet
        else
            computer's move ends game
        endif
    else
       person's move ends game
    endif
end while
display a "win" or a "draw" message, based on value of game variable

Bear in mind that when we write 'get persons move' this will be coded as a procedure, and it is likely to call up other procedures to do its job (just like a manager gets someone to do part of a task - this person may get other more junior staff to do bits in turn. drawback with top-down design is that it needs experience, preferably in similar problem areas.  


Section 11.2

Top-down implementation

You could code up the whole program based on the above plan, but - guess what - it would not work. All the code is untested. To avoid this, you could write a cut-down version of a procedure, known as a stub. This enables us to run the program - to see if it works - before we finish the whole coding.

Think about the above code. 'Get computer move' might be tricky. It probably involves random numbers in range 1 to 9, checking that a position is empty etc. To get the program working we could use this stub:


private sub getComputerMove()
    move = CInt(inputBox("Enter computer's move: 1 to 9"))
end sub

Here, you - the programmer - would enter the move. You can check if the rest of the program works. When it all works, replace your stub with the proper (random number) version.  


Section 11.3

Bottom-up programming

This approach starts at the very low levels. Often it is frowned upon, because you might end up with a collection of procedures that don't fit together well.

What might a bottom-up approach to oxo be? We might say that we know we will need to check if a win happens, so our first step is to write code to check this (assume we check for an 'x' win)

private sub checkWin()
    if ( (b1="x") and (b2 = "x") and (b3="x") ) or _       ' top row
       ( (b4="x") and (b5 = "x") and (b6="x") ) or _       ' 2nd row

       ...etc

end sub

This pile of IF's has to be done somewhere - even in the top-down version. its just that we plan it first in the bottom-up version.

Bottom-up programming can be useful when you are new to programming - it is less abstract. It might also be useful to explore if something is feasible. For example, say you have been given the job of writing a video editor. How will 'cut' and 'paste' work with massive video files? You might do the code for this part first, so you can see if it is fast enough, rather than leave it till the final stages of the project. Some programmers do top-down and bottom-up together.

 


Section 11.4

Event-driven programming

In VB, we create a form with controls on it (e.g. buttons etc) and attach code to each event. This code can manipulate other controls (e.g put text in textboxes) and can use global and local variables. Note that controls are global to the form.

Where does top-down design come in here? If the task to be performed when (e.g.) a button is clicked, we can put the task in a procedure. If this task is big, we can use more procedures. As an example, if we must do the task 'sort mailing list and send letter' when a button is clicked, we might have:


private sub Button1_click(....)
    sortList()
    sendLetters()
end sub

private sub sortList()
   ...to do

end sub

private sub sendletters()
    createLetters()           ' to do
    addAddressesToLetters()   ' to do
end sub

The program is likely to be easier to follow than if we put all the code within the button-click sub.

It might cross your mind that - if we have 9 buttons on screen for noughts and crosses, how do we avoid having masses of identical code for each button? We can still use procedures, as in


private sub Button1_click(....)
   move = 1           ' global  (or a parameter for handleMove)
   handleMove()
end sub

private sub Button2_click(....)
   move = 2
   handleMove()
end sub

private sub handleMove()
checkState()
if game = "running" then   ' not over yet
    get computer move
    check state
    if game = "running" then
        not over yet
    else
        computer's move ends game
...etc
The While-loop has gone, because user interaction makes the game continue. The getpersonMove procedure is not needed, because that is done when the user clicks a button.  


Section 11.5

XP - Extreme programming

This is a new approach for teams of programmers, becoming popular in industry. It involves these approaches:
  • pair programming - 2 programmers to a PC
  • a client on-site, so the team can ask questions immediately (look at the chapter on testing for an e.g. of this).
  • extensive automated tests, which run without user interaction (so they are easy to run). Testing is done - for example - every day.
  • a no-overtime policy, to avoid stress and errors (hence the term 'extreme' is a misnomer.)
  • incremental changing - a part of a program is written and tested, then new parts are added and tested (as opposed to writing the whole thing in one go, and testing it at the end).
  • welcoming change - in the sense that if the client suggests a change, then XP is flexible enough to handle this - we would not begin the whole project again ,or bin masses of design docs. XP does not put a value on lots of design docs.
 


Section 11.6

Prototyping

In the old days, many programs did not have a complex user-interface (e.g just a screen and keyboard where lines of text could be displayed and typed-in. For major projects, the client would be interviewed and the software company would write a specification of exactly what the program should do. The client was then asked to agree to this.

The problem was that when the client saw the finished program months later, it was the wrong kind of thing (the spec was very technical, and the client could not really follow it?)

Nowadays, to ensure that the client knows what they are getting, a prototype is often created. For programs with a major user-interface, this might be a mock-up of the form, showing what facilities will be there - but they will not actually work. Now the client can be asked:

    'is this what you want?'

If not we modify the prototype. In this way, the specification will be more accurate, and the client is clearer about what they are getting.

In our oxo game, a prototype might show that we need some kind of delay before the computer shows its chosen move. When there is no delay, the computer's move comes immediately after the user's move, making the user look stupid for spending a few seconds on their move. Maybe we display a 'Computer thinking...' message ?

VB is one of the best prototyping languages because forms are easy to create, and there is a huge list of extra controls that can be added.  


Section 11.7

Object-oriented design (OOD)

We will not cover this here, but as a brief introduction, the designer/programmer looks for nouns in the problem. One such noun is the board. This becomes an object.

The next step is to decide what properties the board has - what do we need to do to the board? We probably want to find out the state of a particular board position, and to initialise the board (and other actions as well...

The programmer can code this object, which is accessed like we access controls - with the dot notation. We might then use the board object by:


board.initialise

centreBox = board.getState(5)

We need not know how the board is created (it may be 9 integers, characters, 9 pictures, or a large image map. We do not access it directly, but go through the getState procedure.

Note that the VB controls were planned in an OO way. A button is very complex internally, However, we simply manipulate it by e.g:



Button1.Text = "Go"

problems....to do

Module Admin   Contents, Index