Help with my draughts game

This is a discussion on Help with my draughts game within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I have been coding a draughts game and have become stuck with a minor issue. As this is ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    37

    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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I think showing your code (in [code][/code] tags) is the best idea.

  3. #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
    			}
    		
    		}
    	}
    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!

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if this is C++ why are you using pointers and malloc instead of
    reference and new?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #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.

  6. #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!

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #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??

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    And this is your first program??

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

    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.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    37
    Yeh first progam, never been a fan, but im starting to enjoy it now lol!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 11:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21