#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;

//prototypes go here!


int getIntegerWithRange(string, int, int);
int getRandomInteger(int , int);

string menuPlayer(); //this fn made strictly for providing and reading in user option- has cout/cin
int playerAttack(string); //but most functions should be built without console out and console in
int dragonAttack(int); //but most functions should be built without cout/cin
void outputStatus(int, int); //this is a fn made strictly for cout
int useItem(string, int);

int main() {
  srand(time(NULL)); rand();
  int playerHP=20, dragonHP=200;
  int numberOfHeals = 1;
  while (playerHP > 0 && dragonHP > 0) {
    string turnType = menuPlayer();
    dragonHP = dragonHP - playerAttack(turnType);
    playerHP = playerHP + useItem(turnType, numberOfHeals);
    if (turnType == "heal") numberOfHeals--;
    playerHP = playerHP - dragonAttack( getRandomInteger(1,100) );
    outputStatus(playerHP, dragonHP);
  }
  //outputStatus(playerHP, dragonHP);  //doesn't use its return value because it is a void function
}

//it is customary to comment judiciously before each function
//describe inputs/outputs of the functions
//preconditions/postconditions of the variables
//and what the function is meant to do


//present a menu of options for the player
//return which option was selected
string menuPlayer() {
  string turn;
  while (turn != "light" && turn != "medium" &&
	 turn != "heavy" && turn != "heal") {
    cout << "Please input your attack strength: " << endl;
    cout << "light - a light attack " << endl;
    cout << "medium - a medium attack " << endl;
    cout << "heavy - a heavy attack " << endl;
    cout << "heal - take a potion!" << endl;
    cin >> turn;
  }
  return turn;
}

//perform a specific attack from the player
//this attack is selected by the menuPlayer
//1 is a light attack with some heal, 2 is a medium attack with no heal, 3 is a heavy attack that also dmgs yourself
//uses randomness to determine amounts inside this function
int playerAttack(string attackType) {
  if (attackType == "light") {
    int n = getRandomInteger(1, 10);
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = sum + getRandomInteger(1, 10);
    }
    return sum;
  }
  if (attackType == "medium") {
    int n = getRandomInteger(1, 5);
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum = sum + getRandomInteger(2, 20);
    }
    return sum;
  }
  if (attackType == "heavy") {
    return getRandomInteger(50, 95);
  }
  return 0;
}

//perform a specific attack by the dragon
//randomness selects the specific attack
//1-5 means do a heavy dmg breathe fireball , 
//6-40 is a medium bite
//41-100 is a light claw
//uses randomness to determine amounts inside this function
int dragonAttack(int rnd) {
  if (rnd <= 5) {
    //heavy
    return getRandomInteger(10, 20);
  }
  else if (rnd <= 40) {
    //medium
    return getRandomInteger(5, 10);
  }
  else {
    //light
    return getRandomInteger(1, 3);
  }
  return 0;
}

int useItem(string turn, int numHeals) {
  if (turn == "heal") {
    if (numHeals > 0) {
      return getRandomInteger(40, 50);
    }
  }
  if (turn == "light") {
    return getRandomInteger(1, 5);
  }
  if (turn == "heavy") {
    return -getRandomInteger(1, 5);
  }
  return 0;
}


/* output the status of the players hp
   and the dragon's hps - including if
   either has died! */
void outputStatus(int php, int dhp) {
  if (php <= 0) {
    cout << "You have died! - Oh Noesz" << endl;
  }
  else {
    cout << "You have " << php << " hps." << endl;
  }

  if (dhp <= 0) {
    cout << "You have slain the dragon! Yayz" << endl;
  }
  else {
    cout << "The dragon still has " << dhp << "hps." << endl;
  }
  return; //you can have a return inside void functions
  //but it may not return a value
}



int getRandomInteger(int min, int max) {
  if (min > max) { int t = min; min = max; max = t; }
  return rand() % (max - min + 1) + min;
}
