Skip to content

Instantly share code, notes, and snippets.

@rhysforyou
Forked from anonymous/function.cpp
Created May 23, 2012 10:41
Show Gist options
  • Save rhysforyou/2774518 to your computer and use it in GitHub Desktop.
Save rhysforyou/2774518 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "function.h"
#include <fstream>
#include <cstdlib>
using namespace std;
bool loadmap ( int &xcoord, int &ycoord , int gamemap[ROWS][COLUM])
{
ifstream ins;
ins.open("map.txt");
if(ins.fail())
return false;
else
{
ins >> xcoord >> ycoord;
srand(time(NULL));
for( int i = 0; i < ROWS; i++)
{ for ( int j= 0; j < COLUM; j++)
{ins >> gamemap[j][i];
if ( gamemap[j][i] == 9)
gamemap[j][i] =rand() % 7+1;
}
}
}
return true;
}
void update(Player &player, int gamemap[ROWS][COLUM])
{
char playerinput;
cout << "what way do you want to go" << endl;
cin >> playerinput;
switch (playerinput)
{
case 'n':
pmove( player, 0,-1, gamemap);
break;
case 's':
pmove( player, 0,+1, gamemap);
break;
case 'e':
pmove( player, +1,0, gamemap);
break;
case 'w':
pmove( player, -1,0, gamemap);
break;
case 'q':
int menuinput;
cout << " Do you want to: \n";
cout << "1) \t Save your current progress \n";
cout << "2) \t Quit the game ? \n";
cout << "3) \t cancel and return to the game ";
cin >> menuinput;
if ( menuinput == 1)
cout << " this has not been implemented yet" << endl;
if ( menuinput == 2)
cout << " this has not been implemented yet" << endl;
if ( menuinput == 3)
cout << " this has not been implemented yet" << endl;
default:
cout << "invalid input case" << endl;
}
encounters( player, gamemap[player.xcoord][player.ycoord]);
gamemap[player.xcoord][player.ycoord]= 0;
}
void pmove (Player &p, int chxcoord, int chycoord, int gamemap[ROWS][COLUM])
{
if ( gamemap[p.xcoord+chxcoord][p.ycoord+chycoord] ==-1 )
cout << " You cannot go this way right now" << endl;
else if ( chxcoord < 0 && p.xcoord==0)
cout << "you cannot go west right now" << endl;
else if ( chxcoord > 0 && p.xcoord== COLUM-1)
cout << "you cannot go east right now" << endl;
else if (chycoord > 0 && p.ycoord== ROWS-1)
cout << "you cannot go south right now" << endl;
else if (chycoord < 0 && p.ycoord== 0)
cout << "you cannot go north right now" << endl;
else if( gamemap[p.xcoord+chxcoord][p.ycoord+chycoord] ==999 )
{ cout << " the game, you just won it " << endl;
p.finish = true;
}
else
{
p.health -=5;
p.xcoord+= chxcoord;
p.ycoord+= chycoord;
cout << "you have " << p.health << "HP left. " << endl;
}
}
bool loadweapon(weapon wepAry[])
{
cout << "loadWeapon called." << endl;
ifstream infile;
infile.open("weapon.txt");
if(infile.fail())
{ cout << " weapon.txt has failed to load" << endl;
return false;
}
int i=0;
while ( !infile.eof() && infile.good())
{ infile.getline(wepAry[i].name, 100,'\n');
infile >> wepAry[i].strength;
infile.ignore(10, '\n');
i++;
}
return true;
}
void encounters( Player &p, int currentsquare)
{
switch (currentsquare)
{
case 0:
break;
case 1:
cout << "bonus! you add 50 strength to " << p.wepAry[p.currentweapon].name << endl;
p.wepAry[p.currentweapon].strength += 50;
cout << "your " << p.wepAry[p.currentweapon].name << "now has " << p.wepAry[p.currentweapon].strength << "strength" << endl;
break;
case 2: case 3: case 4:
monsterbattle(p);
break;
case 5:
cout << "you find a potion and drink it " << endl;
cout << "you gain 100HP!" << endl;
p.health += 100;
cout << "you now have " << p.health << "left" << endl;
break;
case 6:
cout << "you find a potion and drink it " << endl;
cout << "you lose 100HP!" << endl;
p.health -= 100;
cout << "you now have " << p.health << "left" << endl;
break;
case 7:
cout << " you find a trap. you have now died" << endl;
p.health = 0;
p.finish = true;
break;
default:
break;
}
}
void monsterbattle(Player &p)
{
srand(time(NULL));
int monsterhealth = random() % 500 +1;
if ( monsterhealth <15)
monsterhealth= 15;
while ( p.health !=0 && monsterhealth !=0 && p.currentweapon != -1)
{
if (p.currentweapon < 0 || p.currentweapon>=10) cout << p.currentweapon << endl;
cout << "you encounter a monster with " << monsterhealth << "HP." << endl;
cout << "you fight the monster with " << p.wepAry[p.currentweapon].name << endl;
if ( p.wepAry[p.currentweapon].strength > monsterhealth)
{
cout << p.wepAry[p.currentweapon].name << "'s strength is decreases by " << monsterhealth << endl;
p.wepAry[p.currentweapon].strength -=monsterhealth;
monsterhealth= 0;
cout << p.wepAry[p.currentweapon].name << "'s strength is " << p.wepAry[p.currentweapon].strength << endl;
cout << " the monster dies. " << endl;
}
else
{
cout << p.wepAry[p.currentweapon].name << " is destroyed " << endl;
cout << "the monster loses " << monsterhealth << "HP" << endl;
monsterhealth -= p.wepAry[p.currentweapon].strength;
p.wepAry[p.currentweapon].strength = -1;
p.currentweapon = currentweapon(p.wepAry);
cout << " the monster has " << monsterhealth << "HP left. " << endl;
cout << "you lose" << p.health << "HP." << endl;
p.health -= 20;
cout << "you have " << p.health << "HP left" << endl;
}
}
}
int currentweapon ( weapon wepAry[10])
{
for ( int i = 0; i < 10; i++)
if ( wepAry[i].strength > 0)
return i;
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment