Implementing HiHo! Cherry-O
- The first pass code is here
- Them modified code is here
- Discussion of the first pass
- I decided I needed a file to hold game constants.
- MAX_CHERRIES is needed by both the game and the bucket/tree
- But it does not belong in the bucket or the tree. It is a property of the game.
- I added MAX_PLAYERS to this as well.
- In general, I added a test program for each class/item I created.
- Some of these are good and some not so good.
- This is called Unit Testing and the goal is to test everything BEFORE you integrate it into the final product.
- That way you are debugging one thing at a time.
- Spinner
- I know we discussed various ways to implement a spin
- But in my code, I knew, based on the design, that I would
need to know about the action outside of the spinner.
- So I made an enumated type, ActionT.
- One for each results and an UNKNOWN just to make things easier.
- One to turn an ActionT into a string.
- One to turn an int into an ActionT
- One to compute the "next" ActionT.
- The last two are just casts.
- But I don't want them cluttering up my code.
- I bundled the ActionT and the functions for an ActionT with the spinner.
- This may or may not be the right thing to do.
- But in this case, a spinner is useless without the action.
- So I bundled them.
- I am reasonably proud of spinnerTest
- It fully exercises the spinner.
- It might not do a good job of testing error conditions however.
- Bucket
- I changed the AddCherries function so that it can deal with negative numbers.
- I noticed that add and subtract are now the same thing, so I have subtract call add.
- This is probably a design error.
- Should change the entire thing to be ChangeCherries.
- The bucket test is probably only partial. I only tested adding a +/- 1 cherry.
- Tree
- I had a major "problem" with tree.
- Is there any difference between the tree and the bucket?
- Is there any reason to store a tree?
- I decided no, so I remove the tree class from the design.
- Person
- I renamed some of my person functions.
- You are allowed to have two functions with the same name as long as the function signatures are different.
- A function signature is everything after the return type.
- it includes the function name
- The parameters and parameter types.
- And other items.
- This is called function overloading
- Note we need to pass the spinner by reference in TakeTurn
- I realized that I had a ton of repeated code in the TakeTurn
function
- Move cherries from bucket/tree
- Tell about this.
- So I made a private function to accomplish this.
- This can probably use cleaned up a bit.
- If you spin a bird and the tree is full, you still say you are putting two cherries back on the tree.
- But some simple logic in MoveCherries would clean this up.
- My driver is not the greatest, it is really just a mini game.