my array resets to initial value
My c++ should be proficiency should be visible through my problem :)
I'm trying to write small app in order to generate some hexagon tiles.
I've created a class board which I use for my array, I use this form of drawing on the screen since I'm busy learning c++ .
I have a method in my class that set coords and draw a hexagon by means of some arithmatic setting values in the array.
Once this is done I redraw the array.
The problem that I have is after every setting of the coords it doesn't remember the previous my previous hexagon. I have had this problem before when I wrote a small tic-tac-toe game.
I know how to fix it (not sure if it's the right way). I set the coords of the instance of the class "myboard".
I thought the proper way to get around this is by passing the instance of the class to the classes method. The drawing works but it doesn't remember the previous hexagon.
I'm sure this solution is a much bigger concept which I need to grasp if I want to succeed at c++.
This is why I asked in the forum in order to be able to understand it.
Thanks
Jannie
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
class board
{
public:
board();
~board();
string arboard[50][50];
void refreshboard();
void addtile(board myboard,int x,int y);
};
//constructor init board with x
board::board(){
for(int i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
arboard[i][j] = "X";
cout<< arboard[i][j] ;
}
cout << endl;
}
};
board::~board(){};
void board::refreshboard()
{
//this function will draw the board with tiles one it.
for(int i=0;i<20;i++)
{
for (int j=0;j<20;j++)
{
cout<< arboard[i][j];
}
cout << endl;
}
};
void board::addtile(board myboard,int x,int y)
{
// this version draw the tile but it only ever draw one on the board . I thought by passing the myboard instance to it it will have the same effect as the one in main but it doesn't I believe I am doing something wrong here but not sure what.
/* arboard[y][x] = " ";
arboard[y][x+1] = "/";
arboard[y][x+2] = " ";
arboard[y][x+3] = "\\";
arboard[y][x+4] = " ";
//Draw middle
arboard[y+1][x] = "|";
arboard[y+1][x+1] = " ";
arboard[y+1][x+2] = 1;
arboard[y+1][x+3] = " ";
arboard[y+1][x+4] = "|";
//draw bottom
arboard[y+2][x] = " ";
arboard[y+2][x+1] = "\\";
arboard[y+2][x+2] = " ";
arboard[y+2][x+3] = "/";
arboard[y+2][x+4] = " ";*/
};
void SetTile(board myboard,int x,int y)
{
//function to set the tile to interface with the class method
myboard.addtile(myboard,x,y);
myboard.refreshboard();
}
int main()
{
int x = 0;
int y=0;
int i = 0;
board myboard;
while (i<5)
{
cout << "Insert X and Y \n";
cin >> x ;
cin >> y;
//this works if I set it here.
myboard.arboard[y][x] = " ";
myboard.arboard[y][x+1] = "/";
myboard.arboard[y][x+2] = " ";
myboard.arboard[y][x+3] = "\\";
myboard.arboard[y][x+4] = " ";
//Draw middle
myboard.arboard[y+1][x] = "|";
myboard.arboard[y+1][x+1] = " ";
myboard.arboard[y+1][x+2] = 1;
myboard.arboard[y+1][x+3] = " ";
myboard.arboard[y+1][x+4] = "|";
//draw bottom
myboard.arboard[y+2][x] = " ";
myboard.arboard[y+2][x+1] = "\\";
myboard.arboard[y+2][x+2] = " ";
myboard.arboard[y+2][x+3] = "/";
myboard.arboard[y+2][x+4] = " ";
SetTile(myboard,x,y);
}
return 0;
}