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

this is C++ code.

you'll have more luck getting help in the C++ forum

Moved to the C++ forum.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.