i am at uni an i have a project in which i have to create a backgammon game. i have created the board the dice and the moves, now i need to do the legality of the moves i was wondering if anyone had an idea of how i would go about this?

Thanks

p.s. i have included the code i have already done as an attachment

Attachments
#include "Header1.h"
#include <stdio.h>
#include <stdlib.h>


int main()
{

	Board::Boardstate();
	Board::GenerateMoves();

}

void Board::Boardstate()
{
	int Board[28];
	int dice[2];

	Board[6] = 5;
	Board[8] = 3;
	Board[13] = 5;
	Board[24] = 2;

	Board[1] = -2;
	Board[12] = -5;
	Board[17] = -3;
	Board[19] = -5;

}

void Board::GenerateMoves(int *dice, int *point, int player)
{

	int checker1;
	int checker2;
	int dice1 = dice[0];
	int dice2 = dice[1];
	int destination1;
	int destination2;
	bool checkeronbar = false;
	int NumberofCheckers;
	int i;
	int a;
	int f;



	if (player ==1)
		{

			for(i=24; i>0; i--)	
				if(point[i] > 0)
					{

						checker1 = i;	//find 1st checker that can be moved

						if(checkeronbar = true)				//for checking if a checker is on the bar
							{

							// checker on the bar has to be moved first so there is no method

							}
						else
							{

							destination1 = checker1 - dice1;
							int nextdestination = destination1;			//to be able to move the same checker twice in one move
							destination2 = nextdestination - dice2;

							}

					}

			for(a = checker1; a>0; a--)	
				if(point[a] > 1)			//checks to see if there is more than 1 checker on the point
					{

						if(a == i)			//checking if this is at the posiiton of checker1
							{

								NumberofCheckers = point[a]-1;		//remove the checker from the count of checkers at that point

							}

						else
							{

								NumberofCheckers = point[a];

							}

						for (f = 0; f<NumberofCheckers; f++)
							{

								checker2 = a;
								if(checkeronbar == true)		//for checking if a checker is on the bar
									{ 
										
										checker1 = 26;
										destination1 = 25 - dice1;
									}
								else
									{

										destination1 = checker1 - dice1;

									}

								destination2 = checker2 - dice2;


								if(checkeronbar == true)		//for checking if a checker is on the bar
									{

										checker1 = 26;
										destination1 = 25 - dice2;
									}
								else 
									{ 
										
										destination1 = checker1 - dice2;
								
									}
								destination2 = checker2 - dice1;

							
							}

					}

				else if(point[a] == 1)
					{
						if((a!=i) || (checkeronbar == true))
						{

							checker2 = a;
							if (checkeronbar == true)
								{

								checker1 = 26; 
								destination1 = 25 - dice1;

								}
							else
								{

								destination1 = checker1 - dice1;

								}

							destination2 = checker2 - dice2;

							if (checkeronbar == true)
								{

								checker1 = 26; 
								destination1 = 25-dice2;

								}
							else 
								{

								destination1 = checker1 - dice2;

								}
							
							destination2 = checker2 - dice1;


						}

					}
		}

	else if (player ==2)
			{

			for(i=24; i>0; i--)	
				if(point[i] > 0)
					{

						checker1 = i;	//find 1st checker that can be moved

						if(checkeronbar = true)				//for checking if a checker is on the bar
							{

							// checker on the bar has to be moved first so there is no method

							}
						else
							{

							destination1 = checker1 + dice1;
							int nextdestination = destination1;			//to be able to move the same checker twice in one move
							destination2 = nextdestination - dice2;

							}

					}

			for(a=checker1; a>0; a--)	
				if(point[a] > 1)			//checks to see if there is more than 1 checker on the point
					{

						if(a == i)			//checking if this is at the posiiton of checker1
							{

								NumberofCheckers = point[a]+1;		//remove the checker from the count of checkers at that point

							}

						else
							{

								NumberofCheckers = point[a];

							}

						for (f = 0; f<NumberofCheckers; f++)
							{

								checker2 = a;
								if(checkeronbar == true)		//for checking if a checker is on the bar
									{ 
										
										checker1 = 26;
										destination1 = 25 + dice1;
									}
								else
									{

										destination1 = checker1 + dice1;

									}

								destination2 = checker2 + dice2;


								if(checkeronbar == true)		//for checking if a checker is on the bar
									{

										checker1 = 26;
										destination1 = 25 + dice2;
									}
								else 
									{ 
										
										destination1 = checker1 + dice2;
								
									}
								destination2 = checker2 + dice1;

							
							}

					}

				else if(point[a] == 1)
					{
						if((a!=i) || (checkeronbar == true))
						{

							checker2 = a;
							if (checkeronbar == true) 
								{

								checker1 = 26; 
								destination1 = 25 + dice1;

								}
							else
								{

								destination1 = checker1 + dice1;

								}

							destination2 = checker2 + dice2;

							if (checkeronbar == true)
								{

								checker1 = 26; 
								destination1 = 25 + dice2;

								}
							else 
								{

								destination1 = checker1 + dice2;

								}
							
							destination2 = checker2 + dice1;


						}

					}
		}



}

Code is not supposed to look like that. A single function should not span page after page. You've managed to produce structured spaghetti code, with meatballs. (I don't even know what that means.) :)

Divide it up into more pieces. More objects, or at least more functions. If something seems repetitive, if it's copied over and over with little change, break it out as a function and parameterize the changing stuff.

Checking the legality of moves and applying them to the board should be fairly simple once the other stuff is cleaned up.

>A single function should not span page after page.
I don't subscribe to the theory of no more than a page for each function. That's too impractical for some functions, where complexity and difficulty in refactoring (either through logical flow or performance considerations) justify a longer function.

>You've managed to produce structured spaghetti code, with meatballs.
HA! If you think that's spaghetti code, you're a lucky person indeed. I think you're overreacting.

However, I do agree that the code reads like there was no design intent at all. It looks like the OP hacked a solution out rather than thinking about it and organizing his thoughts first.

Good points, Narue, as usual. It's just all that clearly repetitive code. It isn't really spaghetti code (that would presumably require goto's), but it somehow feels like it. As you say, no design.

how to check legality.

you have your 1D array representing the board, with array indices that represent the sequential position. 1-24 for each point, plus 0 and 25 for the bar would be how i structure it.

each color moves a certain direction, so the rolls of the dice will determine what the allowable moves are.

say white starts at point 1 and moves towards point 24. say youve got your intial pieces at setup, on points 1, 12, 17, and 19. you roll a 5 and a 6 on the dice.

white indicates that he wants to move pieces from point 1. therefore "Possible" moves could be from that point, to point 1+5 = 6, to point 1+6=7, and to point 1+11=12.

check the "possible" moves to see if other pieces are there: point #6 has more than one of the opponent's pieces, therefore not valid. point #7 is empty, therefore it is valid. point #12 has their own pieces on it, and could be valid, but there are 5 pieces there so its full and is in fact not a valid move.

to consider moves from the "bar" for white, i would consider that location 0, for black i would say location 25.

you can have several possible move results for any piece.

1- allowable, no attack. just increases the number of pieces at that point.

2 - allowable, attack. one opponent piece is alone, and will be knocked off to it's bar location.

3 - not allowable, opponent owns point. 2 or more opponent pieces block that point.

3 - not allowable, side full. 5 of your pieces are already there.

4 - "bearing off". piece is moved off the board. this is only allowed when all of the pieces are in the "home board" (the last 6 points)

each roll must be moved by a piece, if possible. two peices can move one die each, or one piece can combine both dice. Teh player can only pass if no possible move is available.

doubles require four (4) moves of each value. by one or more pieces for the total combined value.

This article has been dead for over six months. Start a new discussion instead.