Beispiel #1
0
        protected void whowin()
        {
            /*
             * //拿到最後落子的X,Y座標
             * int centerX = board.Lastnode.X;
             * int centerY = board.Lastnode.Y;
             * //檢查八個方向(3*3-1),不含中心點
             * int fourtime = 0;
             * int Xdirection = -1, Ydirection = -1;
             * for (Xdirection = -1; Xdirection <= 1; Xdirection++)
             * {
             *  for (Ydirection = -1; Ydirection <= 1; Ydirection++)
             *  {
             *      if (Xdirection == 0 && Ydirection == 0)//扣除中間的情況
             *          break;
             *
             *      int number = 1;//紀錄看到幾顆相同棋子
             *      int five = 1;
             *      int Xdir = Xdirection, Ydir = Ydirection;
             *      while (true)
             *      {
             *          int seeX = centerX + number * Xdir;
             *          int seeY = centerY + number * Ydir;
             *
             *          //檢查顏色是否相同 ,且數字不超過矩陣大小
             *          if (seeX < 0 || seeX >= Board.count ||
             *          seeY < 0 || seeY >= Board.count ||
             *          board.gettype(seeX, seeY) != nexttype)
             *          {
             *              if (Xdir == -1 && Ydir == -1) { Xdir = 1; Ydir = 1; fourtime++;number = 1; continue; }
             *              else if (Xdir == -1 && Ydir == 0) { Xdir = 1; Ydir = 0; fourtime++; number = 1; continue; }
             *              else if (Xdir== -1 && Ydir == 1) { Xdir = 1; Ydir = -1; fourtime++; number = 1; continue; }
             *              else if (Xdir == 0 && Ydir == -1) { Xdir = 0; Ydir = 1; fourtime++; number = 1; continue; }
             *              break;
             *          }
             *          five++;
             *          number++;
             *      }
             *      if (five == 5)//檢查是否看到五顆棋子
             *      {
             *          winner = nexttype;
             *      }
             *  }
             *  if (Xdirection == 0 && Ydirection == 0)//扣除中間的情況
             *      break;
             * }
             */
            int LeftTop = 1, Horizon = 1, LeftDown = 1, Vertical = 1, tempx, tempy, x = board.Lastnode.X, y = board.Lastnode.Y;

            tempx = x - 1;
            tempy = y - 1;
            while (tempx >= 0 && tempy >= 0 && board.gettype(tempx, tempy) == nexttype)
            {
                LeftTop++;
                tempx--;
                tempy--;
            }
            //設定左上的左上端點
            tempx = x + 1;
            tempy = y + 1;
            while (tempx < 15 && tempy < 15 && board.gettype(tempx, tempy) == nexttype)
            {
                LeftTop++;
                tempx++;
                tempy++;
            }
            //設定左上的右下端點
            tempx = x - 1;
            tempy = y;
            while (tempx >= 0 && board.gettype(tempx, tempy) == nexttype)
            {
                Horizon++;
                tempx--;
            }
            //設定水平的左端點
            tempx = x + 1;
            tempy = y;
            while (tempx < 15 && board.gettype(tempx, tempy) == nexttype)
            {
                Horizon++;
                tempx++;
            }
            //設定水平的右端點
            tempx = x;
            tempy = y - 1;
            while (tempy >= 0 && board.gettype(tempx, tempy) == nexttype)
            {
                Vertical++;
                tempy--;
            }
            //設定垂直的上端點
            tempx = x;
            tempy = y + 1;
            while (tempy < 15 && board.gettype(tempx, tempy) == nexttype)
            {
                Vertical++;
                tempy++;
            }
            //設定垂直的下端點
            tempx = x - 1;
            tempy = y + 1;
            while (tempx >= 0 && tempy < 15 && board.gettype(tempx, tempy) == nexttype)
            {
                LeftDown++;
                tempx--;
                tempy++;
            }
            //設定左下的左下端點
            tempx = x + 1;
            tempy = y - 1;
            while (tempx < 15 && tempy >= 0 && board.gettype(tempx, tempy) == nexttype)
            {
                LeftDown++;
                tempx++;
                tempy--;
            }
            if (LeftDown >= 5 || LeftTop >= 5 || Horizon >= 5 || Vertical >= 5)
            {
                winner = nexttype;
            }
        }
Beispiel #2
0
 public void rearray()//清空PArray;
 {
     board.Rearray();
     winner   = Ptype.NONE;
     nexttype = Ptype.BLACK;
 }
 public Visual Visualize(Ptype ptype, object parameter)
 {
     return(new Grid());
 }
            public Visual Visualize(Ptype ptype, object parameter = null)
            {
                if (!ptype.Model.Name.Equals("ninepart"))
                {
                    throw new Exception("Nine Part Model cannot render prototypes created by other models.");
                }

                Feature bottomleftFeature  = ptype.Feature("bottomleft");
                Feature toprightFeature    = ptype.Feature("topright");
                Feature bottomrightFeature = ptype.Feature("bottomright");
                Feature topleftFeature     = ptype.Feature("topleft");

                Region topregion      = ptype.Region("top");
                Region leftregion     = ptype.Region("left");
                Region bottomregion   = ptype.Region("bottom");
                Region rightregion    = ptype.Region("right");
                Region interiorRegion = ptype.Region("interior");

                Grid grid = new Grid();

                ////Find the biggest dimensions for each Part object so that
                ////we can figure out how many cells to add into the grid.
                int longestTopOrBottom = (int)Math.Max(topregion.Bitmap.Width, bottomregion.Bitmap.Width);
                int longestLeftOrRight = (int)Math.Max(leftregion.Bitmap.Height, rightregion.Bitmap.Height);
                int widestTLOrBL       = (int)Math.Max(topleftFeature.Bitmap.Width, bottomleftFeature.Bitmap.Width);
                int widestTROrBR       = (int)Math.Max(toprightFeature.Bitmap.Width, bottomrightFeature.Bitmap.Width);
                int tallestTLOrTR      = (int)Math.Max(topleftFeature.Bitmap.Height, toprightFeature.Bitmap.Height);
                int tallestBLOrBR      = (int)Math.Max(bottomleftFeature.Bitmap.Height, bottomrightFeature.Bitmap.Height);
                int interiorWidth      = 0;
                int interiorHeight     = 0;

                if (interiorRegion != null)
                {
                    interiorHeight = interiorRegion.Bitmap.Height;
                    interiorWidth  = interiorRegion.Bitmap.Width;
                }

                //Assign the width and height of the grid.
                int width  = Math.Max(widestTLOrBL + longestTopOrBottom + widestTROrBR + 2, interiorWidth + leftregion.Bitmap.Width + rightregion.Bitmap.Width + 2);
                int height = Math.Max(tallestTLOrTR + longestLeftOrRight + tallestBLOrBR + 2, interiorHeight + topregion.Bitmap.Height + bottomregion.Bitmap.Height + 2);

                //Set the rows and columns of the grid.
                for (int row = 0; row < height; row++)
                {
                    RowDefinition rowdef = new RowDefinition();
                    grid.RowDefinitions.Add(rowdef);
                }

                for (int col = 0; col < width; col++)
                {
                    ColumnDefinition coldef = new ColumnDefinition();
                    grid.ColumnDefinitions.Add(coldef);
                }

                //Add each Part to the grid (cells = pixels).
                PtypeVisualizerHelper.AddFeatureToGrid(topleftFeature, new Prefab.Point(0, 0), grid);
                PtypeVisualizerHelper.AddFeatureToGrid(toprightFeature, new Prefab.Point(width - toprightFeature.Bitmap.Width, 0), grid);
                PtypeVisualizerHelper.AddFeatureToGrid(bottomleftFeature, new Prefab.Point(0, height - bottomleftFeature.Bitmap.Height), grid);
                PtypeVisualizerHelper.AddFeatureToGrid(bottomrightFeature, new Prefab.Point(width - bottomrightFeature.Bitmap.Width, height - bottomrightFeature.Bitmap.Height), grid);


                PtypeVisualizerHelper.AddHorizontalRegionToGrid(topregion.Bitmap, new Prefab.Point(topleftFeature.Bitmap.Width + 1, 0), grid);
                PtypeVisualizerHelper.AddHorizontalRegionToGrid(bottomregion.Bitmap, new Prefab.Point(bottomleftFeature.Bitmap.Width + 1, height - bottomregion.Bitmap.Height), grid);
                PtypeVisualizerHelper.AddVerticalRegionToGrid(leftregion.Bitmap, new Prefab.Point(0, topleftFeature.Bitmap.Height + 1), grid);
                PtypeVisualizerHelper.AddVerticalRegionToGrid(rightregion.Bitmap, new Prefab.Point(width - rightregion.Bitmap.Width, toprightFeature.Bitmap.Height + 1), grid);


                if (interiorRegion != null)
                {
                    Prefab.Point location;
                    if (interiorRegion.MatchStrategy.Equals("horizontal"))
                    {
                        location = new Prefab.Point(topleftFeature.Bitmap.Width + 1, (height - interiorHeight) / 2);
                    }
                    else
                    {
                        location = new Prefab.Point((width - interiorWidth) / 2, topleftFeature.Bitmap.Height + 1);
                    }

                    PtypeVisualizerHelper.AddEachPixelOfBitmapToGrid(interiorRegion.Bitmap, location, grid);
                }


                return(grid);
            }
Beispiel #5
0
        private static void EraseFeaturesFromforeground(Bitmap foreground, Tree occurrence, Ptype ptype)
        {
            //top left
            Feature topleft = ptype.Feature("topleft");

            for (int row = occurrence.Top; row < occurrence.Top + topleft.Bitmap.Height; row++)
            {
                for (int col = occurrence.Left; col < occurrence.Left + topleft.Bitmap.Width; col++)
                {
                    foreground[row, col] = Utils.BACKGROUND;
                }
            }

            //top right
            Feature topright = ptype.Feature("topright");

            for (int row = occurrence.Top; row < occurrence.Top + topleft.Bitmap.Height; row++)
            {
                for (int col = occurrence.Left + occurrence.Width - topright.Bitmap.Width; col < occurrence.Left + occurrence.Width; col++)
                {
                    foreground[row, col] = Utils.BACKGROUND;
                }
            }

            //bottom left
            Feature bottomleft = ptype.Feature("bottomleft");

            for (int row = occurrence.Top + occurrence.Height - bottomleft.Bitmap.Height; row < occurrence.Top + occurrence.Height; row++)
            {
                for (int col = occurrence.Left; col < occurrence.Left + bottomleft.Bitmap.Width; col++)
                {
                    foreground[row, col] = Utils.BACKGROUND;
                }
            }

            //bottom right
            Feature bottomright = ptype.Feature("bottomright");

            for (int row = occurrence.Top + occurrence.Height - bottomright.Bitmap.Height; row < occurrence.Top + occurrence.Height; row++)
            {
                for (int col = occurrence.Left + occurrence.Width - bottomright.Bitmap.Width; col < occurrence.Left + occurrence.Width; col++)
                {
                    foreground[row, col] = Utils.BACKGROUND;
                }
            }
        }