Backgammon Board Game

Hi everyone,

I've just signed up so new here :-)

I'm currently a first year engineering student in New Zealand.
Part of the course entails c++ programming using Visual Studio 2010.

As part of an assignment, I've been asked to produce a "simplified" version of the board game "Backgammon" (only basic manoeuvres such as blocking / hitting etc...).

I've produced the graphics for the board and have basic functions working for mouse clicks for new game, roll dice, and exit programme, which all work. The graphic's has been produced by an add-on package provided by my lecturer (header and source files), and not using the standard c++.NET system.

However, I'm a little stumped as how to go about moving the pieces on the board, and would like some assistance or guidance from someone whom has developed such software before. Or, could you please provide some existing code of backgammon for me to use as a reference to my assignment. I can provide a copy of my code thus far if so requested.

All help will be greatly appreciated.

Thanks,
Bevan
Well you could go really simple like this (pseudo-code):
1
2
3
4
5
6
7
if (piece not there)
    switch (dirrection)
        case left:
            --x;
        case up:
            --y;
        etc...

That would make it so every time you program loops the piece's location would move in the direction it needs to go
Hi Angeljruiz,

Thanks for your prompt response.

I follow what you have listed above, and so has sparked a few ideas which I'll work on later tonight.

Do you have any advice on storing the number of pieces on each triangle as they move around the board? I can use an array, no problem, but it's a matter of calling and executing the various function so as to move a single piece according to dice values (2 dice used), redraw the moved piece on the board (erasing it's previous location), and updating it's location within the array (not using animations - every time a piece is moved it is redrawn in it's new location - can be considered "static").

But thanks once again - appreciated.

Bevan
Umm I'm not sure. Honestly I've never played backgammon or know how it works so I don't know what you need. Maybe you could make a triangle class with a vector of pieces and a variable that keeps track of how many pieces are on that triangle.
You don't need to keep track of every piece. You only need to keep track of how many pieces there are on each triangle and what colour they are (All pieces on the same triangle will always have the same colour).

There are 24 triangles on the board so you could have an array of 24 ints to store how many pieces there are on each triangle, and another array of the same size to store the colour.

Another possibility is to have one array for each player, containing the number of pieces of the player's colour. In this case you don't need an extra array to store the colours. I will use this representation in the examples below.

To simplify the implementation you can use 0 and 1 to represent which player it is. You can then use that number as an index in arrays removing the need to duplicate much of the code for both players. The players will move in different directions so you can define the move direction to be 1 for player 0 and -1 for player 1.
1
2
int moveDirection[2] = {1, -1};
int pieces[2][24] = {...


Assume the sum of the two dices is N. To check if the player can move a piece from position x you first need to make sure that the player has pieces on position x pieces[player][x] > 0. After that the new position is calculated y = x + N * moveDirection[player];. If the new position is outside the board y < 0 || y >= 24 the move is invalid, otherwise the move is valid if the opponent has no more than 1 piece at that position pieces[opponent][y] <= 1.

To make the move from x to y the player's piece count is decreased by one on x --pieces[player][x];, and increased by one on y ++pieces[player][y];. If an opponent piece was hit it has to be removed pieces[opponent][y] = 0; and placed on the bar.
Last edited on
Hello Peter87,

Thank you for your response.

What you have described makes good sense, and so is pretty much what I'm looking for.

I shall update my code and see if I can make it work - considering I'm not the most experienced user. I shall post my updated code here when I'm finished so others can check to see if all code works accordingly.

Because I'm to produce a "simplified" version of backgammon, essentially, as part of my assignment, and as recommended by my lecturer, the pieces that move are those furthest from the end position. Is there any particular code you can suggest for me to calculate the furthest position on the board?

For instance, RED starts at Pieces[0][0] on the board (BLACK at Pieces[1][23]). At Pieces[0][0], 5 pieces will be held here at the start of the game. All these pieces will be moved until none are left held in this position, after which I need to find the next piece in the array furthest from the end position and move this piece up the board according to the dice value.

This is what I'm thinking:

1
2
3
4
5
6
7
8
9
10
11
12
13
        int pos = 0;
        bool found = false;
        do
        {
            if (Pieces[RED][pos] > 0)
            {
                found = true;
            }
            else
            {
                pos++;
            }
        } while (pos < Pieces[RED][23] && !found);


Any ideas here?

One other issue I have now, after working from what you have explained above, is the output of the pieces.

I wish to have a function that loops through the entire array Pieces[2][24] and updates the location of all pieces on the board, each time the dice is thrown.

Some code i have generated below to give a rough idea (does not work):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    // Draw the RED Pieces
    for (int i = 0; i < 24; i++) // Number of triangles on board (24 in total)
    {
        // Checks value at each triangle, for instance: [0][0] is first triangle, [0][1] is second etc...
        if (Pieces[0][i] > 0) // If value at index greater than zero, move to "for" loop below and display pieces
        {
            double X1 = 12.5, Y1 = 1.5; // 'x' and 'y' coordinates of starting position for RED pieces
            for (int j = 0; j < Pieces[0][i]; j++) // Displays the number of pieces at index on trianlge
            {
                // 'x' and 'y' coordinates need to update once pieces for first triangle are displayed
                // Unsure how to do this???
                cwin << BrushColour(204, 50, 50) << PenColour(204, 50, 50) << PenWidth() << Circle(Point(X1, Y1), 0.45);
            }
        }
    }


Ideas here would be appreciated.

Once again, thanks for your response - greatly appreciated.

Bevan
Last edited on
Sorry, I didn't see that you added more text to your previous post until now.

I think the code you have posted for finding the furthest position should work. If you want to use the same code for both players you could introduce two new arrays to store the start and end positions of each player.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int startPosition[2] = {0, 23};
int endPosition[2] = {23, 0};

int pos = startPosition[player];
bool found = false;
do
{
	if (Pieces[player][pos] > 0)
	{
		found = true;
	}
	else
	{
		pos += moveDirection[player];
	}
} while (pos != endPosition[player] && !found);



About the graphics. You will have to come up with a way to calculate the position from i and j.
Hi Peter87,

Thanks for your help so far - greatly appreciated.

It's nearly finished - just having some issues with states (help control the game).

Thanks,
Bevan
Topic archived. No new replies allowed.