CS 235 Unit 10 Exercise: Friends

Table of Contents

1. Introduction

This program has already been implemented; it's a small

simulation of worms and a bird. You will be adding a feature utilizing CritterManager, which is a friend of the Critter class. See the document for more details.

  • Practice working with different classes, with one class as a friend to the other.
  • Practice modifying existing code written by somebody else in order to add new features.
Download the starter code here: https://gitlab.com/moosadee/courses/-/tree/main/wip_exercises/starter_code/c3_u10_Friends?ref_type=heads

2. About the program

 | Time: 19 |


         ~ %      

[Bird moved left!]
[Worm1 moved right!]
[Worm2 moved left!]
[Worm3 moved down!]
[Worm4 is dead.]
[Worm5 is dead.]

This program has already been implemented. It is a simple simulation of a bird going after worms. There is no user input required and it runs on its own automatically.

There is a CritterManager and a Critter class, with the CritterManager directly accessing the Critter's member variables, even though they're private. That's because, in the declaration of Critter, it has made CritterManager a friend:

class CritterManager; // Foreward declaration

class Critter

    // ( ... stuff ... )

    friend class CritterManager; // Friends!

Here is the UML diagram of the two classes.


Class Name  
+ Critter()  
+ Update() : void
+ Draw() : void
+ SetGoal( x: int, y: int ) : void
+ HasGoal() : bool
+ CaughtWorm() : void
+ OutputLog() : void
- m_name : string
- m_symbol : char
- m_x : int
- m_y : int
- m_goalX : int
- m_goalY : int
- m_alive : bool
- m_log : string
- m_eaten : int


Class Name  
+ CritterManager()  
+ Run() : void
+ Setup() : void
- Draw() : void
- Delay() : void
- GetRandomX() : int
- GetRandomY() : int
- GetClosestWormIndex() : int
- GetDistance( x1: float, y1: float, x2: float, y2: float ) : float
- m_mapWidth : int
- m_mapHeight : int
- m_timeStamp : int
- REFRESH_RATE : const int
- m_critters : vector<Critter>

The Critter is an animal on the screen, such as a bird or worm. It has these member variables:

  • m_x and m_y, the coordinates of the animal on the screen. \((0, 0)\) is at the top-left of the screen.
  • m_goalX and m_goalY, a coordinate of a goal location to walk to.
  • m_symbol, what symbol should be drawn on the screen to represent the animal. For example, ~ for a worm.

The CritterManager stores a vector of Critters (mcritter) and is responsible for handling the world, and calling Update() and Draw() for all its critters. You can add any member variables or functions to either class as needed. If you have any questions about the assignment, feel free to ask.

3. Making modifications

It is up to you how much or how little you want to modify this program. The main idea is for you to look at how CritterManager is managing its Critters directly, modifying their (x, y) coordinates (which are the variables m_x and m_y). The manager also accesses the critter's name (m_name), and other things. You will get a completion grade for this exercise.

(Video overview: https://www.youtube.com/watch?v=aS5bi1n6hTg)

3.1. Ideas for modifications

You only need to implement one of these, but can implement more if you'd like.

  • Add additional animal types during Setup()

    The animal types are defined by setting the critter's m_name and m_symbol. See the CritterManager's Setup() function for reference.

  • Add a "time alive" counter to the Critter

    Add up by 1 for each time the Critter's Update() function is called while it is alive (m_alive = true)=. Display it in the log.

  • Have the CritterManager create new worms periodically

    For reference on making worms, see CritterManager's Setup().

  • Add an end state

    For example, once all the worms have been eaten, end the program (You can set done to true during the CritterManager's Run() function.)

  • Give different animal types different speeds

    For example, a worm might move every-other-cycle, the bird might move every cycle.

  • Add obstacles that critters can't move on top of

    The critter will need awareness of all other critters on the board, and decide if there is an obstacle in its way before moving.

You can also come up with a different idea of what to update if you'd like.

4. The Helper library

The Helper.hpp / Helper.cpp file contains various useful functions you might want to utilize. Here are some examples:

Action Description
Turn an int/float into a string string str = Helper::ToString( num );
Turn a string into an integer int num = Helper::StringToInt( str );
Does the string contain some text? if ( Helper::Contains( fullText, findMe ) )
Clear the screen Helper::ClearScreen();

Browse through the header file for a complete list of available functionality.

5. Turning in your work

Once you're done making an update of your program, you will just be submitting a presentation… but also a presentational part.

Screen capture video (preferred)
  • Use a screen capture tool to record your console window as your program runs. Make sure that your new functionality is visible.
  • Speak over the video OR add text comments when uploading to let me know what you updated in the program.
  • Highlight the parts of the code that you updated.
Screen capture images
  • Create a Word or LibreOffice Writer document.
  • Take a series of screenshots of your program running. Enough to show off your new functionality.
  • Add text to the document to explain what you updated in the program.
  • Highlight the parts of the code that you updated. (Include as screenshots, preferably)
  • Export as a PDF for upload, please.

Screen capture info:

Author: Rachel Wil Sha Singh

Created: 2023-09-30 Sat 23:18