Since I'm new to coding, can you give me some advise on game class interactions? The game does work, but my code looks ugly and I think it can be improved a lot.
What I'm mostly interested in:
class1.setvalue(class2.returnvalue())
is this how it is supposed to be done?- Should I derive my objects from some virtual base?
- If I would like to add another enemy (or food) class object at some point of the game, how should it be done?
- The use of
int k
to make enemy move in only half of cycles.
Don't go rough on console things. No one makes console games anyway.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
#include <windows.h>
using namespace std; //know its a bad practise
struct coord
{
int x;
int y;
char mark;
};
class karta //map
{
public:
karta(int a, int b )
{
mx = a; my = b;
for (i = 0; i < mx; i++)
{
vector<char> clm;
for (j = 0; j < my; j++)
{
clm.push_back(' ');
}
mapa.push_back(clm);
}
}
void generatemap()
{
for (i = 1; i < mx; i++)
for (j = 1; j < my; j++)
mapa[i][j] = ' ';
for (i = 1; i <= my; i++)
{
mapa[0][i-1] = 177;
mapa[mx-1][i-1] = 177;
}
for (i = 1; i <= mx; i++)
{
mapa[i-1][0] = 177;
mapa[i-1][my-1] = 177;
}
}
void generateobjects()
{
mapa[food.x][food.y] = food.mark;
mapa[hero.x][hero.y] = hero.mark;
mapa[enemyc.x][enemyc.y] = enemyc.mark;
}
void printemap()
{
for (j = 0; j < my; j++)
{
for (i = 0; i < mx; i++)
cout << mapa[i][j];
cout << endl;
}
}
void updateobjectdata(coord k)
{
hero = k;
}
void setfoodc(coord k)
{
food = k;
}
void updatenemy(coord k)
{
enemyc = k;
}
int pass_x()
{
return mx;
}
int pass_y()
{
return my;
}
private:
int i,j;
int mx;
int my;
vector< vector<char> > mapa;
coord hero,food,enemyc;
};
class game
{
public:
game()
{
running = true;
score = 0;
flag = true;
}
void setwalls(int x, int y)
{
wallx = x-1; wally = y-1;
}
void sethero(coord k)
{
heroc = k;
}
void setfood(coord k)
{
foodc = k;
flag = false;
}
void setenemy(coord k)
{
enemyc = k;
}
bool ifrunning()
{
if (running)
return true;
}
void checkwall()
{
if (wallx == heroc.x || wally == heroc.y || heroc.x == 0 || heroc.y == 0)
{
running = false;
}
}
void checkenemy()
{
if (heroc.x == enemyc.x && heroc.y == enemyc.y)
{
running = false;
}
}
int enemymovement()
{
if (heroc.x > enemyc.x)
if (heroc.y > enemyc.y)
if (rand() % 2 == 1)
return 1;
else
return 3;
else
if (heroc.y == enemyc.y)
return 1;
else
if (rand() % 2 == 1)
return 1;
else
return 4;
else
if (heroc.y > enemyc.y)
if (heroc.x == enemyc.x)
return 3;
else
if (rand() % 2 == 1)
return 2;
else
return 3;
else
if (heroc.x == enemyc.x)
return 4;
else
if (heroc.y == enemyc.y)
return 2;
else
if (rand() % 2 == 1)
return 2;
else
return 4;
}
void checkfood()
{
if (heroc.x == foodc.x && heroc.y == foodc.y)
{
score += 5;
flag = true;
}
}
bool flagstatus()
{
return flag;
}
void gameover()
{
cout << "Game over! Your score is :" << score;
}
private:
bool running,flag;
int wallx, wally, score;
coord heroc,foodc,enemyc;
};
class enemy
{
public:
enemy(int a, int b)
{
limx = a - 2; limy = b - 2;
obj.mark = 'G';
}
void gencoord()
{
obj.x = rand() % limx + 1;
obj.y = rand() % limy + 1;
}
void move(int a)
{
switch (a)
{
case 1:
obj.x++;
break;
case 2:
obj.x--;
break;
case 3:
obj.y++;
break;
case 4:
obj.y--;
break;
}
}
coord givecord()
{
return obj;
}
private:
coord obj;
int limx, limy;
};
class object //hero
{
public:
object()
{
obj.x = 6;
obj.y = 6;
obj.mark = '$';
}
void move(char key)
{
switch (key)
{
case 'w':
obj.y--; // inversed!
break;
case 'a':
obj.x--;
break;
case 'd':
obj.x++;
break;
case 's':
obj.y++; // inversed!
break;
}
}
coord givecord()
{
return obj;
}
private:
coord obj;
};
class food
{
public:
food(int a, int b)
{
limx = a-2; limy = b-2;
obj.mark = '5';
}
void gencoord()
{
obj.x = rand() % limx +1;
obj.y = rand() % limy +1;
}
coord givecord()
{
return obj;
}
private:
coord obj;
int limx, limy;
};
int main()
{
karta mapka(30,15); //size depends on your console
object hero;
game gamz;
food eda(mapka.pass_x(), mapka.pass_y());
enemy vrag(mapka.pass_x(), mapka.pass_y());
vrag.gencoord();
int k = 0;
gamz.setwalls(mapka.pass_x(), mapka.pass_y());
gamz.sethero(hero.givecord());
gamz.setenemy(vrag.givecord());
mapka.generatemap();
mapka.updateobjectdata(hero.givecord());
mapka.updatenemy(vrag.givecord());
mapka.generateobjects();
mapka.printemap();
Sleep(2000);
_getch();
while (gamz.ifrunning() == true)
{
Sleep(110);
if (_kbhit())
{
hero.move(_getch());
}
gamz.sethero(hero.givecord());
gamz.checkenemy();
gamz.checkwall();
gamz.checkfood();
if (gamz.flagstatus() == true)
{
eda.gencoord();
gamz.setfood(eda.givecord());
mapka.setfoodc(eda.givecord());
}
if (k%2 == 1)
vrag.move(gamz.enemymovement());
k++;
gamz.setenemy(vrag.givecord());
gamz.checkenemy();
system("cls");
mapka.updateobjectdata(hero.givecord());
mapka.updatenemy(vrag.givecord());
mapka.generatemap();
mapka.generateobjects();
mapka.printemap();
}
gamz.gameover();
Sleep(5000);
_getch();
return 0;
}
$ - you; 5 - food; G - enemy; WASD - move. Game language must be English