Esempio n. 1
0
        /// <summary>
        /// Randomizes the Squares in the board according to the number of Squares
        /// </summary>
        private void randomizeSquares(int randomMovesCap)
        {
            //Create and display the progress bar
            generateProgressBar(randomMovesCap);

            //Track the number of random moves performed
            int randomMoves = 0;

            //Create a new Random generator to move the squares randomly
            Random random = new Random();

            //Perform random moves until the number of random moves is equal to the random moves cap
            while (randomMoves < randomMovesCap)
            {
                try
                {
                    //Get a randomized direction
                    Direction randomDirection = (Direction)random.Next(0, 4);

                    //Perform a random move in the corresponding direction
                    switch (randomDirection)
                    {
                    case Direction.Up:
                        Square.moveSquare(Square.SquaresArray[Square.OpenX, Square.OpenY - 1]);
                        break;

                    case Direction.Right:
                        Square.moveSquare(Square.SquaresArray[Square.OpenX - 1, Square.OpenY]);
                        break;

                    case Direction.Down:
                        Square.moveSquare(Square.SquaresArray[Square.OpenX, Square.OpenY + 1]);
                        break;

                    case Direction.Left:
                        Square.moveSquare(Square.SquaresArray[Square.OpenX + 1, Square.OpenY]);
                        break;

                    default:
                        //Throw an exception if the random direction is invalid
                        throw new Exception("Invalid Square");
                    }
                }
                catch (Exception)
                {
                    //If the selected square is not valid, continue with the next iteration of the loop
                    continue;
                }

                //Increment the random moves counter and the progress bar
                ++randomMoves;
                proGenerateProgress.PerformStep();
                proGenerateProgress.Update();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Click handler for each square
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Square_Click(object sender, EventArgs e)
        {
            //Check if the clicked Square has a valid move
            Direction moveDirection = Square.determineValidMove((Square)sender);

            //If there are no valid moves, exit the method
            if (moveDirection == Direction.None)
            {
                return;
            }

            //Move the clicked Square
            Square.moveSquare((Square)sender);

            //Update the game board
            updateBoard();

            //Increment the move counter
            lblMoveCount.Text = (++Square.MoveCounter).ToString();

            //The game has no longer been "just saved"
            justSaved = false;

            //Check win
            if (checkWin() == true)
            {
                //Prompt the user to choose if they wish to play again
                DialogResult newGame = MessageBox.Show(
                    "You have won in " + Square.MoveCounter + " moves!\nDo you want to play again?",
                    "New Game?",
                    MessageBoxButtons.YesNo);

                switch (newGame)
                {
                //If the user wants to play again, display the main menu
                case DialogResult.Yes:
                    initializeGameMenu();
                    break;

                //If the user doesn't want to play again, exit the game
                case DialogResult.No:
                    this.Close();
                    break;

                default:
                    break;
                }
            }
        }