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
|
// my first game.
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int i,j, gi, gj, ei, ej;
srand (time(NULL));
char ar[10][10];
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
ar[i][j] ='.';
//put 5 (at most) random trap
ar[rand()%8+1][rand()%8+1] = 'T';
ar[rand()%8+1][rand()%8+1] = 'T';
ar[rand()%8+1][rand()%8+1] = 'T';
ar[rand()%8+1][rand()%8+1] = 'T';
ar[rand()%8+1][rand()%8+1] = 'T';
//enemy
ei = rand()%8+1; ej = rand()%8+1;
ar[ei][ej] = 'E';
ar[0][0] ='G'; ar[9][9] = 'X';
gi = 0; gj =0;
char u ='u', d='d', l='l', r='r', key;
//cout << "set you keys for movement: up, down, left, right, separated by space.\n";
//cin >> u >> d >> l >> r;
//cin.ignore();
cout << "mission: go to destination X\n"
<< "you are G, enemy is E, you will die if go to trap\n"
<< "enemy can swap trap!\n"
<< "lets start. u=up d=down l=left r=rigth\n\n";
for(int i=0; i<10; i++) ///diagram
{
for(int j=0; j<10; j++)
{cout << ar[i][j]; }
cout << endl;
} cout << "\n\n";
do
{
cout << "go.";
cin>>key; cin.ignore();
if (key==u) { if(ar[gi-1][gj] == 'T') { cout <<"entered trap\n"; break; } gi--; swap ( ar[gi][gj], ar[gi+1][gj]); }
else if (key==d) { if(ar[gi+1][gj] == 'T') { cout <<"entered trap\n"; break; } gi++; swap ( ar[gi][gj], ar[gi-1][gj]); }
else if (key==l) { if(ar[gi][gj-1] == 'T') { cout <<"entered trap\n"; break; } gj--; swap ( ar[gi][gj], ar[gi][gj+1]); }
else if (key==r) { if(ar[gi][gj+1] == 'T') { cout <<"entered trap\n"; break; } gj++; swap ( ar[gi][gj], ar[gi][gj-1]); }
tryagain:
int erand = rand()%4;
if (erand == 0) { if(ar[ei-1][ej] == 'G') { cout <<"caugth by enemy\n"; break; } ei--; swap ( ar[ei][ej], ar[ei+1][ej]); }
else if (erand == 1) { if(ar[ei+1][ej] == 'G') { cout <<"caugth by enemy\n"; break; } ei++; swap ( ar[ei][ej], ar[ei-1][ej]); }
else if (erand == 2) { if(ar[ei][ej-1] == 'G') { cout <<"caugth by enemy\n"; break; } ej--; swap ( ar[ei][ej], ar[ei][ej+1]); }
else if (erand == 3) { if(ar[ei][ej+1] == 'G') { cout <<"caugth by enemy\n"; break; } ej++; swap ( ar[ei][ej], ar[ei][ej-1]); }
if ( ei<0 || ej<0 || ei>9 || ej>9) { goto tryagain; }
if (gi==9 && gj==9) { cout << "congratulation! you won!\n"; break; }
if ( gi<0 || gj<0 || gi>9 || gj>9) { cout << "went out of field!\n"; break; }
for(int i=0; i<10; i++) ///diagram
{
for(int j=0; j<10; j++)
{cout << ar[i][j]; }
cout << endl;
} cout << "\n\n";
} while (1);
cout <<"Game Over\n";
return 0;
}
|