Hello everyone, I am working on a Draughts game and have got a Draught class. I will need 12 pieces for each player and wish to declare them quickly within a loop. I just am not sure how.
Is the following code on the right tracks?
1 2 3 4 5
//draw the board
for (int i=0; i<12; i++)
{
Draught d[i](i,0);
}
in which my Draught constructor with two parameters is:
1 2 3 4 5 6 7 8 9 10 11
Draught::Draught(int n, bool c)
{
number = n;
colour = c;
y = (n/7) + (c*5);//works out what row it belongs in and adds 5 rows if white
x = (n%7)*2; //works out column placement
if (y%2 == 0) {x++;} //if on an even line, move it across one square
xTemp = x;
yTemp = y;
active = 1;
}
Which basically takes the number (0-11) and works out the position on the board (not got to testing if this is an accurate position yet, should be though).
Have you any ideas on how to quickly create 12 different draught pieces each with their own name in an array using this constructor?
Thank you for any help you can supply :)
EDIT: I fixed it now by creating a function called construct which replicated what the constructor did and calling that from within the function. Code within main is:
1 2 3 4 5 6 7 8
Draught d[12];
//initialise the board
for (int i=0; i<12; i++)
{
d[i].construct(i,1);
cout<<"Number: "<<d[i].getNumber()<<"\tX: "<<d[i].getX()<<"\tY: "<<d[i].getY()<<endl;
}
and the draught.cpp is:
1 2 3 4 5 6 7 8 9 10 11 12
void Draught::construct(int n, bool c)
{
number = n;
colour = c;
y = floor((n/4)) + (c*5);//works out what row it belongs in and adds 5 rows if white
x = (n%8)*2; //works out column placement
if (x>=8) {x-=8;}
if (y%2 == 0) {x++;} //if on an even line, move it across one square
xTemp = x;
yTemp = y;
active = 1;
}