vendredi 21 septembre 2018

C# execlude numbers from a list in random generation

i am making a tic tac toe game and i am making it to be unbeatable but first i have to let the copmuter know the rules

i'm stack in a step that is => when it's the computer's turn and we have just started the game so no win case so it's up to the coputer the generate a random number that will be the computer choise ( the block where he mark X or O ) so i need it to gennerate a number from 1 to 9 but by excluding the already used blocks ( numbers ) i tried doing that by making a list and adding a number every time the humain player use a block but i can't find a way to use those numbers from the list as exclusion for the random choise of the computer here is what i tried and thnx in advance :

    //random

    List<int> cas = new List<int>();
    if (c1 == true)
    {
        cas.Add(1);
    }
    if (c2 == true)
    {
        cas.Add(2);
    }
    if (c3 == true)
    {
        cas.Add(3);
    }
    if (c4 == true)
    {
        cas.Add(4);
    }
    if (c5 == true)
    {
        cas.Add(5);
    }
    if (c6 == true)
    {
        cas.Add(6);
    }
    if (c7 == true)
    {
        cas.Add(7);
    }
    if (c8 == true)
    {
        cas.Add(8);
    }
    if (c9 == true)
    {
        cas.Add(9);
    }


    for (int i = 0; i < cas.Count; i++)
    {
        random_except_list(cas[]);
    }

public static int random_except_list(int[] x)
{
    Random r = new Random();
    int result = r.Next(1, 9 - );

    for (int i = 0; i < x.Length; i++)
    {
        if (result < x[i])
            return result;
        result++;
    }
    return result;
}




Aucun commentaire:

Enregistrer un commentaire