crazyDarts.cpp
// Nathan Eloe
// crazyDarts.cpp
// Function definitions for Moe's harebrained darts game
#include "crazyDarts.h"
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
float xCoord (const int gamma)
{
return BOARD_DIST * sin(TO_RADIANS * gamma) / cos(TO_RADIANS * gamma);
}
float yCoord (const int theta, const int gamma)
{
return BOARD_DIST * sin(TO_RADIANS * theta) / (cos(TO_RADIANS * theta) *
cos(TO_RADIANS * gamma));
}
int randAngle(const int numBeers)
{
return ((rand() % (2 * RAND_BEER_FACTOR * numBeers + 1))
- RAND_BEER_FACTOR * numBeers);
}
float vecLength(const float x, const float y)
{
return sqrt(x * x + y * y);
}
void displayThrow(const float x, const float y, const float score,
const string sound)
{
cout << "x = " << x << ", y = " << y << "\tscore = " << score
<< "\t" + sound << endl;
return;
}
int score(const int beers)
{
return beers / 2;
}
float score(const float scores[], const int numScores, const int beers)
{
return average(scores, numScores) / beers;
}
float score(const float scores[], const int numScores)
{
return variance(scores, numScores);
}
void play(float scores[], const int numRounds,
const string sounds[], const int numSounds, const int numBeers)
{
seed();
float x, y;
int gamma, theta;
for (int i=0; i<numRounds; i++)
{
gamma = randAngle(numBeers);
theta = randAngle(numBeers);
x = xCoord(gamma);
y = yCoord(theta, gamma);
scores[i] = vecLength(x,y);
displayThrow(x, y, scores[i],
sounds[min(static_cast<int>(scores[i]), numSounds-1)]);
}
return;
}
void play(float scores[], const int numRounds)
{
seed();
for (int i=0; i<numRounds; i++)
scores[i] = rand() % MAX_RAND_SCOREX10 / 10.0;
return;
}
void seed()
{
static bool isSeeded = false;
if (! isSeeded)
{
isSeeded = true;
srand(time(NULL));
}
return;
}
void displayScore(const float score)
{
cout << "Here's your score:\t" << score << endl;
return;
}
char getYorN()
{
char input;
do
{
cout << "(y/n): ";
cin >> input;
if (input != 'y' && input != 'n')
cout << "Not a y or an n. Try again\n";
} while (input != 'y' && input != 'n');
return input;
}