Ejemplo n.º 1
0
    private Point getFullRandom(bool[,] fields)
    {
        //this function returns a random viable point in fields

        ChompBoard   thisBoard = (ChompBoard)board;
        List <Point> viable    = new List <Point>();

        //get viable
        if (!(fields[0, 1] || fields[1, 0]))
        {
            return(new Point(0, 0));
        }

        for (int x = 0; x < fields.GetLength(0); x++)
        {
            for (int y = 0; y < fields.GetLength(1); y++)
            {
                if (fields[x, y] && !(x == 0 && y == 0))
                {
                    viable.Add(new Point(x, y));
                }
            }
        }

        //select and return random viable point
        return(viable[(new Random()).Next(viable.Count)]);
    }
Ejemplo n.º 2
0
    private Point computerTurn()
    {
        ChompBoard thisBoard   = (ChompBoard)board;
        Point      destination = getWinningPoint(thisBoard.squares); //look for several simple win conditions

        if (destination == new Point(-1, -1))
        {
            //simple AI

            /*
             * destination = getBiasedRandom(thisBoard.squares); //look for a point that doesn't result in a simple win condition for the other player
             * if (destination == new Point(-1, -1))
             * { Console.WriteLine("getFullRandom"); return getFullRandom(thisBoard.squares); } //select random
             * else Console.WriteLine("getBiasedRandom");
             */

            //advanced with list (can display total number of remaining choices)

            /*
             * List<Point> nextLs;
             * for (int degree = DIFF; degree > 0; degree--)
             * {
             *  try
             *  { nextLs = getBiasedRandomRecursive(thisBoard.squares, degree); Console.WriteLine("Degree " + degree + ": " + nextLs.Count + " choices."); }
             *  catch (Exception e)
             *  { Console.WriteLine(degree + " failed: " + e.Message); continue; }
             *
             *  if (nextLs.Count > 0)
             *  { destination = nextLs[(new Random()).Next(nextLs.Count)]; }
             *  else destination = new Point(-1, -1);
             *
             *  if (destination != new Point(-1, -1)) return destination;
             * }
             * Console.WriteLine("getFullRandom"); return getFullRandom(thisBoard.squares);
             */

            //advanced with point
            for (int degree = DIFF; degree > 0; degree--)
            {
                try
                { destination = getBiasedRandomRecursivePoint(thisBoard.squares, degree); }
                catch (Exception e)
                { Console.WriteLine(degree + " failed: " + e.Message); continue; }

                if (destination != new Point(-1, -1))
                {
                    return(destination);
                }
            }
            Console.WriteLine("getFullRandom"); return(getFullRandom(thisBoard.squares));
        }
        else
        {
            Console.WriteLine("getWinningPoint");
        }

        return(destination);
    }