![]() |
| | #1 |
| Registered User Join Date: Mar 2007
Posts: 37
| Help with my draughts game 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 |
| Zishaan is offline | |
| | #2 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| I think showing your code (in [code][/code] tags) is the best idea. |
| Daved is offline | |
| | #3 |
| Registered User Join Date: Mar 2007
Posts: 37
| 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
}
}
}
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! |
| Zishaan is offline | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| if this is C++ why are you using pointers and malloc instead of reference and new?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 |
| Registered User Join Date: Dec 2005 Location: Colchester, Essex, United Kingdom.
Posts: 31
| 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. |
| tomcant is offline | |
| | #6 |
| Registered User Join Date: Mar 2007
Posts: 37
| 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! |
| Zishaan is offline | |
| | #7 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| your code is C-style 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
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #8 |
| Registered User Join Date: Mar 2007
Posts: 37
| 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?? |
| Zishaan is offline | |
| | #9 | |
| The superheterodyne. Join Date: Dec 2005 Location: Ireland
Posts: 2,205
| And this is your first program?? new and delete are easy enough to change for your class. Quote:
__________________ I blag! | |
| twomers is offline | |
| | #10 |
| Registered User Join Date: Mar 2007
Posts: 37
| Yeh first progam, never been a fan, but im starting to enjoy it now lol! |
| Zishaan is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Open Source / Semi Open source game idea. Help needed | CaptainPatent | Projects and Job Recruitment | 10 | 05-16-2007 10:44 AM |
| game engine advice? | stien | Game Programming | 0 | 01-23-2007 03:46 PM |
| how do the game engine and the api interact? | Shadow12345 | Game Programming | 7 | 06-05-2002 10:06 PM |
| My Maze Game --- A Few Questions | TechWins | Game Programming | 18 | 04-24-2002 11:00 PM |