# Help with my draughts game

• 03-23-2007
Zishaan
Help with my draughts game
Hi guys,

I have been coding a draughts game and have become stuck with a minor issue. As this is the first ever program i have written, i cant really think outside of the box. I was hoping somebody could help me. I am trying to apply forced moves to the draughts, but i cause segmentation errors because as i apply the same rules to each square, the squares on the end of the board have trouble using the same rules. Basicaly, the rules make the program to try and check squares that don't exist, i know this is the problem. BUT i thought a few if statements would sort it out?! it makes sense to me, but maybe i am totaly wrong, and as i am a noob i would appreciate the help.

I will show the code but i wont waste space by pasting it now, if anyone would like to have a look at the code and have a go helping me, please let me know asap, i will paste the code and show what i have done.

Thanks again, Z
• 03-23-2007
Daved
I think showing your code (in [code][/code] tags) is the best idea.
• 03-23-2007
Zishaan
Hey,

Ill show you what i have done:

Code:

``` bool Game::move(Square *dest, Draught *current){         bool fm = false;         bool valid = false;         Square * sq1 = current->getSquare();         if (dest->isOccupied()) return false;         if (cp==1){  //FORCED MOVE CHECK                 for(int i=0; i<player2->getNDraughts(); i++){  // here i get each draught and check if there is forced move                         Draught * d = player2->getDraught(i);                         Square * sqq = current->getSquare();                                                 if(sq1->getX() == 0 && sq1->getX() == 1){          // THIS IS PROBLEM ZONE- i am saying if draught is in position '0' or '1'                                                                                                                 //then dont check certain squares - AVOIDING the segmaentation error - it dont work ??                                                                                                                                                                                                                                                 Square * s = board->getSquare(sqq->getX()+1, sqq->getY() +1); // getting squares for top right from current draught                                 Square * o = board->getSquare(sqq->getX()+2, sqq->getY() +2);                                                                 Square * s1 = board->getSquare(sqq->getX()+1, sqq->getY() -1);        // getting squares for bottom right                                 Square * o1 = board->getSquare(sqq->getX()+2, sqq->getY() -2);                                                                 if ((d->getSquare() == s) && !o->isOccupied()){                                // checking if top right square is occupied                                 fm = true;                                                                                                                                                }                                                                 if ((d->getSquare() == s1) && !o1->isOccupied()){                // checking if bottom right square is occupied                                                                 fm = true;                                 }                                                                                 }                                 else if (sq1->getX() == 6 && sq1->getX() == 7){          //SAME PROBLEM, this time i do for the other end of the ROW '6' and '7'                                                                                                                                         //but for some reason, it works here?? now i am baffed???                                                                         Square * s = board->getSquare(sqq->getX()-1, sqq->getY()+1);  // getting squares for top left focred move                                         Square * o = board->getSquare(sqq->getX()-2, sqq->getY()+2);                                                                                 Square * s1 = board->getSquare(sqq->getX()-1, sqq->getY() -1);        // getting squares for bottom left                                         Square * o1 = board->getSquare(sqq->getX()-2, sqq->getY() -2);                                                                                 if ((d->getSquare() == s) && !o->isOccupied()){                                // checking if top right square is occupied                                         fm = true;                                                                                                                                                        }                                                                         if ((d->getSquare() == s1) && !o1->isOccupied()){                // checking if bottom right square is occupied                                                                         fm = true;                                         }                                                                         }                                                                else {  //THIS IS FOR THE SQUARES NOT ON THE EDGES, AND IT WORKS - squares '2' '3' '4' '5' in the row                                                 Square * s = board->getSquare(sqq->getX()+1, sqq->getY()-1);  // getting squares for top left focred move                         Square * o = board->getSquare(sqq->getX()+2, sqq->getY()-2);                         Square * s1 = board->getSquare(sqq->getX()+1, sqq->getY() +1); // getting squares for top right                         Square * o1 = board->getSquare(sqq->getX()+2, sqq->getY() +2);                         Square * s2 = board->getSquare(sqq->getX()-1, sqq->getY() -1);        // getting squares for bottom left                         Square * o2 = board->getSquare(sqq->getX()-2, sqq->getY() -2);                         Square * s3 = board->getSquare(sqq->getX()-1, sqq->getY() +1);        // getting squares for bottom right                         Square * o3 = board->getSquare(sqq->getX()-2, sqq->getY() +2);                         if ((d->getSquare() == s) && !o->isOccupied()){                                // checking if top left square is occupied                                 fm = true;                                                                                                                                        }                         if ((d->getSquare() == s1) && !o1->isOccupied()){                // checking if top right square is occupied                                                                 fm = true;                         }                                         if (current->isKing()){                                 if ((d->getSquare() == s2) && !o2->isOccupied()){        // checking if bottom left square is occupied                                         fm = true;                                 }                                                 }                                                 if (current->isKing()){                                 if ((d->getSquare() == s3) && !o3->isOccupied()){        // checking if bottom right square is occupied                                         fm = true;                                 }                         }                         }                 }                                 if (fm){ //IF FORCED MOVE IS TRUE, MAKE SURE PLAYER CARRIES IT OUT                                                 if (sq1->getX()!=dest->getX()-2) {}                         else if ((sq1->getY()!=dest->getY()+2) && (sq1->getY()!=dest->getY()-2)) {}                // check if move made by player is valid                                 else if (current->isKing()){                                         if(sq1->getX()!=dest->getX()+2){}                                                                         else                                                                         {valid = true;}                                 }                                else {                                 valid = true;                         }                 }                                                /* END OF FORCED MOVE CHECK FOR P1                   NOW CHECK FOR STANDARD MOVES FOR P1*/                 if (!fm){  //IF FORCED MOVE FALSE THEN CHECK MOVE IS VALID                         if (sq1->getX()!=dest->getX()-1){}                // Checking conditon for P1                                 else if ((sq1->getY()!=dest->getY()+1) && (sq1->getY()!=dest->getY()-1)){}                                         else if (current->isKing()){                                                 if (sq1->getX()!=dest->getX()+1){}                                         else                                                 {valid = true;}        // for the king                                                                }                                         else {                                 valid = true; // standard piece                         }                                 }         }```
If you need more code, like from my other classes let me know, and also im a little confused on how which is x and y on the board, this is how it was created:

Code:

``` Board::Board(int x, int y){         squares = (Square **) malloc(sizeof(Square *) * x);         for(int i=0; i<x; i++)                 squares[i] = (Square *) malloc(sizeof(Square) * y);                for(int i=0; i<x; i++)                 for(int j=0;j<y; j++)                         squares[i][j].setXY(i, j);         sizeX=x; sizeY=y; }```

Thanks!
• 03-24-2007
vart
if this is C++ why are you using pointers and malloc instead of
reference and new?
• 03-24-2007
tomcant
A simple way to retrieve the target square from a given square, is to setup an array for each possible direction of a piece. So, to find the square up-right from the bottom-left square on the board, you can just do `target_squares_up_right[a1]' (similiarly for up_left, bottom_left and bottom_right). So clearly there would be 32 elements in each array and some of the elements will be NULL (to say that a move from a square is not possible, i.e. up-left from the bottom-left square does not exist, so this element will be NULL).

This is not how I would go about the problem though. It is much better to use a single unsigned int to map all the squares on the board, where the state of each square is given by the state of each bit in it's binary representation.
• 03-24-2007
Zishaan
Well, a friend who is on my course has helped me through, whats the difference with your way instead of using pointers?

Im not so good with arrays, but ill have a shot, it makes sense to me, nemore help would be good! Thanks guys!
• 03-24-2007
vart
you don't use any features of C++
for 2D array you can use vactor of vectors

You can avoid using of dynamic memory allocations directly by using suitable stl-types, It can greatly simplify your program
• 03-24-2007
Zishaan
well how long would it take to change my program around now??? I have a month before this is due in, its a 3D draughts game, but i have to implement the mouse handler and the A.I yet. Is it worth changing it around??
• 03-24-2007
twomers
And this is your first program??

new and delete are easy enough to change for your class.

Quote:

int num;
// Get num1
int *pint = new int[num];
I'd say if you understand what you're doing now leave it as it is and when you finish consider changing it around.
• 03-24-2007
Zishaan
Yeh first progam, never been a fan, but im starting to enjoy it now lol!