求助:我有个数字游戏的代码,怎么把其中指定的数字变为随机数,我发原代码,请高手

作者在 2008-06-14 19:14:00 发布以下内容
using System;
namespace puzzle
{
    class MoveGridPuzzle
    {
        int[,] Grid = new int[3, 3];
        string _ValidMove;
        int CurRow, CurCol;
        public MoveGridPuzzle()
        {
            Reset();
        }
        public void Reset()//如何把这个数组里面的数字变为一些随机的数,请高手帮我修改一下!
        {
            Grid[0, 0] = 0; Grid[0, 1] = 5; Grid[0, 2] = 8;
            Grid[1, 0] = 7; Grid[1, 1] = 4; Grid[1, 2] = 3;
            Grid[2, 0] = 2; Grid[2, 1] = 6; Grid[2, 2] = 1;
            CurRow = 0; CurCol = 0;
            SetValidMove();
        }
        public bool IsWon()
        {
            for (int i = Grid.GetLowerBound(0); i < Grid.GetUpperBound(0); i++)
            {
                for (int j = Grid.GetLowerBound(1); j &lt;= Grid.GetUpperBound(1); j++)
                {
                    if (i == Grid.GetUpperBound(0) && j == Grid.GetUpperBound(1))
                    {
                        return true;
                    }
                    else
                    {
                        if (Grid[i, j] != (i * 3) + j + 1)
                            return false;
                    }
                }
            }
            return true;
        }
        bool IsMoveValid(string Move)
        {
            return (ValidMove.IndexOf(Move)) >= 0 ? true : false;
        }
        private string ValidMove
        {
            get { return _ValidMove; }
            set { _ValidMove = value; }
        }
        void SetValidMove()
        {
            string Move;
            switch (CurRow)
            {
                case 0:
                    Move = "D";
                    break;
                case 1:
                case 2:
                    Move = "UD";
                    break;
                default:
                    Move = "";
                    break;
            }
            switch (CurCol)
            {
                case 0:
                    Move += "R";
                    break;
                case 1:
                    Move += "LR";
                    break;
                case 2:
                    Move += "L";
                    break;
                default:
                    Move = "";
                    break;
            }
            ValidMove = Move;
        }
        private int this[int row, int col]
        {
            get { return Grid[row, col]; }
            set { Grid[row, col] = value; }
        }
        private void Play()
        {
            bool IsNotGameDone = true;
            string MoveTo;
            do
            {
                ShowGrid();
                Console.Write("Enter your Move [L,R,U,D,E]: ");
                MoveTo = Console.ReadLine().ToUpper();
                if (IsWon())
                {
                    Console.WriteLine("You Won the Game!!");
                    MoveTo = "E";
                }
                if (IsMoveValid(MoveTo))
                {
                    switch (MoveTo)
                    {
                        case "L":
                        case "l":
                            SwapPosition(CurRow, CurCol, CurRow, CurCol - 1);
                            break;
                        case "R":
                        case "r":
                            SwapPosition(CurRow, CurCol, CurRow, CurCol + 1);
                            break;
                        case "U":
                        case "u":
                            SwapPosition(CurRow, CurCol, CurRow - 1, CurCol);
                            break;
                        case "D":
                        case "d":
                            SwapPosition(CurRow, CurCol, CurRow + 1, CurCol);
                            break;
                    }
                    SetValidMove();
                }
                else
                {
                    switch (MoveTo)
                    {
                        case "E":
                        case "e":
                            IsNotGameDone = false;
                            if (!IsWon())
                                Console.WriteLine("You Lose the Game");
                            break;
                        default:
                            Console.WriteLine("Invalid Move or Choice");
                            break;
                    }
                }
            } while (IsNotGameDone);
        }
        void ShowGrid()
        {
            Console.WriteLine("+===+===+===+");
            for (int i = Grid.GetLowerBound(0); i &lt;= Grid.GetUpperBound(0); i++)
            {
                Console.Write("+");
                for (int j = Grid.GetLowerBound(1); j &lt;= Grid.GetUpperBound(1); j++)
                    Console.Write(" {0} +", this[i, j]);
                Console.WriteLine();
            }
            Console.WriteLine("+===+===+===+");
        }
        void SwapPosition(int FromRow, int FromCol, int ToRow, int ToCol)
        {
            int Temp;
            Temp = this[FromRow, FromCol];
            this[FromRow, FromCol] = this[ToRow, ToCol];
            this[ToRow, ToCol] = Temp;
            CurRow = ToRow;
            CurCol = ToCol;
        }
        public static void Main()
        {
            MoveGridPuzzle GameObject = new MoveGridPuzzle();
            GameObject.Play();
            Console.ReadLine();
        }
    }
}
默认分类 | 阅读 5624 次
文章评论,共2条
ntdom
2008-06-15 10:24
1
使用随机数函数可以完成<br />
比如:<br />
stand(time(null));<br />
i=rand%4;<br />
<br />
i为0~3
lone(作者)
2008-06-15 11:23
2
能不能具体一点,这样我不大明白,谢啦!
游客请输入验证码
浏览10598次
文章分类
文章归档