Hello. I have some problems understanding the code. I'd like to ask for some help. I need someone to break it down for me. Just to explain what's going on and what's happening.
Well this is map.cpp:
I got the other bits but I am struggling with this one and I'd appreciate any help.
#include <iostream>
#include <string>
#include "map.h"
#include <Windows.h>
using namespace std;
Map::Map()
{
map = new string[12];
map[0] = "XXXXXXXXXXXXXXXXXXXXXXXX";
map[1] = "X X XeX";
map[2] = "X X X X";
map[3] = "X X X";
map[4] = "X XX@XXXXXXXXXXXXXXXXX";
map[5] = "X X X";
map[6] = "X X k X";
map[7] = "X XXXXX X X";
map[8] = "X X k X X X";
map[9] = "X X X X";
map[10] = "X> X X @ X";
map[11] = "XXXXXXXXXXXXXXXXXXXXXXXX";
}
void Map::draw()
{
for(int i = 0; i < 12; i++)
{
cout << map[i] << endl;
}
cout << "Keys: " << keys << endl;
}
void Map::pos(int& ppX, int& ppY)
{
pX = ppX;
pY = ppY;
}
char Map::getTile(int abc, int xyz)
{
char tile = map[abc][xyz];
return tile;
}
void Map::movePlayer(int leftright, int updown)
{
switch(updown)
{
case -1:
if(getTile((pX - 1), pY) != 'X' & getTile((pX - 1), pY) != 'k' & getTile((pX - 1), pY) != '@' & getTile((pX - 1), pY) != ' ')
{
system("cls");
cout << "YOU WIN" << endl;
system("pause");
exit (1);
}
if(getTile((pX - 1), pY) != 'X' & getTile((pX - 1), pY) != 'k' & getTile((pX - 1), pY) != '@')
{
setTile(pX, pY, ' ');
setTile((pX - 1), pY, '^');
pX--;
}
else
{
if(getTile((pX - 1), pY) != 'X' & getTile((pX - 1), pY) != 'k' & getTile((pX - 1), pY) != ' ')
{
if(keys > 0)
{
keys--;
setTile(pX, pY, ' ');
setTile((pX - 1), pY, '^');
pX--;
}
}
setTile(pX, pY, '^');
}
break;
case 0:
break;
case 1:
if(getTile((pX + 1), pY) != 'X' & getTile((pX + 1), pY) != 'k' & getTile((pX + 1), pY) != '@')
{
setTile(pX, pY, ' ');
setTile((pX + 1), pY, 'V');
pX++;
}
else
{
if(getTile((pX + 1), pY) != 'X' & getTile((pX + 1), pY) != 'k' & getTile((pX + 1), pY) != ' ')
{
if(keys > 0)
{
keys--;
setTile(pX, pY, ' ');
setTile((pX + 1), pY, 'V');
pX++;
}
}
setTile(pX, pY, 'V');
}
break;
}
switch(leftright)
{
case -1:
if(getTile(pX, (pY - 1)) != 'X' & getTile(pX, (pY - 1)) != 'k' & getTile(pX, (pY - 1)) != '@')
{
setTile(pX, pY, ' ');
setTile(pX, (pY - 1), '<');
pY--;
}
else
{
if(getTile(pX, (pY - 1)) != 'X' & getTile(pX, (pY - 1)) != 'k' & getTile(pX, (pY - 1)) != ' ')
{
if(keys > 0)
{
keys--;
setTile(pX, pY, ' ');
setTile(pX, (pY - 1), '<');
pY--;
}
}
setTile(pX, pY, '<');
}
break;
case 0:
break;
case 1:
if(getTile(pX, (pY + 1)) != 'X' & getTile(pX, (pY + 1)) != 'k' & getTile(pX, (pY + 1)) != '@')
{
setTile(pX, pY, ' ');
setTile(pX, (pY + 1), '>');
pY++;
}
else
{
if(getTile(pX, (pY + 1)) != 'X' & getTile(pX, (pY + 1)) != 'k' & getTile(pX, (pY + 1)) != ' ')
{
if(keys > 0)
{
keys--;
setTile(pX, pY, ' ');
setTile(pX, (pY + 1), '>');
pY++;
}
}
setTile(pX, pY, '>');
}
break;
}
}
void Map::setTile(int x, int y, char z)
{
map[x][y] = z;
}
void Map::fire()
{
char direction = getTile(pX, pY);
switch(direction)
{
case '^':
fX = pX;
fY = pY;
while(getTile((fX - 1), fY) != 'X' & getTile((fX - 1), fY) != '@')
{
if(getTile((fX - 1), fY) != ' ' & getTile((fX - 1), fY) != 'X' & getTile((fX - 1), fY) != '@')
{
keys++;
}
setTile((fX - 1), fY, '*');
fX--;
setTile((fX+1), fY, ' ');
setTile(pX, pY, '^');
system("cls");
draw();
}
setTile(fX, fY, ' ');
setTile(pX, pY, '^');
break;
case 'V':
fX = pX;
fY = pY;
while(getTile((fX + 1), fY) != 'X' & getTile((fX + 1), fY) != '@')
{
if(getTile((fX + 1), fY) != ' ' & getTile((fX + 1), fY) != 'X' & getTile((fX + 1), fY) != '@')
{
keys++;
}
setTile((fX + 1), fY, '*');
fX++;
setTile((fX -1), fY, ' ');
setTile(pX, pY, 'V');
system("cls");
draw();
}
setTile(fX, fY, ' ');
setTile(pX, pY, 'V');
break;
case '<':
fX = pX;
fY = pY;
while(getTile(fX, (fY - 1)) != 'X' & getTile(fX, (fY - 1)) != '@')
{
if(getTile(fX, (fY - 1)) != ' ' & getTile(fX, (fY - 1)) != 'X' & getTile(fX, (fY - 1)) != '@')
{
keys++;
}
setTile(fX, (fY - 1), '*');
fY--;
setTile(fX, (fY + 1), ' ');
setTile(pX, pY, '<');
system("cls");
draw();
}
setTile(fX, fY, ' ');
setTile(pX, pY, '<');
break;
case '>':
fX = pX;
fY = pY;
while(getTile(fX, (fY + 1)) != 'X' & getTile(fX, (fY + 1)) != '@')
{
if(getTile(fX, (fY + 1)) != ' ' & getTile(fX, (fY + 1)) != 'X' & getTile(fX, (fY + 1)) != '@')
{
keys++;
}
setTile(fX, (fY + 1), '*');
fY++;
setTile(fX, (fY - 1), ' ');
setTile(pX, pY, '>');
system("cls");
draw();
}
setTile(fX, fY, ' ');
setTile(pX, pY, '>');
break;
}
}
void Map::setkeys(int keynum)
{
keys = keynum;
}