ok, so I know there is inheritance and friend that I can use on a class to kind of link two of them together. From what I understand inheritance is an extension of another class. SO if my two classes have nothing to do with each other they shouldn't be derived. As I believe my class fall under.
So I have two classes, Gameboard which creates, displays, and sets the players location on the gameboard.
And then I have the player class, which allows the player to move around the board.
I understand how to display the board and everything and get the player to move in ONE class, but when I try to organize my code and make it proper coding by splitting everything up, I am not sure how I get the Gameboard array to change its value from the player class (the array is what holds the x,y and the current value for that position board[x][y]
Does anyone know how I can make it so that when the player changes their x, y cords that its transferred to the gameboard array, and it changes the value accordingly.
I was thinking something like this
1 2
|
GameBoard::set(tile, tempX, tempY); //changes old x,y to standard gameboard tile
Gameboard::set(player, xCo, yCo); //changes current x,y to player tile.
|
but I just can't get everything linked up properly.
here are the files,
gameboard.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#ifndef GAMEBOARD_H
#define GAMEBOARD_H
class Gameboard
{
private:
static const int ROW = 15;
static const int COL = 25;
static const char tile = 176;
char board[ROW][COL];
int xCo;
int yCo;
public:
Gameboard() : xCo(0),yCo(0){}
Gameboard(int r, int c) : xCo(r), yCo(c){}
void create();
void display();
void set(char player);
void set(char player, int x, int y);
};
#endif // GAMEBOARD_H
|
gameboard.cpp
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
|
#include <iostream>
#include <cstdlib>
#include "gameboard.h"
#include "player.h"
using namespace std;
void Gameboard::create(){
for (int j = 0; j < ROW; j++){
for (int i = 0; i < COL; i++){
board[j][i] = tile;
}
}
}
void Gameboard::display(){
for (int j = 0; j < ROW; j++){
for (int i = 0; i < COL; i++){
cout << board[j][i];
}
cout << endl;
}
}
void Gameboard::set(char player){
this->board[xCo][yCo] = player;
}
void Gameboard::set(char player, int x, int y){
this->board[x][y] = player;
}
|
player.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
private:
static const char player = 233;
int hp;
int attackPower;
int xCo;
int yCo;
public:
Player() : hp(100), attackPower(5), xCo(5), yCo(5) {}
Player(int h, int ap, int x, int y) : hp(h), attackPower(ap), xCo(x), yCo(y) {}
char setPlayer();
void move();
};
#endif // PLAYER_H
|
player.cpp
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
|
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include "gameboard.h"
#include "player.h"
using namespace std;
char Player::setPlayer(){
return player;
}
void Player::move(){
char dir = 'x';
while (dir != '/r'){
cout << "Your location is x:" << xCo << " y:" << yCo << endl;
cout << "User W(up), A(left), S(down), D(right) to move." << endl;
dir = getche();
int tempX = xCo;
int tempY = yCo;
bool error = 0;
switch(dir){
case 'w':
if (xCo >= 15)
xCo = 15;
else
xCo++;
break;
case 's':
if (xCo <= 0)
xCo = 0;
else
xCo--;
break;
case 'a':
if (yCo >= 25)
yCo = 25;
else
yCo++;
break;
case 'd':
if (yCo <= 0)
yCo = 0;
else
yCo--;
break;
}
}
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <iostream>
#include <cstdlib>
#include "gameboard.h"
#include "player.h"
using namespace std;
int main()
{
Gameboard board(5,5);
Player p1;
board.create();
board.display();
board.set(p1.setPlayer());
board.display();
p1.move();
return 0;
}
|
thanks for any help and possible leads in the right direction.
cheers.