Exemple #1
0
 public ClearBarEventArgs(SquareArray underlying, int line, int tick) : base(tick)
 {
     Line    = line;
     Squares = new Square[underlying.GetUpperBound(1) + 1];
     for (var j = 0; j < underlying.GetUpperBound(1) + 1; j++)
     {
         Squares[j] = underlying[line, j];
     }
 }
Exemple #2
0
        public static List <SquareArray> Styles(int[][,] styles) // 根据传进的整数矩阵数组生成SquareArray的List
        {
            var result = new SquareArray[styles.Length];

            for (int i = 0; i < styles.Length; i++)
            {
                result[i] = Style(styles[i]);
            }
            return(new List <SquareArray>(result));
        }
Exemple #3
0
 public const int TempId = -2; // 临时方块Id
 public Block(SquareArray style, int blockId = -1)
 {
     if (blockId == -1) // 如果没有指定Id,生成新Id
     {
         Id = _id++;
     }
     else
     {
         Id = blockId; // 否则直接使用给定的Id
     }
     Style = style;
 }
Exemple #4
0
        private static SquareArray Style(int[,] style) // 根据传进的整数矩阵生成SquareArray类
        {
            var result = new SquareArray(style.GetUpperBound(0) + 1, style.GetUpperBound(1) + 1);

            for (int i = 0; i < style.GetUpperBound(0) + 1; i++)
            {
                for (int j = 0; j < style.GetUpperBound(1) + 1; j++)
                {
                    result[style.GetUpperBound(0) - i, j] = style[i, j] != 0 ? new Square(style[i, j]) : null;
                }
            }
            return(result);
        }
Exemple #5
0
 private bool Intersect(Block block, SquareArray array)
 {
     if (block.LPos < 0)
     {
         return(true);
     }
     for (int i = 0; i < block.Height; i++)
     {
         for (int j = 0; j < block.Width; j++)
         {
             if ((block.SquareAt(i, j) != null) && (array[block.LPos + i, block.RPos + j] != null))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #6
0
        private IController _controller;          // 游戏控制器

        #endregion



        public TetrisGame(int id, IEnumerable <SquareArray> styles, IEngine engine, ITetrisFactory factory, int w, int h,
                          int gameSpeed)
        {
            Width                   = w;
            Height                  = h;
            GameSpeed               = gameSpeed;
            engine.TickEvent       += UpdateDispatch;
            engine.PropertyChanged +=
                delegate(object sender, PropertyChangedEventArgs e) { this.Fps = (sender as IEngine).Fps; };
            _underLying   = new SquareArray(h, w);
            _factory      = factory;
            _factory.Game = this;
            if (_factory is CacheFactory) // 如果是CacheFactory,初始化
            {
                (_factory as CacheFactory).Init();
            }
            InitTickAPI();
            Id     = id;
            _tick  = 0;
            _state = 0;
        }
Exemple #7
0
        public int calRating(Block block)
        {
            int h = _game.Height;
            int w = _game.Width;
            int i, j;

            for (i = 0; i < block.Height; i++)
            {
                for (j = 0; j < block.Width; j++)
                {
                    if ((block.SquareAt(i, j) != null) && ((block.LPos + i < 0) || (block.RPos + j < 0) || (block.RPos + j >= w)))
                    {
                        return(-999999);
                    }
                }
            }

            SquareArray underlying = _game.UnderLying;
            SquareArray array      = new SquareArray(h, w);

            Array.Copy(underlying.Storage, array.Storage, h * w);

            while (!Intersect(block.Fall(), underlying))
            {
            }
            block.LPos++;

            for (i = 0; i < block.Height; i++)
            {
                for (j = 0; j < block.Width; j++)
                {
                    if (block.SquareAt(i, j) != null)
                    {
                        array[block.LPos + i, block.RPos + j] = block.SquareAt(i, j);
                    }
                }
            }
            int landHeight = 0; // 落子距底部的方格数
            int metric     = 0; // 消去行数 * 当前落子被消去的格子数
            int rowTrans   = 0; // 各行变换次数之和
            int colTrans   = 0; // 各列变换次数之和
            int holes      = 0; // 各列空洞个数之和
            int wells      = 0; // 井深之和加1
            int line       = 0; // 消去的行数
            int bl         = 0; // 当前落子消去的block数

            for (i = h - 1; i >= 0; i--)
            {
                bool clear = true;
                for (j = 0; j < w; j++)
                {
                    if (array[i, j] == null)
                    {
                        clear = false;
                        break;
                    }
                }
                if (clear)
                {
                    line++;
                    for (j = 0; j < block.Width; j++)
                    {
                        if (block.SquareAt(i - block.LPos, j) != null)
                        {
                            bl++;
                        }
                    }
                    for (int s = i; s < h - 1; s++)
                    {
                        for (j = 0; j < w; j++)
                        {
                            array[s, j] = array[s + 1, j];
                        }
                    }
                    for (j = 0; j < w; j++)
                    {
                        array[h - 1, j] = null;
                    }
                }
            }
            metric     = line * bl;
            landHeight = block.LPos - line;
            if (landHeight < 0)
            {
                landHeight = 0;
            }
            for (i = 0; i < h; i++)
            {
                bool pre = true;
                for (j = 0; j < w; j++)
                {
                    if (array[i, j] != null)
                    {
                        pre = false;
                        break;
                    }
                }
                if (pre)
                {
                    continue;
                }
                pre = true;
                for (j = 0; j < w; j++)
                {
                    if (pre ^ (array[i, j] != null))
                    {
                        pre = !pre;
                        rowTrans++;
                    }
                }
                if (!pre)
                {
                    rowTrans++;
                }
            }
            int[] height = new int[w + 2];
            for (i = 0; i < w; i++)
            {
                bool pre = true;
                for (j = 0; j < h; j++)
                {
                    if (pre ^ (array[j, i] != null))
                    {
                        pre = !pre;
                        colTrans++;
                    }
                }
                if (!pre)
                {
                    colTrans++;
                }
                for (j = h - 1; j >= -1; j--)
                {
                    if (j == -1)
                    {
                        break;
                    }
                    if (array[j, i] != null)
                    {
                        break;
                    }
                }
                height[i + 1] = j;
                for (j = j - 1; j >= 0; j--)
                {
                    if (array[j, i] == null)
                    {
                        holes++;
                    }
                }
            }
            height[0]     = h + 1;
            height[w + 1] = h + 1;
            for (i = 0; i < w; i++)
            {
                j = min(height[i], height[i + 2]);
                if (j > height[i + 1])
                {
                    wells += (j - height[i + 1]);
                }
            }


            return(-landHeight + metric - rowTrans - colTrans - 4 * holes - wells);
        }
Exemple #8
0
        private int calTonRating(Block block)
        {
            int         h = _game.Height;
            int         w = _game.Width;
            int         i, j;
            SquareArray underlying = _game.UnderLying;
            SquareArray array      = new SquareArray(h, w);

            Array.Copy(underlying.Storage, array.Storage, h * w);

            int rowTrans = 0;   // 各行变换次数之和
            int colTrans = 0;   // 各列变换次数之和
            int holes    = 0;   // 各列空洞个数之和
            int wells    = 0;   // 井深之和加1
            int bl       = 0;   // 当前落子消去的block数

            for (i = 0; i < h; i++)
            {
                for (j = 0; j < block.Width; j++)
                {
                    if (array[i, j + block.RPos] != null)
                    {
                        bl++;
                        array[i, j + block.RPos] = null;
                    }
                }
            }
            for (i = 0; i < h; i++)
            {
                bool pre = true;
                for (j = 0; j < w; j++)
                {
                    if (array[i, j] != null)
                    {
                        pre = false;
                        break;
                    }
                }
                if (pre)
                {
                    continue;
                }
                pre = true;
                for (j = 0; j < w; j++)
                {
                    if (pre ^ (array[i, j] != null))
                    {
                        pre = !pre;
                        rowTrans++;
                    }
                }
                if (!pre)
                {
                    rowTrans++;
                }
            }
            int[] height = new int[w + 2];
            for (i = 0; i < w; i++)
            {
                bool pre = true;
                for (j = 0; j < h; j++)
                {
                    if (pre ^ (array[j, i] != null))
                    {
                        pre = !pre;
                        colTrans++;
                    }
                }
                if (!pre)
                {
                    colTrans++;
                }
                for (j = h - 1; j >= -1; j--)
                {
                    if (j == -1)
                    {
                        break;
                    }
                    if (array[j, i] != null)
                    {
                        break;
                    }
                }
                height[i + 1] = j;
                for (j = j - 1; j >= 0; j--)
                {
                    if (array[j, i] == null)
                    {
                        holes++;
                    }
                }
            }
            height[0]     = h + 1;
            height[w + 1] = h + 1;
            for (i = 0; i < w; i++)
            {
                j = min(height[i], height[i + 2]);
                if (j > height[i + 1])
                {
                    wells += (j - height[i + 1]);
                }
            }


            return(bl - rowTrans - colTrans - 4 * holes - wells);
        }