Ejemplo n.º 1
0
 public override int GetHashCode()
 {
     return(board?.GetHashCode() ?? 0);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 执行移动指令
        /// </summary>
        /// <param name="direction">需要移动的方向</param>
        /// <returns>本次移动的得分,-1表示无法移动</returns>
        public int Move(Direction direction)
        {
            isMoving = true;
            Hashtable actions    = new Hashtable();
            int       totalScore = 0;
            bool      isMoved    = false;

            int[,] oldValue = BoardValue();
            int boardBackup = board.GetHashCode();

            for (int i = 0; i < SIZE; i++)
            {
                int[] row = new int[SIZE];
                for (int j = 0; j < SIZE; j++)
                {
                    Tile tmpTile;
                    if (direction == Direction.Up)
                    {
                        tmpTile = board[j, i];
                    }
                    else if (direction == Direction.Down)
                    {
                        tmpTile = board[SIZE - j - 1, i];
                    }
                    else if (direction == Direction.Left)
                    {
                        tmpTile = board[i, j];
                    }
                    else if (direction == Direction.Right)
                    {
                        tmpTile = board[i, SIZE - j - 1];
                    }
                    else
                    {
                        throw new ArgumentException();
                    }

                    row[j] = tmpTile == null ? 0 : tmpTile.index;
                }

                // 设置每条直线的起点和终点
                int begin, end;
                if (direction == Direction.Up)
                {
                    begin = i;
                    end   = i + SIZE * SIZE;
                }
                else if (direction == Direction.Down)
                {
                    begin = i + (SIZE - 1) * SIZE;
                    end   = i - SIZE;
                }
                else if (direction == Direction.Left)
                {
                    begin = i * SIZE;
                    end   = i * SIZE + SIZE;
                }
                else if (direction == Direction.Right)
                {
                    begin = (i + 1) * SIZE - 1;
                    end   = i * SIZE - 1;
                }
                else
                {
                    throw new ArgumentException();
                }

                int rowScore;
                actions     = MergeActionsTable(CombineLine(begin, end, row, out rowScore), actions);
                totalScore += rowScore;
            }

            PlayActions(actions);

            int[,] newValue = BoardValue();
            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    if (oldValue[i, j] != newValue[i, j])
                    {
                        isMoved = true;
                    }
                }
            }

            return(isMoved ? totalScore : -1);
        }