Construktor problem?? C++
Hejsan, har ett problem och jag tror att det är ett Constructor problem... är fortfarande nybörjare.
Här har ni koden som jag inte får fungera.. Hade uppskattat lite hjälp
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
enum Color // 1byte/8 bits of info, 128 = bgIntensity, 64 = bgRed, 32 = bgGreen, 16 = bgBlue, 8 = frIntensity, 4 = frRed, 2 = frGreen, 1 = frBlue, 0 = none
{
NONE = 0,
BLUE = 1,
GREEN = 2,
TEAL = 3,
RED = 4,
lightTEAL = 11,
lightBLUE = 9,
lightGREEN = 10,
lightRED = 12
};
void MoveCursor(int y, int x)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //Points to what console window that is currently beeing used
COORD coord; //??
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord); //??
}
void SetColor(Color forecolor, Color backcolor) //pass color enums
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, (WORD)forecolor + ((WORD)backcolor << 4)); //Shift the 4 backcolor bits to left
}
class Map //group togeather relevant variables/functions, private by default
{
public:
Map(){}
Map(int width, int height) //Construct
{
mapWidth = width;
mapHeight = height;
map = new char*[height];
int heightCreated = 0;
int widthCreated = 0;
char toWrite;
while (heightCreated < height)
{
map[heightCreated] = new char[width];
widthCreated = 0;
while (widthCreated < width)
{
if (heightCreated == 0 || heightCreated == mapHeight - 1)
{
toWrite = 'X';
}
else if (widthCreated == 0 || widthCreated == mapWidth - 1)
{
toWrite = 'X';
}
else
{
toWrite = ' ';
}
map[heightCreated][widthCreated] = toWrite;
widthCreated = widthCreated + 1;
}
heightCreated = heightCreated + 1;
}
}
private:
int mapWidth;
int mapHeight;
char **map;
public:
void Draw(int yPos, int xPos, char object)
{
MoveCursor(0, 0);
SetColor(Color::NONE, Color::TEAL); //Use :: for clarification. Called Scope when using ::
int mapHeigthPrinted = 0;
int mapWidthPrinted = 0;
while (mapHeigthPrinted < mapHeight)
{
mapWidthPrinted = 0;
while (mapWidthPrinted < mapWidth)
{
cout << map[mapHeigthPrinted][mapWidthPrinted];
mapWidthPrinted = mapWidthPrinted + 1;
}
cout << endl;
mapHeigthPrinted++;
}
SetColor(Color::lightGREEN, Color::NONE); //Use :: for clarification. Called Scope when using ::
cout << "Coordinates " << endl << "X: " << xPos << " " << endl << "Y: " << yPos << " " << endl;
cout << "Object: " << object << endl;
}
void DrawObject(int y, int x, char object)
{
map[y][x] = object;
}
char GetObjectAt(int y, int x)
{
if (y >= 0 && y < mapHeight && x >= 0 && x < mapWidth)
{
return map[y][x]; //Computer reads Y-axis first, X-Axis after
}
return 'X';
}
};
class PlayerChar //Private from start, struct is used to clump variables togeather without worrying about changes
{
private:
public:
char symbol = 1;
int POSx = 1;
int POSy = 1;
void Draw()
{
SetColor(Color::lightTEAL, Color::BLUE); //Use :: for clarification. Called Scope when using ::
MoveCursor(POSy, POSx);
cout << symbol;
}
void TryMoveTo(int y, int x, char ObjectAt)
{
switch (ObjectAt)
{
case 'X':
break;
case ' ':
POSy = y;
POSx = x;
break;
case 'T':
POSy = 10;
POSx = 10;
break;
case 'O':
POSy = 5;
POSx = 5;
break;
default:
break;
}
}
};
void ShowObject()
{
Map Draw;
Draw.DrawObject(8, 8, 'Z');
}
int main()
{
#pragma region Size_of_Map
int width = 20;
int height = 20;
//menu();
/*
cout << "How big map do you want? (full screen is Width: 79, Height 21)" << endl
<< "Width (Max 79 or it will default to 10): ";
cin >> width;
cout << "Height (Max 21 or it will default to 10): ";
cin >> height;
if (width > 79 || width < 0)
{
width = 79;
}
else if (height > 21 || height < 0)
{
height = 21;
}
else
{
cout << "Critical error. Shutting down";
}
system("CLS");
*/
#pragma endregion Size_of_Map
Map world(width, height);
PlayerChar hero;
char ObjectAt = ' ';
ShowObject();
world.Draw(hero.POSy, hero.POSx, ObjectAt);
hero.Draw();
int toPosX = 0;
int toPosY = 0;
bool wantstomove = false;
bool play = true;
while (play)
{
char movement = _getch();
wantstomove = false;
switch (movement)
{
case 'a': //left
{
toPosX = hero.POSx - 1;
toPosY = hero.POSy;
wantstomove = true;
break;
}
case 'w': //up
{
toPosY = hero.POSy - 1;
toPosX = hero.POSx;
wantstomove = true;
break;
}
case 'd': //right
{
toPosX = hero.POSx + 1;
toPosY = hero.POSy;
wantstomove = true;
break;
}
case 's': //down
{
toPosY = hero.POSy + 1;
toPosX = hero.POSx;
wantstomove = true;
break;
}
case 'q': //quit
{
play = false;
break;
}
case 'r': //reset
{
hero.POSx = 1;
hero.POSy = 1;
break;
}
default: //Do nothing if anything not binded is not binded.
{
break;
}
}
if (wantstomove == true)
{
ObjectAt = world.GetObjectAt(toPosY, toPosX); //Call Map class with world., use function GetObjectAt(Y-coord, X-Coord)
hero.TryMoveTo(toPosY, toPosX, ObjectAt); //Call PlayerChar class with hero., use function TryMoveTo(Y-coord, X-Coord, Send what object is where we are going to move
}
ShowObject();
world.Draw(hero.POSy, hero.POSx, ObjectAt); //don't pass any arguments in normal use. pass hero.POSx, hero.POSy for coords.
hero.Draw();
}
return 0;
}
Problemet kommer när jag försöker lägga till objekt på kartan via en utomstående funktion.