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
|
#include <iostream>
#include <ctime> // for time()
#include <cstdlib> // for rand() and srand()
using namespace std;
void Progress(int anField[][10], int nPlayerX, int nPlayerY, bool &bContinue);
void UserMove(int &nPlayerX, int &nPlayerY);
int main()
{
//Game initialization.
cout << "Move to the treasure (9) to win. Watch out for traps (and hopefully monsters soon) :) " << endl;
int anField[10][10] = {0};
int nPlayerX = 3, nPlayerY = 4;
int nTreasureX = 8, nTreasureY = 9;
anField[nTreasureY][nTreasureX] = 9;
bool bContinue = true;
//Implementation of traps.
const int nAmountTraps = 3;
int anTrapsX[nAmountTraps] = {4, 5, 6};
int anTrapsY[nAmountTraps] = {6, 7, 8};
for(int jjj=0;jjj<nAmountTraps;jjj++)
{
anField[anTrapsY[jjj]][anTrapsX[jjj]] = 1;
}
//Implementation of monsters
const int nAmountMonsters = 3;
int anMonsterX[nAmountMonsters] = {6, 7, 8};
int anMonsterY[nAmountMonsters] = {4, 5, 6};
//Looping an overview and the option for the user to move, until we hit a trap or the treasure
do
{
Progress(anField, nPlayerX, nPlayerY, bContinue);
UserMove(nPlayerX, nPlayerY);
}while(bContinue);
return 0;
}
void Progress(int anField[][10], int nPlayerX, int nPlayerY, bool &bContinue)
{
//Detecting if the player moves to a treasure (win), trap(lose) or free spot (nothing happens, go again)
if(anField[nPlayerY-1][nPlayerX-1] == 1)
{
cout << "Trap! " << endl;
bContinue = false;
exit(0);
}
else if(nPlayerX == 9 && nPlayerY == 10)
{
cout << "Got treasure bitch!" << endl;
bContinue = false;
exit(0);
}
//Keeping track or player position. The cout is to check during development
anField[nPlayerY-1][nPlayerX-1] = 5;
//cout << "Player x,y: " << nPlayerX << ", " << nPlayerY << endl;
//Shows the map
for(int iii = 0; iii<10;iii++)
{
for(int jjj=0;jjj<10;jjj++)
{
cout << anField[iii][jjj] << " ";
}
cout << endl;
}
//Resetting the value of where the player was (else we'd leave a trace of 5's through the map where we were)
anField[nPlayerY-1][nPlayerX-1] = 0;
cout << endl;
}
// Changing users position according to input.
void UserMove(int &nPlayerX, int &nPlayerY)
{
cout << "Pick one: move up (u), down (d), left (l) or right (r). ";
char nUserMove;
TryAgain:
cin >> nUserMove;
switch(nUserMove)
{
case 'u':
nPlayerY -= 1;
break;
case 'd':
nPlayerY += 1;
break;
case 'l':
nPlayerX -= 1;
break;
case 'r':
nPlayerX += 1;
break;
default:
cout << "Only udlr" << endl;
goto TryAgain;
}
}
// Inserting monsters in the game. Function will make one random move (up down left or right) for every monster in the game.
// Al least, it should; haven't been able to test this yet.
int ComputerMove(int &anMonsterX[], int &anMonsterY[], int nAmountMonsters)
{
int nComputerMove;
srand(time(0));
for(int iii = 0; iii<nAmountMonsters; iii++)
{
nComputerMove = rand() % 4 + 1;
switch(nComputerMove)
{
case 1:
anMonsterY[iii] -= 1;
case 2:
anMonsterY[iii] += 1;
case 3:
anMonsterX[iii] -= 1;
case 4:
anMonsterX[iii] += 1;
}
}
// Should return the new postions and stuff like that, but since i can't test it i haven't done that yet. (Maybe different approach is needed?)
return 0;
}
|