



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























- January 22, John Latham This case study presents much of the development of a program to play a snake game, similar to that found on certain old mobile phones. The core game playing functionality is actually left as a staged laboratory exercise.
The intentions of this case study are as follows.
Whilst there is no substitute for writing your own programs, watching the development of another’s is still an extremely effective way of collecting design and programming experience, particularly as the programs can be a little more challenging than those you would be able to write yourself at this stage. (Caveat: this is only true for case studies that you are not supposed to be doing yourself – watching someone else develop code that you are supposed to be doing on your own is disastrous for your learning!)
How much you get from this case study depends on how much you put in. Ideally you should carefully follow every part of it and check your depth of understanding at every opportunity. All confusions or misunderstandings which it may reveal must be dealt with immediately.
The game is similar to snake on mobile phones (which is similar to a game played on Unix over 30 years ago), but with some ‘improvements’.
The game is played by one player, who has the objective of obtaining the highest score possible. The player is in control of a snake which is constantly moving around a square field of cells. The length of the snake is a whole number of cells. At any time, the snake moves in one of the 4 directions, parallel to a side of the square, and the player can change the direction using the 4 arrow keys of the keyboard. If the snake crashes into a side, or into itself, then it is dead, and the game stops.
Also in the field is a single piece of food. When the head of the snake hits this, the food is eaten, and the snake becomes one cell longer. This event increases the score of the player. At
Figure 1: A screen dump of a game. The optional extra feature of a gutter trail is visible behind the snake.
As is usual in the object oriented approach, the program will be developed as a number of separate classes. Identifying the appropriate classes of a program is really an art which can slowly be learned from experience. And like the result of any art, the quality of any choice of classes is subjective (although most people will agree there are many more bad choices then good ones!).
By thinking about our understanding of the game, we can form a list of classes which will appear in the program.
Class list for Snake Game: main model classes
Class Description Cell The game grid is made up of cells. Each cell has a type and a number of attributes. For example, a cell could be clear, or it could be a snake head facing in a particular direction, etc.. Direction The snake in the game moves in one of four directions. This non-instantiable class provides those four directions, and various methods to manipulate them (such as obtaining the opposite of a direction). Game This is the main behaviour of the game play. Its primary purpose is to provide a method for making one move of the game.
We will also require some other classes to support the operating framework, including the following.
Class list for Snake Game: operating support classes
Class Description GameGUI This will provide a user interface so that the player can see the snake and the food, etc., and be able to operate the game via the keyboard. It will also be the driving force for the game, that is, it shall contain a method to play the game by repeatedly invoking the move method from the Game class. SpeedController A speed controller is something which controls the speed of the game, to make it easier or harder for the player as they choose. This will be achieved rather simply by the program calling a method to cause a delay between every move. A controller will have various speed settings, the ability to increase or decrease speed, and to pause and resume the game. AboutBox This will provide a pop-up box which can display information about the program and help on how to use it.
Finally, we shall need a class to contain the main method.
Class list for Snake Game: top level class
Class Description Snake This is the top level class, containing only the main method. This shall simply create an instance of GameGUI, invoke its game playing method and print out the score once the game has finished.
set initial game state while user has not asked to quit { speedController.delay(by some amount) moveValue = some function of (speedController.getCurrentSpeed()) game.move(moveValue) update the game’s image } return game.getScore()
A GameGUI will also interpret the key presses from the player, and invoke the appropriate methods: e.g, to get the SpeedController to change speed or pause and resume; to create and show the AboutBox, to change the direction of the snake in the Game, etc..
A Game will primarily offer the method move(). This will cause the game to change state, progressing by one move. The new state will depend on the settings of the old state. Where is the head of the snake? What direction is it facing? Where is the food? Etc.. There will also be a method setSnakeDirection() which will be called from the GameGUI when the player presses one of the arrow keys. There will be other methods too, such as getScore(), cheat() and toggleTrees(). The state of the game will be represented by a number of instance variables, the most obvious one being a 2-dimensional array of Cells to make up the field.
A SpeedController will have a number of methods, such as speedUp(), slowDown(), pause(), resume() and, most importantly, delay().
A Cell will store information, such as is there a piece of snake in the cell? If so, what direction is it facing? Or, is there a piece of food here? Or a tree? Etc.. It will have accessor method s to access and mutator method s to mutate the information it stores, so the state of a cell can be seen and changed.
The Direction class will be non-instantiable, it will simply provide 4 directions, and some useful methods to manipulate them. For example, the method xDelta() will return what value you need to add to an x coordinate, if you want to make a movement in a certain direction given as its argument. (The amount returned will be -1, 0 or 1.) This combined with the similar yDelta() will make the new position of the head of the snake easy to compute from the direction it is facing.
An AboutBox will simply pop up to display some text given to it when it is created, and provide a dismiss button to make it go away. This will be used to provide some help to the player.
Let us analyse the anticipated dependencies between these classes.
So, we plan to develop the classes in the following order: Direction, Cell, Game, SpeedController, AboutBox, GameGUI, and finally Snake.
In general, programs have some kind of behaviour and an interface connecting the behaviour to the outside world. The behaviour is often referred to as the model , as it is this that must implement the real world functionality required of the program.
The interface to a program can be divided into two categories: output and input, often referred to as view and control , respectively.
There is much debate in the O.O. world about these three functions of a program. The so-called M.V.C. argument (standing for model , view and control ) suggests there is much to be gained by separating the 3 functions into different classes in the final program. On the other hand, some people prefer to combine them – there are no hard and fast rules.
Perhaps the main reason for separating the parts is that it more easily allows certain parts to be changed without affecting the others. For example, somebody might find a better way of modelling the real world requirements, but is quite happy with the existing user interface. Or someone might make a better interface for an existing program which is otherwise quite ac- ceptable. The inherent flexibility can be exploited from the initial design too. For example, a program might have a number of different interfaces to support different operating envi- ronments, whilst having only one model part. Perhaps the most obvious examples are those programs which have both a text and a graphical interface.
The most persuasive argument for separating the model from the other two parts in a learning environment is that it permits us to study quite complex programs long before we need to discover the details of complex graphical user interfaces: we can simply hide the specifics of the control and view parts of the program at this point in time.
Class list for Snake Game: image classes
Class Description SpeedControllerImage Provides the image and control for speed controllers.
Having made the decision to develop separate classes for model and view, we next must figure out the way we would like to do it. For example, for each instance of Cell which we create, we will also need to create an instance of CellImage. We must decide on a strategy for keeping track of the relationship between corresponding instances of these classes. There are numerous ways we could do this. The approach we have chosen in this case study has actually been taken so that you can develop the Game class without having to worry about view and control classes! So, if we were not in such a learning context, we might have taken a different approach.
Here is the policy we shall use in this case study.
As suggested above, the attraction of this strategy is that the model classes for the game and the cells need to have absolutely no knowledge of the image classes. This is particularly persuasive given that the Game class is to be written by you!
A second attraction is that we can also hide most of the details of the image and control classes for this current discussion.
Figure 2 shows how the classes will be used in the program. The diagram is simplified, and in particular ignores the class Direction.