user2431834
user2431834

Reputation: 1

Checkers Board Assistance

I'm just wondering if there is a simpler way of doing this:

for (int i = 0; i < 1; i++)
{
    for (int j = 0; i < 8; j+2)
    {
        board[ i, j ] = 2;
        board[( i + 1 ), j ] = 2;
        board[( i + 2 ), j ] = 2;
    }
}  

What I'm trying to do is place checkers pieces on the actual checkers board. So this is to place the black pieces on the top.

P.S. If you could also give me some help on the bottom set of pieces(white).

Upvotes: 0

Views: 1763

Answers (3)

Nikola Radosavljević
Nikola Radosavljević

Reputation: 6911

You missed to tell us what exactly is it that you want to achieve. I see several big errors in your code, so I'll assume that you don't have full grasp of how for loop works. I hope it's not too presumptuous that I'm trying to explain it here


For loop

For loop is used to execute same portion of code several times. How many times it will be executed depends on conditions you set. Most often, you will see it in this format:

for (int i = 0; i < n; i++)
{
    // Some code
}

This for loop executed code within the brackets ({ and }) n times. This is not only way to define a loop. More thorough definition of a loop is following:

for (<initialization>; <condition>; <afterthought>)
  • Initialization - You can some variables needed for looping. This is executed once before code within the loop is executed first time. This is optional, and you can leave it empty and use variable declared before in condition.
  • Condition - This is executed before each execution of code within the loop. If condition expression evaluates to true, loop is executed. Once loop is executed and afterthought is executed, condition is evaluated again and again until it evaluates to false. Condition is also optional. If you leave it out, in C# loop will be executed again until you break the loop in a different way.
  • Afterthought - This is executed each time code within the loop is finished executing. This is usually used to increment a variable on which loop depends. This is also optional.

Fixing your code

I assume you wanted to mark fields in a 8x8 matrix like in a checkerboard, although this is not stated in your question. You could do it this way:

// For each row in a board (start with 0, go until 7, increase by 1)
for (int i = 0; i < 8; i++)
{
    // start coloring the row. Determine which field within the row needs
    // to be black. In first row, first field is black, in second second
    // field is black, in third row first field is black again and so on.
    // So, even rows have black field in first blace, odd rows have black
    // on second place.
    // We calculate this by determining division remained when dividing by 2.
    int firstBlack = i % 2;
    // Starting with calculated field, and until last field color fields black.
    // Skip every second field. (start with firstBlack, go until 7, increase by 2)
    for (int j = firstBlack; j < 8; j += 2)
    {
        // Color the field black (set to 2)
        board[i][j] = 2;
    }
}

You can see my comments inline.


Big errors in your code

// This loop would be executed only once. It goes from 0 to less than 1 and is increased
// after first execution. You might as well done it without the loop.
for (int i = 0; i < 1; i++)
{
    // This doesn't make sense, because you use i in condition, and initialize
    // and update j.
    // Also, you are trying to update j, but you are not doing so. You are not
    // assigning anything to you. You should do j+=2 to increase by two. Or you
    // can do j = j + 2
    for (int j = 0; i < 8; j+2)
    {
        // I didn't really understand what you were trying to achieve here
        board[ i, j ] = 2;
        board[( i + 1 ), j ] = 2;
        board[( i + 2 ), j ] = 2;
    }
}  

Upvotes: 0

Menelaos Vergis
Menelaos Vergis

Reputation: 3955

Modulo will do the trick but i think that TonyS's anwser was my first reaction and I would prefer that insteed the one shown below.

  char[,] board = new char[8,8];
    private void InitializeBoard()
    {
        string BoardRepresentation = "";
        //for every board cell
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                //initialize board cell
                board[i, j] = '0';

                if (j <= 2)//black top
                {
                    //Modulo is the trick
                    if ((j - i) == 0 || ((j - i) % 2) == 0)
                    {
                        board[i, j] = 'B';
                    }
                }
                else if (j >= 5) //white bot
                {
                    if ((j - i) == 0 || ((j - i) % 2) == 0)
                    {
                        board[i, j] = 'W';
                    }
                }
            }
        }

        for (int j = 0; j < 8; j++)
        {
            for (int i = 0; i < 8; i++)
            {
                BoardRepresentation += board[i, j] + " ";
            }
            BoardRepresentation += Environment.NewLine;
        }
    }

Upvotes: 0

TonyS
TonyS

Reputation: 238

Apart from fixing the loop you could otherwise explicitly place the pieces, makes it more readable

int[,] board = new[,]{{1,0,1,0,1,0,1,0},
                      {0,1,0,1,0,1,0,1},
                      {1,0,1,0,1,0,1,0},
                      {0,0,0,0,0,0,0,0},
                      {0,0,0,0,0,0,0,0},
                      {0,1,0,1,0,1,0,1},
                      {1,0,1,0,1,0,1,0},
                      {0,1,0,1,0,1,0,1}};

Upvotes: 4

Related Questions