1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Program: map.cpp
//Description:2-dimensional array game that has money, fighting, and a shop.
//Date: 1/13/2009
#include "stdafx.h"
using namespace std;
void display (char show[16][22])
{
for (int r=0; r<16; r++)
{
for(int c=0;c<22;c++)
cout<<show[r][c];
cout<<endl;
}
}
int findmoney (int &money)
{
int randmoney, moneyamount;
randmoney=rand()%(10-1)+1;
if (randmoney==7)
{
moneyamount=rand()%(200-1)+1;
money=money+moneyamount;
system ("cls");
cout<<"You found"<<moneyamount<<". Your total is $"<<money<<". "<<endl;
}
return money;
}
int attack (int &yhealth)
{
int ehealth=20, ydamage=0, edamage=0, attack=0;
time_t seconds;
time (&seconds);
srand ((unsigned int)seconds);
char choice='x';
attack=rand()%(20-1)+1;
if (attack==10)
{
system ("cls");
cout<<"You are being attacked!"<<endl;
cout<<"Press 'a' to attack or 'r' to run."<<endl;
choice=getch();
system("cls");
if (choice=='a')
{
}
if (choice=='r')
{
system ("cls");
cout<<"You escaped from the enemy!"<<endl;
}
}
return yhealth;
}
int info(int &yhealth, int &ydamage, int &ehealth, int &edamage)
{
yhealth=100;
ehealth=50;
while (ehealth>0)
{
system ("cls");
cout<<"Enemy's health= "<<ehealth<<endl;
cout<<"Your health= "<<yhealth<<endl;
edamage=rand()%(20-1)+1;
yhealth=yhealth-edamage;
cout<<"You were attacked. You lost "<<edamage<<"health points."<<endl;
cout<<"Your total health is"<<yhealth<<"."<<endl;
system ("pause");
ydamage=rand()%(20-1)+1;
ehealth=ehealth-ydamage;
system ("cls");
cout<<"You attacked your enemy. They lost "<<ydamage<<"health points."<<endl;
system ("pause");
}
return ehealth;
}
int _tmain(int argc, _TCHAR* argv[])
{
char map[16][22]=
{{'0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9','0','|'},
{'1','$','$','$','$',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','@','@','@','@','|'},
{'2','$','$','$','$',' ',' ',' ',' ',' ',' ',' ','R',' ',' ',' ',' ','@','@','@','@','|'},
{'3','$','$','$','$',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','@','@','@','@','|'},
{'4',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'5',' ',' ',' ',' ',' ',' ',' ','^',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'6',' ',' ',' ',' ',' ',' ','^',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'7','^','^','^','^','^','^',' ',' ',' ',' ',' ',' ',' ','T',' ',' ',' ',' ',' ',' ','|'},
{'8',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'9',' ',' ',' ',' ',' ','T','T','T','T','T','T','T','T','T',' ',' ',' ',' ',' ',' ','|'},
{'0',' ',' ',' ',' ',' ','T','T','T','T','T','T','T','T','T','T',' ',' ',' ',' ','O','|'},
{'1',' ',' ',' ',' ',' ','T','T','T','T','T','T','T','T',' ',' ',' ',' ',' ','O','O','|'},
{'2','~',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','O','O','O','|'},
{'3',' ','~',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','O','O','O','O','|'},
{'4',' ',' ','~','~','~','~','~','~','~',' ',' ',' ',' ',' ',' ','O','O','O','O','O','|'},
{'5','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'}};
cout<<endl;
char move='b';
int x=4, y=4, yhealth=100;
int sx=4, sy=4;
int money=0;
map [x][y]='x';
display(map);
cout<<"You have $"<<money<<endl;
while (move!='q')
{
move=getch();
map[x][y]=' ';
//up
if (move=='8')
x--;
//down
if (move=='2')
x++;
//left
if (move=='4')
y--;
//right
if (move=='6')
y++;
//boundaries
if (y<1)
y=1;
if (x<1)
x=1;
if (y>20)
y=20;
if (x>14)
x=14;
//shop
if (map[x][y]=='$')
{
system ("cls");
cout<<"Welcome to the shop. What would you like to buy?"<<endl;
cout<<"- - - - - - - - - - - - - - - - - - - - - - - - "<<endl;
cout<<"Food $10, press 'f' to buy"<<endl;
cout<<"Water $5, press 'w' to buy"<<endl;
cout<<"20 health points $20, press 'h' to buy"<<endl;
char purchase;
purchase=getch();
if (purchase=='f')
{
if (money>9)
{
cout<<"You have purchased food for $10"<<endl;
money=money-10;
cout<<"You now have $"<<money<<". "<<endl;
}
if (money<10)
{
cout<<"You don't have enough money to buy food."<<endl;
cout<<"You still have $"<<money<<". "<<endl;
}
}
if (purchase=='w')
{
if (money>4)
{
cout<<"You have purchased water for $5"<<endl;
money=money-5;
cout<<"You now have $"<<money<<". "<<endl;
}
if (money<5)
{
cout<<"You don't have enough money to buy water."<<endl;
cout<<"You still have $"<<money<<". "<<endl;
}
}
if (purchase=='h')
{
if (money>19)
{
yhealth=yhealth+20;
money=money-20;
cout<<"You have purchased 20 health points. You now have "<<yhealth<<" health points."<<endl;
}
if (money<20)
{
cout<<"You don't have enough money to buy 20 health points."<<endl;
cout<<"You still have "<<money<<" ."<<endl;
}
}
}
if (map[x][y]!='@'&&map[x][y]!='^'&&map[x][y]!='T'&&map[x][y]!='R'&&map[x][y]!='O'&&map[x][y]!='~')
{
map[x][y]='x';
sx=x;
sy=y;
}
else
{
x=sx;
y=sy;
map[x][y]='X';
}
if (money<0)
{
system ("cls");
cout<<"Oh no! You have no money left!"<<endl;
cout<<"Press 'q' to quit..."<<endl;
}
}
}
|