What's the best way to place 20 pieces on a chessboard in random places?

The idea I had to solve is to create an 8x8 buffer, fill it with pointers to my checkers (all 20 of them) and leave the rest 0 (null), then run the shuffle algorithm on the buffer, and thats it (just read it as an 8x8 array)

  • I was wondering if there is a better way to do this.
  • I need to write it in C # and my suggestion will not work the way I described it.

anyone?

0


source to share


4 answers


To put together Borzio's idea, as you get closer to part twentieth, there is a 1 in 3 chance that you will have to re-generate the random number and try again. With 20 pieces, you're probably still safe, but if you had, say, 40 pieces, it would be better than a 1 in 2 chance and you might be stuck waiting.

  • Build an array of boards, all empty.
  • Copy the link to each square before the list ( emptySquareList

    ).
  • A loop for each piece you want to add:
    • Create a random number between 0

      and emptySquareList.Length-1

      .
    • Place a piece in this square
    • Remove the square from emptySquareList

      .


This way you always keep a list of empty squares and choose from them. Note that array of indices to your array of arrays works just as well and may be faster ( Enumerable.Range(0,64)

).

I encourage you to experiment with the random check algorithm as well as this one; see which one is more effective.

+7


source


An 8x8 matrix containing either pointers (C ++) for pieces of objects or references (C # / java) seems very reasonable.

You can use a 1D array that has 64 elements (you can easily map 2D indices to a 1D array) and use something like this:

using System;
using System.Linq;

Piece[] board = whatever();

// New random number generator.
Random random = new Random();

// LINQ query orders each element by the next random number.
Piece[] randomBoard = (from piece in board
    orderby random.Next()
    select piece).ToArray();

      

NOTE. I am not a C # programmer, but it seems reasonable.



To convert a pair x, y to an array index, you use this algorithm:

int index = (y * 8) + x;

      

NOTE 2: I don't know if you need to follow the checker placement rules or not, if so, you need to do something smarter. Perhaps you can have a 32 element array large (representing all black squares). Shuffle this, then assign each element in 32 large arrays to each "black" square of a real array of 64 elements.

0


source


If you need to follow placement rules given your example of an 8x8 matrix, I would also consider it as a 1st array as Evan pointed out (see his math for x and y definition) -

Black and white squares are odd and even index values; But hey, you never said you needed placement rules - I also assume that you are not looking for C # code, but more logic level ideas ...

If you are trying to find a Linq-Challenged solution, consider the following:

  • Build a board array, all empty; (64 elements)
  • Build an array of "game pieces" from your pieces.
  • Create a cycle from 1 to # pieces.
  • Create a random number between 1 and # spaces (64)
  • If the board space pointed to by # 4 is 0, repeat step 4 (or you can "bye"! 0)
  • Place the piece (# from the loop in step 3) in square X from step 4

Just one idea, which will surely be a lot -

Hope it helps ...

0


source


To answer the question posed:

  • Place the first chess piece on a random spot that is not yet occupied
  • Repeat for the remaining 19 pieces.
Seriously, you haven't given us enough information to continue answering this question. Here are some questions:
  • What are chess pieces?
  • What are the colors (16 pieces each) and the colors? Will White avoid capture by Black and vice versa?

Assuming no rules are followed, except that you need to place all 20 on the board i.e. no collisions, this will do what you want:

Piece[] pieces = ... your 20 pieces here
Piece[] board = new Piece[8 * 8];
Random r = new Random();
foreach (Piece piece in pieces)
{
    while (true)
    {
        Int32 location = r.Next(board.Length);
        if (board[location] == null)
        {
            board[location] = piece;
            break;
        }
    }
}

      

A simpler solution, if you already have helper routines, would be to simply place them at the beginning of the 64 element array and then shuffle the entire array. This would shuffle those 20 pieces and 44 empty squares around and effectively place them in random spots.

0


source







All Articles