hw7_fncts.h
/* Programmer: Jennifer Leopold
Date: March 8, 2018
File: hw7_functs.h
Purpose: Constants, type definitions, and function prototypes
used in program that simulates a main character
(BoJack Horseman) randomly encountering various other
characters and collecting cards of different sizes.
*/
#ifndef HW7_FUNCTS_H
#define HW7_FUNCTS_H
#include <iostream>
#include <cstdlib>
using namespace std;
// ***********************************************************
// Type declarations
struct Rectangle // 2D rectangle
{
int length, width;
};
struct Creature // Info about a sea creature
{
string name;
Rectangle card;
bool operator <=(const Creature &other) const
{
return(((card.length <= other.card.length) &&
(card.width <= other.card.width)) ||
((card.width <= other.card.length) &&
(card.length <= other.card.width)));
}
bool operator ==(const Creature &other) const
{
return(((card.length == other.card.length) &&
(card.width == other.card.width)) ||
((card.width == other.card.length) &&
(card.length == other.card.width)));
}
};
// ***********************************************************
// Constants
// # entries in CREATURES array
const int NUM_CREATURES = 10;
const Creature CREATURES[NUM_CREATURES] =
{
{"Wanda Walrus", {2, 6}}, {"Stanley Sardine", {3, 1}},
{"Sylvia Seahorse", {4, 2}}, {"Janie Jellyfish", {1, 10}},
{"Doris Dolphin", {3, 2}}, {"Bob Blobfish", {1, 5}},
{"Sammy Shark", {8, 4}}, {"Walter Whale", {6, 2}},
{"Stevie Salmon", {2, 3}}, {"Sheila Shellfish", {1, 3}}
};
// Possible creature greetings
const int NUM_CREATURE_SAYINGS = 12;
const string CREATURE_SAYINGS[NUM_CREATURE_SAYINGS] =
{"Whazzup?",
"Dude, totally love the horse head and human body combo!",
"Looking for a card (shark)?",
"Are you someone famous?", "Don't trust the salmon!!!",
"Here's some advice: the shark wants you to be his chum!",
"Better take some paper towels if you visit a blobfish!",
"Hey man, got any seaweed?",
"Let me give you my card...my phone number is on the back!",
"I'd gladly pay you Tuesday for a lobster roll today.",
"Just keep swimming, swimming, swimming, ...",
"If you're looking for Nemo, he isn't down here."
};
// Simulation termination criteria
const int MIN_DIFFERENT_CREATURES_TO_SEE = 7;
const int MIN_CARDS_TO_STACK = 4;
const int MAX_NUM_CARDS_TO_COLLECT = 100;
// ***********************************************************
// Function prototypes
// Display a greeting to the user.
// Preconditions: None
// Postconditions: A message has been displayed on the screen.
void greeting();
// Display a sign-off message to the user.
// Preconditions: None
// Postconditions: A message has been displayed on the screen.
void signOff();
// Generate a random number within a specified range.
// Preconditions: Parameter low is <= parameter high.
// Postconditions: A value >= low and <= high is returned.
int myRand(const int low, const int high);
void initCreaturesSeenSoFar(bool creaturesSeenSoFar[],
const int n);
void recordDifferentCreaturesSeen(const int whichCreature,
bool creaturesSeenSoFar[],
const int n,
int &numCreaturesSeenSoFar);
void randomlyOutputCreatureGreeting(const int whichCreature);
void addCreatureCardToCollection(const int whichCreature,
Creature cards[],
int &numCards);
int numCardsStackable(Creature cards[], const int numCards);
void printStackableCards(const Creature cards[],
const int numCards,
const int stackSize);
// ***********************************************************
// Templated functions
// Sorts the elements of the given array into ascending order.
// Preconditions: arraySize should be > 0, and a[0..arraySize-1]
// should contain data. Base type of array (i.e., T) must be
// comparable using > operator.
// Postconditions: Elements of a[0..arraySize-1] are in
// ascending order.
template <typename T>
void sortArray(T a[], const int arraySize)
{
T minValue, temp;
int positionOfMin;
for (int i = 0; i < arraySize-1; i++)
{
// Assume min from position i to end of array is the
// value at position i
minValue = a[i];
positionOfMin = i;
// See if you can find a smaller value from position i+1
// to the end of the array
for (int j = i+1; j < arraySize; j++)
{
if (a[j] <= minValue)
{
minValue = a[j];
positionOfMin = j;
}
}
// Swap the value at positionOfMin with the value at
// position i
temp = a[i];
a[i] = minValue;
a[positionOfMin] = temp;
}
return;
}
#endif