示例#1
0
        public static ArrayList getValidMoves(State currentState)
        {
            int[,] board = currentState.getState();
            ArrayList available = new ArrayList();

            bool found = false;
            for (int i = 0; i < Game.LENGTH; i++)
            {
                for (int j = 0; j < Game.LENGTH; j++)
                {
                    if (board[i, j] == 0)
                    {
                        if (i > 0)
                            available.Add(moves[0]);
                        if (j > 0)
                            available.Add(moves[1]);
                        if (j < Game.LENGTH - 1)
                            available.Add(moves[2]);
                        if (i < Game.LENGTH - 1)
                            available.Add(moves[3]);

                        found = true;
                        break;
                    }
                }

                if (found)
                    break;
            }
            return available;
        }
示例#2
0
        public static State moveBlank(State currentState, string action)
        {
            State newState = new State(currentState);
            if (!moves.Contains(action))
                return newState;

            int[,] board = newState.getState();
            if (getValidMoves(currentState).Contains(action))
            {
                int[] blankLocation = new int[2];
                bool found = false;
                for (int i = 0; i < Game.LENGTH; i++)
                {
                    for (int j = 0; j < Game.LENGTH; j++)
                    {
                        if (board[i, j] == 0)
                        {
                            blankLocation[0] = i;
                            blankLocation[1] = j;
                            found = true;
                            break;
                        }
                    }

                    if (found)
                        break;
                }

                int temp;
                switch (action)
                {
                    case "up":
                        temp = board[blankLocation[0], blankLocation[1]];
                        board[blankLocation[0], blankLocation[1]] = board[blankLocation[0] - 1, blankLocation[1]];
                        board[blankLocation[0] - 1, blankLocation[1]] = temp;
                        break;
                    case "left":
                        temp = board[blankLocation[0], blankLocation[1]];
                        board[blankLocation[0], blankLocation[1]] = board[blankLocation[0], blankLocation[1] - 1];
                        board[blankLocation[0], blankLocation[1] - 1] = temp;
                        break;
                    case "right":
                        temp = board[blankLocation[0], blankLocation[1]];
                        board[blankLocation[0], blankLocation[1]] = board[blankLocation[0], blankLocation[1] + 1];
                        board[blankLocation[0], blankLocation[1] + 1] = temp;
                        break;
                    case "down":
                        temp = board[blankLocation[0], blankLocation[1]];
                        board[blankLocation[0], blankLocation[1]] = board[blankLocation[0] + 1, blankLocation[1]];
                        board[blankLocation[0] + 1, blankLocation[1]] = temp;
                        break;
                }
            }
            return newState;
        }
示例#3
0
文件: State.cs 项目: collaj/15Puzzle
 public State(State aState)
 {
     int[,] aStateBoard = aState.getState();
     int length = aStateBoard.GetLength(0);
     this.state = new int[length, length];
     for (int i = 0; i < length; i++)
     {
         for (int j = 0; j < length; j++)
         {
             this.state[i, j] = aStateBoard[i, j];
         }
     }
 }
示例#4
0
文件: Game.cs 项目: collaj/15Puzzle
        public static void newGame()
        {
            Random rand = new Random();
            bool solvable;
            int[,] board = new int[LENGTH, LENGTH];

            do
            {
                for (int i = 0; i < 5; i++)
                {
                    ArrayList usedTiles = new ArrayList();
                    for (int j = 0; j < Game.LENGTH * Game.LENGTH; j++)
                    {
                        int random;
                        do
                        {
                            random = rand.Next(Game.LENGTH * Game.LENGTH);
                        } while (usedTiles.Contains(random));
                        usedTiles.Add(random);
                        board[j % Game.LENGTH, j / Game.LENGTH] = random; // this will build columns first, not rows....cause why not
                    }
                }

                solvable = isSolvable(board);
            } while (!solvable);

            currentState = new State(board);
            gameOn = true;
        }
示例#5
0
文件: Game.cs 项目: collaj/15Puzzle
 public static void setCurrentState(State state)
 {
     currentState = state;
 }