You are on page 1of 14

Class Assignment Hare and Tortoise Part 2

Due Date: Weighting: 4 June, 2013 No late submissions accepted. 30%

Specification Version 1.0 (14 May 2013) In this part of the assignment you have the option of working in pairs. If you work alone, you will not receive any special consideration if you are unable to complete the assignment in time. Programming pairs need to be registered with Mike Roggenkamp by email before COB 24 May, 2013. Given 3 weeks to complete this assignment, there will be no extensions granted under any circumstances. If illness or other exceptional circumstances prevent you from submitting this assignment on time, email Mike, m.roggenkamp@qut.edu.au; plan to submit by 1 June, 2013.

Table of Contents
1. 2. 3. 4. 5. 6. 7. Introduction .........................................................................................................................1 The Task..............................................................................................................................1 The Hare and Tortoise Game ..............................................................................................2 The Previous Programmers Notes ....................................................................................3 Where to Start .....................................................................................................................4 Notes ...................................................................................................................................7 Submission Details..............................................................................................................7

Introduction
This assignment aims to give you a real world experience that occurs far too often in the workplace. You have been hired to complete as much as possible a project that has not been fully specified at this stage. You have been given (in your mind) an impossible delivery date. There is some supporting documentation to the project in addition to this document. In order to fulfil your contract you must deliver the prototype project which fulfils the stated functionality by the required date.

The Task
The Good Product Software Company has hired you to complete the implementation of a prototype that is at an early stage of development. It is a Board game, named Hare and Tortoise. The programmer who was to implement the prototype has left the company at short notice and has left behind only the source code of various Classes and some notes regarding the prototype. You are to develop a Windows Form version of the game. You are required to

Use Windows Forms, rather than other GUI technologies such as WPF, XNA, webpages, game engines, etc. To keep this program relatively simple, do not use advanced techniques such as MVC or layered architectures. (If you dont know what some of these acronyms mean, thats fine. Just ignore them.) Develop a GUI layout that is very similar to the screenshots shown over the page.

The above screenshot shows the game with 6 players, at the start of the game. Each of the 6 players has a coloured token (or piece) on the Start square. (If you want to see a bigger picture, right-click on the screenshot, select Format Picture..., then the Size tab, and then increase the Height or Width to 80% or more.).

The Hare and Tortoise Game

This is entirely fictitious board game and bears no resemblance to the fable of the Hare and the Tortoise (by Aesop) or any exsting on-line game of the same name. The game is played with a board consisting of 40 actual squares, a pair of six-sided dice and can be played by 2 or more players up to a limit of 6 (for the purpose of this assignment only). The squares are numbered 1 to 40. The game commences with all players starting off the board. (You can think of the Start as being square 0.) One at a time, the players take a turn. This involves rolling the pair of dice and advancing their respective tokens on the board the required number of squares. When all the players are done with their turn, it is called a round.

Each player has a sum of money, they all start with $100. There are three types of squares on the board: ordinary, bad investment and lottery win. Squares 5, 15, 25 and 35 are Bad Investment squares. The effect when a players token lands on one of them is to subtract $25 from the players current sum of money, if the player has less than $25 his sum is set to zero. The sum cannot go below zero. Squares 10, 20, 30 and 40 are Lottery Win squares. The effect when a players token lands on one of them is to add $10 to that players sum of money. As well the player continues his turn, rolls the dice again. Note: landing on the 40th square the player receives the additional money and the game is finished witout playing another turn. The game continues until a player moves beyond the 40th square, reaching the Finish. (You can think of the Finish as being square 41.) Players cannot move beyond the Finish. When one or more players have finished, the winner will be the player who has the most money at this point of time, even if that particular player hasn't reached the Finish. Note that there may be more than one player with the highest amount of money, in this case they are all winners.

The Previous Programmers Notes


The following text is the introduction to the previous programmers notes about the prototype and its classes. See the document, My Working Notes, for the full set of notes In the Solution Explorer window, the current VS solution contains two projects (or folders, if you like):
The GuiGame project contains incomplete code for running the GUI version of the game the classes in this project are described below. The SharedGameClasses project contains the code which performs the logic of the game.

Because of that, these classes should normally not do any I/O. However, to assist debugging

and to finish the prototype in the time available, an exception is made in the case of the HareAndTortoiseGame class, as discussed in more detail later.

Finer details about the Hare and Tortoise Game board


The screenshot on the page 2 illustrate the basic layout of the GUI, there are some finer details that the screenshot does not show, or does not show clearly. Your experience in Part 1 of this assignment will assist you in completing the design of the GUI. Do not start implementing the GUI until you have finished reading this document and the programmers notes concerning the HareAndTortoiseForm class in My Working Notes. Position the GUI Controls as shown. While you are not expected to get their locations and sizes correct to the very last pixel, your GUI layout should look fairly close to the one given in this specification. No additional dialog boxes or message boxes are to be used (unless you need them to assist you with debugging though they are not to appear in your submitted assignment when it is executed). The game board is displayed by using a TableLayoutPanel. While you will need to tell this TableLayoutPanel how many rows & columns you need dont waste your time adding a Control for each of the 42 (= 7 x 6) squares on the board, in the Design View. It is much easier to do this in your program code later. This will be explained in the programmers notes. The fonts used for labels, etc., are just the normal fonts that the Windows Forms Designer uses, except in the cases of the two larger labels in the upper right-hand part of the form which both use a 16pt, bold, italic Book Antiqua font. The Start and Finish squares have Color.BurlyWood as their background colour. The Win and Lose images shown on the board above are included as part of the prototype given to you. You are not expected to create these images, although you will have to write some code to make them appear on the board. The numbers shown in each square are usually white, but the Bad Investment squares (Lose) are shown in red, and the Lottery Win squares (Win) are shown in black. The Number of Players ComboBox upper right-hand corner allows the user to choose a value between 2 and 6. When using the Windows Forms Designer, you should enter the allowed values 2 through 6 directly, rather than writing program code to insert these values.

When the game is first started, the Number of Players ComboBox always shows 6, the maximum number of players. This value has been chosen to make things a little bit easier for you to program. It is simpler to always construct 6 Player objects when the game starts, and then not use some of them when the user selects a smaller number of players rather than having to dynamically create/delete Player objects. By the way, the Player objects are held in the Players BindingList, in HareAndTortoiseGame.cs. Whenever the user selects a different number of players in the ComboBox, the game is immediately reset, whether the current game is finished or not. This means that the user cannot change the number of players in the middle of a game and then continue the same game. When the game is reset like this, the Start square shows the correct number of tokens for the newly-selected number of players. E.g. if the value 2 is selected, then only two tokens are shown. In the Players DataGridView upper right-hand side only values in the Name column can be changed, by the user(s) typing in their names to replace the default names. The Colour, Money and Winner columns are read-only. They cannot be changed by the user(s) entering/selecting new values. The initially-blank area on the right-hand side is a ListBox that is used to display outputs similar to a Console window. It is included as part of the design, so that you can more easily see whether your GUI components are working correctly or not. Further information on ListBox will be provided. The overall purpose of the three buttons in the right-hand lower corner should be obvious. Each of the buttons can be clicked at any stage of the game, although the Roll dice button should be disabled whenever a game is finished, and re-enabled at the start of the next game. Clicking the Reset button moves all the players tokens back to the Start, clears the ListBox, and resets the information shown in the Players DataGridView. It does not change the value selected in the Number of Players ComboBox.

The user can resize the game by dragging on its borders. (Dont worry if doing this causes some of the square numbers to display strangely.) To support this, most controls should have the default set of values for their Anchor property, which is: Anchor = Top, Left But so that the three buttons stay in the bottom right corner, each should have: Anchor = Bottom, Right And so that the ListBox expands (or shrinks) when the form is resized, it should have: Anchor = Top, Bottom, Left, Right

Where to Start

Use Design View to add controls to the prototype HareAndTortoiseForm, so that it has the overall layout as shown on page2. Dont expect to get your form looking exactly the same as the screenshots, to start with. Some parts the boards squares and the Players DataGridView will initially look quite different. As well as reading what the previous programmer said about this form, you should refer to Lectures 9 & 10 and the related Practicals, for help on using Design View and the experience of Part 1 of this assignment. As stated in previous section 5(a), do not waste your time by adding controls for each of the 42 squares in the board. When you have finished creating the boards TablePanelLayout, you should see a 7 x 6 grid (in Design View only), but not the individual coloured squares. They will come later when you start writing code to initialise the board. Once youve got all (or almost all) the controls added to the form even though some of them appear blank initially, or show the wrong columns its time to start adding some code to make those controls work a bit better. Most of this work will require you to add code to that already provided in HareAndTortoiseForm.cs, so start by trying to make some sense of what is already there. You probably wont understand it all to start with, but make use of the previous programmers notes about the existing code. There are also comments in the code that should help explain the purpose of the methods provided. Adding Code to the Game controls While some parts of the code behind the GUI can be implemented in any order you like, I recommend doing things in the following sequence. It is easier to do most of the steps by assuming there are always 6 players, and allowing for a different number at the end.

Get the board to display all the squares, by adding code to the SetupGameBoard method. To do this, you must construct a SquareControl object for each of the squares in the board, including the Start and Finish squares. Dont try to get all the squares displayed perfectly correct at your first attempt. Getting a board full of blue squares is a good start. Enhance your code so that the Start and Finish squares have the right colour, and that the square numbers are in the correct sequence they should go right-to-left on some rows. You dont need to see the player tokens yet. Modify the code in SquareControl.cs so that the Win and Lose images are shown on the board, and so those squares have red number or black numbers as shown in the screenshots. (If you get stuck on any of this, go on to the next step. Were just making the board look fancier, here.) Create an event-handler for the Roll dice button, so that the output for a round of the game is displayed in the ListBox . This so that you can see that the game is working, even though you dont have player-tokens displayed on the board just yet.

If you havent already done so, use Design View to create the Players DataGridView. (Then add one line of code to your SetupPlayersDataGridView() method so that the playerBindingSource knows that its DataSource is the hareAndTortoiseGame.Players BindingList. By using Design View to set the properties of this DataGridView, you avoid the need to write much code at all. This is one of the reasons that the DataGridView is a very useful way of displaying data. (If you have any problems with this step, you can still do the steps below.) Add code to the ResetGame method so that all 6 player-tokens appear on the Start square, when the game is first opened, and whenever the game is reset. You code should make use of the UpdatePlayersGuiLocations method. You have to write the code inside that method too, but once youve done that youll have a useful method for your ResetGame method and for some other methods too. Create an event-handler for the Reset button. Because of the methods youve already created by this stage, this should now be very easy. Modify your Roll dice buttons event-handler, so that all the players token are moved when you click that button. Because of your existing methods, this should be fairly easy. When you test your game, check that the tokens are moved to the same squares as the ones described in your ListBox, i.e. to the correct squares. Add/change code so that the user-selected value in Number of Players ComboBox works as expected. In particular, only the correct number of player-tokens should appear on the board. As shown in the earlier screenshots of the game with two players, all six players are still shown in the Players DataGridView (for simplicity), but only the correct number of players are updated as the game proceeds. The same applies to the information displayed in the ListBox. implement a simple form of animation that shows each player moving one square at a time. E.g. if Player One is on the Start square and rolls 8 on the dice, then they are displayed as moving to square 1, then to square 2, and so on, until they reach square 8, rather than moving from the Start to square 8 in one go. Player 2 then moves similarly, etc. Information on doing this will be provided at a later date as it is less important than getting the GUI working overall. That is, animation is something that you should only work on once you have most things working first.

The challenge in this assignment is to complete what you can before the due date. Better to hand in a working project which does something correctly than one which does not run.

Notes

Though all care has been taken in the production of this specification, there may be a need to notify by class email any alterations/clarifications to this specification. SO CHECK YOUR EMAIL DAILY. Working in Optional Pairs Ensure that both people in the group are involved and are responsible for doing some part of the assignment. You will be required to record each members contribution in your assignment submission. Do not forget to register your group details via email to Mike before COB on 24 May, 2013. Include the full name and student number of each person in your email.

Submission Details
Will be provided by 27 May 2013 on Blackboard. There is some confusion caused by what appears to be conflicting statements on page 3 of Part 2 Specs regarding a player landing on the 40th square. Hopefully the following will clear up some of the confusion. 1. When all players have rolled the dice, that is referred to as a round (of play)

2. When a player has finished, any other players who have not rolled the dice still gets their turn ( that is, the round has to be completed) 3. Normally when a player lands on a Lottery Win square they receive the additional money and have another turn ( that is, another roll of the dice). However landing on square 40, since another roll of the dice would move you past the 40th square, the game play can assume that this player is finished without actually rolling the dice ( how your code handles this is up to you, no one is going to check this low level code detail) 4. The last paragraph in Section 3 which starts When one or more should have said At the end of a round if one or more players have finished, - to agree with points 1 & 2 above. 5. Players cannot move beyond the Finish means if a player is on square 36 and rolls a total of 9 they would move to square 41 not 45 ( in fact any total more than 3 would get this player to 41, think about it! ) Finally just to make it easier for the tutors to play your game, when the Roll dice button is clicked, a round is played (that is, all players have their turn)

However you should write your game logic so that in the future you could change this so that a button click would only roll the die for the next players turn! Hope this helps Mike

My Working Notes: Hare and Tortoise Project


In the Solution Explorer window, the current VS solution contains two projects (or folders, if you like):
The GuiGame project contains incomplete code for running the GUI version of the game the classes in this project are described below. The SharedGameClasses project contains the code which performs the logic of the game.

Because of that, these classes should normally not do any I/O. However, to assit debugging and to finish the prototype in the time available, an exception is made in the case of the HareAndTortoiseGame class, as discussed in more detail later.

The notes below describe aspects of the GuiGame Project.


The Images folder (in Solution Explorer) contains two .png files that Ive found. These will be used to display the Win and Lose pictures on the game board (but that part not done yet). GuiMain Class GuiMain.cs This class contains a fairly simple Main method which has been generated automatically by Visual Studio. This class is complete. It needs no further work. ListBoxTraceListener Class ListBoxTraceListener.cs This class allows Console-like Write and WriteLine statements to send their output to a list box on the GUI form, instead of the Console window. This class is complete. You dont have to
understand its code. Its usage is explained in the Appendix A. It will not be of any use until you are writing the actual logic of the game in the HareAndTortoiseGame class.

SquareControl Class SquareControl.cs This class is used to display (each) one of the squares in the game board. For each square on the board, there will be a Square object (from the Square class in SharedGameClasses) and a corresponding SquareControl object from this SquareControl class. The Board class already has the former.

This class is a type of GUI Control. Its actually a subtype of the PictureBox Control, which is a standard part of Windows Forms. This class is mostly complete. No extra constants, instance variables or methods should be needed. The bodies of the LoadImageWhenNeeded and SetTextColour methods have to be written.

HareAndTortoiseForm Class HareAndTortoiseForm.cs This class will display the whole game, as shown in the specification. I have started developing the GUI game by using the Windows Forms Designer. Once that design is finished, I plan to add extra code to the existing methods as well as possibly adding some new methods. This is the class that requires the most work, so I describe it from two perspectives: firstly, some aspects of the form itself; secondly the code that will make the form work. The form is at an early stage. You can use F5 to run it, but it doesnt do much yet. Ignore the warnings from the compiler. Ive set the forms size to be 900 x 700 pixels. This is a good size for using Design View to layout the form, and shouldnt be changed. The forms precise size will be altered by program code that Ive already written, so that each of the boards squares is displayed with the correct size. The form contains two controls so far. Both are essential. A SplitContainer which divides the form into two panels: one on the left, for the board; and one on the right, for all the other controls. You cant really see the SplitContainer, just its two panels. The SplitContainer is docked in its parent container, so that its panels occupy the whole of the form, i.e. its Dock property = Fill. (This is the default behaviour when you add a SplitContainer to a form, so you shouldnt have to do anything. But if you accidentally this property, your form will look a mess.) This control is complete. Its position on the form is correct. Do not move it. The Exit button. It has an event-handler that terminates the game. This control is complete.

You will need to add a TableLayoutPanel to the left-hand panel rename the control boardTableLayoutPanell. So that this TableLayoutPanel always occupies the whole of the lefthand panel, you will need to set these three properties: Dock = Fill AutoSize = True AutoSizeMode = GrowAndShrink N.B. set the AutoSize property after you set the Dock property or you will find that your TableLayoutPanel becomes extremely small and difficult to work with. Once youve done that, you can search for the word Uncomment, which occurs in three places in HareAndTortoiseForm.cs ( in Code View) and follow the instructions given there. In one place, the instructions in the code say to delete a line, as well as doing the uncommenting. The line to be deleted (return null) was there merely to allow the code to compile without errors.

The code in HareAndTortoiseForm.cs.


Overall, this code makes use of the HareAndTortoiseGame class, which is part of the SharedGameClasses which is explained towards the end of this document. A HareAndTortoiseGame object is (already) constructed right at the start of the HareAndTortoiseForm class (line 23). So that the form makes proper use of this object, hareAndTortoiseGames properties and/or methods should be used in at least the following methods: the HareAndTortoiseForm constructor, SetupGameBoard, SetupPlayersDataGridView, ResetGame, RefreshPlayersInfoInDataGridView, GetSquareNumberOfPlayer and the event-handler for the Roll dice button. Ive written some of the high-level code in the HareAndTortoiseForm class.

The HareAndTortoiseForm constructor is complete. Likewise, SetupTheGui is almost complete. There are comments in the code describing what else need to be done. Likewise, ResizeGameBoard is complete, once its code has been uncommented, as described above.

Ive also written some useful low-level helper methods that take care of some basic details. These are inside a section of the code that is marked by the comments: START OF LOW-LEVEL METHODS and END OF LOW-LEVEL METHODS. Apart from two Refresh... methods see below these methods can help you by converting one thing into another, e.g. for a given playerNumber, one of these methods will tell you which square the player is on. These methods are all complete, except

for MapSquareNumToScreenRowAndColumn where you need to add a few lines of code, and remove its existing lines. For efficiency reasons, some GUI controls do not immediately change what you see on the screen, whenever your program updates their data. Because your program might be making several changes at the same time e.g. updating several players winnings at the same time it is more efficient for those GUI controls to wait for your program to say that it has finished updating, before they change what you see on the screen. Because this can be a tricky area, Ive written two helper methods for this:
RefreshBoardTablePanelLayout() RefreshPlayersInfoInDataGridView()

While each of these two methods contains only a single line of code, that code is obscure, and these method names make their purpose clearer. Be careful to call the appropriate one of these methods whenever youve updated any of the SquareControl objects in the boards TablePanelLayout, or updated the information for the games players (Money or Winner values). Overall in this class, most of whats missing is the code in the middle. There are three empty methods SetupGameBoard(), SetupPlayersDataGridView() and ResetGame(). The method UpdatePlayersGuiLocations is mostly empty, but Ive included one very important line. Also missing is the event-handling code for the Reset button, the Roll dice button, and for handing the event when the user selects a value in the Number of Players ComboBox. Given the other methods that exist in this class, I think that the event-handling code for the Reset button wont be much work, but the Roll dice button will be more complicated. Thats not surprising, given that this button makes the game work. And the ComboBox looks simple. If you find it useful, you may add a small number of extra methods to this class. No extra constants or instance variables should be needed.

These notes describe aspects of the classes in the SharedGameClasses project.


Die Class Die.cs This class represents a many-sided die (commonly called a dice) with each face having a distinct and unique value between 1 and the number of faces. Future dice games may have any number of faces though the minimum number of faces will be 4. This class is complete, i.e. no other variables, properties or methods need to be added to this class. Square Class Square.cs Represents an ordinary square on the board. It is also the base (parent) class for the LotteryWinSquare class and the BadInvestmentSquare class. Each square has a name which is simply the type of square it is: Ordinary, Start, Finish, Lottery Win or Bad Investment. Each square has a number which is the position (1 ... 40) of the square on the board, except that the Start squares number is 0 and the Finish squares number is 41.

The NextSquare method makes it easier to move from one square to the next. Called repeatedly to move the desired number of squares. The method LandOn in this class does nothing in this version of the game. However, that is not true of the LandOn methods in the subclasses Bad Investment Square and Lottery Win Square. This class is complete, except for the IsFinish method. Bad Investment Square class BadInvestmentSquare.cs A subclass of Square which represents a Bad Investment square on the board. This class is complete. Lottery Win Square class LotteryWinSquare.cs A subclass of Square which represents a Lottery Win square on the board. This class is complete.

Board class Board.cs Represents the board used in the game of Tortoise & Hare. I.e. this class must contain all the squares that make up the board. This class is incomplete. While no other instance variables or properties are required to implement the board, code must be added for initializing the squares including the special squares: Start, Finish, etc which can be all done within the constructor. No additional methods are required in this class. You are allowed (and are encouraged) to define additional constants. Player class -- Player.cs Represents a player of the game. A player has a name, an amount of money and knows which square they are currently on (location), a winner Boolean for recording if this player has won, a token-colour and token-image. Note the parameterless constructor is not to be used and the other constructor initialises the properties of the Player This class is incomplete. No additional instance variables or properties are needed. The bodies of the following methods need to be completed: Player, Play, Move, Debit and ResetToLocation. Note Credit method is complete. Hare and Tortoise Game Class HareAndTortoiseGame.cs Plays the board game of the Hare & Tortoise according to the rules specified earlier. This class puts the whole game together. It contains the game board, two dice, and the players.

The players are stored in a BindingList, which is like an array but is dynamic, so elements (players) can be added or deleted so that the list can grow or shrink in size. It also enables a DataGridView to be used in the GUI. This class is responsible for initializing the players of the game, so it knows the number of players. It also has a Finished property, which can be used to record that the game is over. You will need to implement the bodies of several methods in this class, as well as creating additional methods to support playing the actual game. You are allowed to define additional constants, but additional properties are not needed. Add your additional game playing methods above the line marked //################## Game Play Methods above this line

The method OutputIndividualDetails demonstrates how a ListBoxTraceListener object can output to the ListBox on the GUI. Feel free to use this method and/or write other similar to assist you to debug you code. The method is complete.

You might also like