示例#1
0
        private void moveBlue()
        {
            // the blue oponent moves too fast, so a delay is added in order for the human to spot clearly what move has been made
            var timer = new DispatcherTimer {
                Interval = TimeSpan.FromSeconds(1)
            };

            timer.Start();
            timer.Tick += (sender, args) =>
            {
                timer.Stop();
                warning.Text       = "";
                warning.Visibility = System.Windows.Visibility.Hidden;
                int move = 0;
                // blue opponent's first move is random
                int i = 1, j = 2, index = 1;
                if (firstMoveBlue)
                {
                    while (a[i, j] != 0)
                    {
                        index = rand.Next(1, 25);
                        Cell c = new Cell(index);
                        i = c.i;
                        j = c.j;
                    }
                    a[i, j]       = 2;
                    firstMoveBlue = false;
                    drawEllipse(ellipseV[index], 2, blueColor, 35, false);
                    move         = index;
                    ballPlayer2 -= 1;
                }
                else
                if (thirdPhaseBlue)
                {
                    // check if red will soon create a mill
                    List <Cell> incompleteRedMill = Mill.checkDangerMill(a, 1);
                    // check if blue will create soon a mill
                    List <Cell> incompleteBlueMill = Mill.checkDangerMill(a, 2);
                    List <Mill> blueMills          = Mill.getMills(a, 2);
                    Cell        moved = Mill.getBlueStoneThirdPhase(a);
                    if (incompleteBlueMill.Count != 0)
                    {
                        a[moved.i, moved.j] = 0;
                        drawEllipse(ellipseV[Helper.toIJ(moved.i, moved.j)], 2, yellowColor, 15, false);
                        int ii = incompleteBlueMill[0].i;
                        int jj = incompleteBlueMill[0].j;
                        drawEllipse(ellipseV[Helper.toIJ(ii, jj)], 2, blueColor, 35, false);
                        a[ii, jj] = 2;
                    }
                    else
                    if (incompleteRedMill.Count != 0)
                    {
                        a[moved.i, moved.j] = 0;
                        drawEllipse(ellipseV[Helper.toIJ(moved.i, moved.j)], 2, yellowColor, 15, false);
                        int ii = incompleteRedMill[0].i;
                        int jj = incompleteRedMill[0].j;
                        drawEllipse(ellipseV[Helper.toIJ(ii, jj)], 2, blueColor, 35, false);
                        a[ii, jj] = 2;
                    }
                    else
                    if (blueMills.Count != 0)
                    {
                        Cell x = new Cell(blueMills[0].start);
                        for (int k = 1; k < 25; k++)
                        {
                            Cell n = new Cell(k);
                            if (a[n.i, n.j] == 0)
                            {
                                a[n.i, n.j] = 2;
                                a[x.i, x.j] = 0;
                                drawEllipse(ellipseV[Helper.toIJ(x.i, x.j)], 2, yellowColor, 15, false);
                                drawEllipse(ellipseV[Helper.toIJ(n.i, n.j)], 2, blueColor, 35, false);
                            }
                        }
                    }
                    else
                    {
                        int  ct     = 0;
                        Cell first  = new Cell(0);
                        Cell second = new Cell(0);
                        for (int k = 1; k < 25; k++)
                        {
                            Cell x = new Cell(k);
                            if ((a[x.i, x.j] == 2) && (ct == 0))
                            {
                                ct++;
                                first = x;
                            }
                            else
                            if ((a[x.i, x.j] == 2) && (ct == 1))
                            {
                                second = x;
                                a[first.i, first.j] = 0;
                                drawEllipse(ellipseV[Helper.toIJ(first.i, first.j)], 2, yellowColor, 15, false);
                                for (int n = 1; n < 25; n++)
                                {
                                    Cell n_c = new Cell(n);
                                    if (Cell.neighbour(a, n_c, second) && (a[n_c.i, n_c.j] == 0))
                                    {
                                        a[n_c.i, n_c.j] = 2;
                                        drawEllipse(ellipseV[Helper.toIJ(n_c.i, n_c.j)], 2, blueColor, 35, false);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                // if the game is in phase 1 check whether red will soon form a mill (two neighbour red stones)
                // and whether blue will form soon a mill;
                // completing blue mill has preference over blocking formation of red mill because after
                // completing a blue mill, it will remove a red stone that is inside a dangerous/ incomplete red mill)
                if (ballPlayer2 > 0)
                {
                    // check if red will soon create a mill
                    List <Cell> incompleteRedMill = Mill.checkDangerMill(a, 1);
                    // check if blue will create soon a mill
                    List <Cell> incompleteBlueMill = Mill.checkDangerMill(a, 2);
                    // try to build blue mill
                    Cell aimBlueMill = Mill.goMill(a);
                    // if no danger of red mill was detected and blue will not create soon a mill
                    if (incompleteRedMill.Count == 0 && incompleteBlueMill.Count == 0)
                    {
                        if (aimBlueMill != null)
                        {
                            a[aimBlueMill.i, aimBlueMill.j] = 2;
                            drawEllipse(ellipseV[Helper.toIJ(aimBlueMill.i, aimBlueMill.j)], 2, blueColor, 35, false);
                            move = Helper.toIJ(aimBlueMill.i, aimBlueMill.j);
                        }
                        else
                        {
                            i = 1; j = 2; index = 1;
                            while (a[i, j] != 0)
                            {
                                index = rand.Next(1, 25);
                                Cell c = new Cell(index);
                                i = c.i;
                                j = c.j;
                            }
                            a[i, j] = 2;
                            drawEllipse(ellipseV[index], 2, blueColor, 35, false);
                            move = index;
                        }
                    }
                    else
                    if ((incompleteRedMill.Count != 0) && (incompleteBlueMill.Count == 0))
                    {
                        // danger for red to create mill and blue will not create soon a mill
                        // => move blue stone to block red mill
                        a[incompleteRedMill[0].i, incompleteRedMill[0].j] = 2;
                        drawEllipse(ellipseV[Helper.toIJ(incompleteRedMill[0].i, incompleteRedMill[0].j)], 2, blueColor, 35, false);
                        move = Helper.toIJ(incompleteRedMill[0].i, incompleteRedMill[0].j);
                        checkMill(2, ellipseV[Helper.toIJ(incompleteRedMill[0].i, incompleteRedMill[0].j)]);
                    }
                    else
                    if ((incompleteRedMill.Count != 0) && (incompleteBlueMill.Count != 0))
                    {
                        // danger for red to create mill and blue will create soon a mill
                        // => complete blue mill
                        a[incompleteBlueMill[0].i, incompleteBlueMill[0].j] = 2;
                        drawEllipse(ellipseV[Helper.toIJ(incompleteBlueMill[0].i, incompleteBlueMill[0].j)], 2, blueColor, 35, false);
                        move = Helper.toIJ(incompleteBlueMill[0].i, incompleteBlueMill[0].j);
                    }
                    else
                    {
                        // no danger for red to create mill and blue will create soon a mill
                        a[incompleteBlueMill[0].i, incompleteBlueMill[0].j] = 2;
                        drawEllipse(ellipseV[Helper.toIJ(incompleteBlueMill[0].i, incompleteBlueMill[0].j)], 2, blueColor, 35, false);
                        move = Helper.toIJ(incompleteBlueMill[0].i, incompleteBlueMill[0].j);
                    }
                    ballPlayer2 -= 1;
                }
                else
                if (secondPhase)
                {
                    List <Cell> blueDangerousMills = Mill.checkDangerMill(a, 2);
                    List <Cell> redDangerousMills  = Mill.checkDangerMill(a, 1);
                    List <Mill> blueMills          = Mill.getMills(a, 2);
                    bool        blueMoved          = false;
                    // search through all the blue mills to get a free neighbour position where to move
                    // one of the blue stones from inside the mill (strategy for removing red stones => next move will close back the mill)
                    if (!blueMoved && (!breakMill))
                    {
                        HashSet <int> blueStones = new HashSet <int>();
                        foreach (Mill m in blueMills)
                        {
                            blueStones.Add(m.start);
                            blueStones.Add(m.middle);
                            blueStones.Add(m.stop);
                        }
                        if (blueStones.Count != 0)
                        {
                            foreach (int stone in blueStones)
                            {
                                if (!blueMoved)
                                {
                                    for (int k = 1; k < 25; k++)
                                    {
                                        Cell n_k = new Cell(k);
                                        Cell n_s = new Cell(stone);
                                        if (Cell.neighbour(a, n_k, n_s) && (a[n_k.i, n_k.j] == 0))
                                        {
                                            drawEllipse(ellipseV[Helper.toIJ(n_k.i, n_k.j)], 2, blueColor, 35, false);
                                            move = Helper.toIJ(n_k.i, n_k.j);
                                            drawEllipse(ellipseV[Helper.toIJ(n_s.i, n_s.j)], 2, yellowColor, 15, false);
                                            a[n_k.i, n_k.j] = 2;
                                            a[n_s.i, n_s.j] = 0;
                                            blueMoved       = true;
                                            breakMill       = true;
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                    // try to find an incomplete blue mill and if a blue stone is in the neighbourhood, move it to complete the mill
                    if (!blueMoved && breakMill)
                    {
                        HashSet <int> StonesInMills = new HashSet <int>();
                        foreach (Mill m in blueMills)
                        {
                            StonesInMills.Add(m.start);
                            StonesInMills.Add(m.middle);
                            StonesInMills.Add(m.stop);
                        }

                        foreach (Cell blue in blueDangerousMills)
                        {
                            for (int k = 1; k < 25; k++)
                            {
                                if (!blueMoved)
                                {
                                    Cell n = new Cell(k);
                                    if (Cell.neighbour(a, n, blue) && (a[n.i, n.j] == 2) && (!StonesInMills.Contains(k)))
                                    {
                                        drawEllipse(ellipseV[Helper.toIJ(blue.i, blue.j)], 2, blueColor, 35, false);
                                        move = Helper.toIJ(blue.i, blue.j);
                                        drawEllipse(ellipseV[Helper.toIJ(n.i, n.j)], 2, yellowColor, 15, false);
                                        a[n.i, n.j]       = 0;
                                        a[blue.i, blue.j] = 2;
                                        blueMoved         = true;
                                        breakMill         = false;
                                        break;                                                         // found a good position to move the blue stone, so stop searching
                                    }
                                }
                                else
                                {
                                    break;                                                     // break from the outer loop after finding a good postion to move
                                }
                            }
                        }
                    }
                    // if the precedent case didn't result in a position where to move the blue stone
                    // then search through all incomplete red mills for a position that has in
                    // the neighbourhood a blue stone and if one is found, move blue stone to block forming a red mill
                    if (!blueMoved)
                    {
                        foreach (Cell red in redDangerousMills)
                        {
                            for (int k = 1; k < 25; k++)
                            {
                                if (!blueMoved)
                                {
                                    Cell n = new Cell(k);
                                    if (Cell.neighbour(a, n, red) && (a[n.i, n.j] == 2))
                                    {
                                        drawEllipse(ellipseV[Helper.toIJ(red.i, red.j)], 2, blueColor, 35, false);
                                        move = Helper.toIJ(red.i, red.j);
                                        drawEllipse(ellipseV[Helper.toIJ(n.i, n.j)], 2, yellowColor, 15, false);
                                        a[n.i, n.j]     = 0;
                                        a[red.i, red.j] = 2;
                                        blueMoved       = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }

                    while (!blueMoved)
                    {
                        index = rand.Next(1, 25);

                        for (int k = 1; k < 25; k++)
                        {
                            Cell n_k = new Cell(k);
                            Cell c   = new Cell(index);
                            if ((a[c.i, c.j] == 2) && (Cell.neighbour(a, n_k, c)) && (a[n_k.i, n_k.j] == 0))
                            {
                                a[n_k.i, n_k.j] = 2;
                                a[c.i, c.j]     = 0;
                                drawEllipse(ellipseV[k], 2, blueColor, 35, false);
                                drawEllipse(ellipseV[index], 2, yellowColor, 15, false);
                                move      = index;
                                blueMoved = true;
                            }
                        }
                    }
                }

                Helper.printBoard(a);
                blueMill = false;
                checkMill(2, ellipseV[move]);
                oldBlueMills = bMills.Count;
                bMills       = Mill.getMills(a, 2);
                newBlueMills = bMills.Count;
                if (oldBlueMills - newBlueMills == 1)
                {
                    removedRedStone--;                      // blue moved so that one mill is broken
                }
                if (newBlueMills != removedRedStone)
                {
                    Cell red = Mill.removeRedStone(a);
                    warning.SetResourceReference(TextBox.TextProperty, "redStoneRemoved");
                    warning.Visibility = System.Windows.Visibility.Visible;
                    a[red.i, red.j]    = 0;
                    Helper.printBoard(a);
                    drawEllipse(ellipseV[Helper.toIJ(red.i, red.j)], 1, yellowColor, 15, false);
                    removedRedStone++;
                }
            };
        }