lundi 5 février 2018

get a random array element index by excluding some of that array's element values C#

I the following array of all false values

bool[] deniedMoves = new bool[64];

think of a game board with x possible moves. After a move is played, it is recorded, some of the moves become impossible in the next play. A game like Go would fit this description as you cannot play over an intersection that has already been played on.

I want the "AI" to play a random move from a list of possible moves. First move is OK:

System.Random randMove1 = new System.Random();
int selectedMove1 = randMove1.Next(1, deniedMoves.Length);

and then that move sets some of the values in my array as true.

For example...

void Reset()
{
        if (mainBoard[0, 0] == 1)
        deniedMoves[0] = true;
        deniedMoves[3] = true;
        deniedMoves[5] = true;
}

The next random move the "AI" will play cannot choose from all 64 possibilities so I don't want it to randomize to 5 for example.

I have tried something like this, but RandomElement doesn't belong...

IEnumerable<bool> queryMoves = deniedMoves.Where(deniedMove => deniedMove !=             
true).RandomElement();

My first question and I am terribly sorry for bad code and general coding ignorance. I have tried.




Aucun commentaire:

Enregistrer un commentaire