Example #1
0
        /// <summary>
        /// generate the buttons and scramble if needed
        /// </summary>
        /// <param name="sender">useless</param>
        /// <param name="e">useless</param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            //More var initialized
            int buttonRow = ADJUST;
            int buttonColumn = 0;
            //If player is loading the game. Grab the rows and columns from gameFile.
            if (!isLoadGame)
            {
                numberOfRows = int.Parse(txtRow.Text);
                numberOfColumns = int.Parse(txtColumn.Text);
            }
            string buttonText = "";
            int buttonIndex = 1;
            bool breakOut = false;
            //hide buttons and labels. Disable them too incase of witchcraft.
            btnGenerate.Visible = false;
            txtColumn.Visible = false;
            txtRow.Visible = false;
            btnGenerate.Enabled = false;
            txtColumn.Enabled = false;
            txtRow.Enabled = false;
            lblColumn.Visible = false;
            lblRow.Visible = false;

            //make the buttons!
            for (int i = 0; i < numberOfRows; i++)
            {
                for (int j = 0; j < numberOfColumns; j++)
                {
                    //if it's the last piece. Don't make it and assign the empty space the position.
                    if (i == numberOfRows-1 && j == numberOfColumns-1)
                    {
                        breakOut = true;
                        break;
                    }
                    //new piece type for each puzzle piece.
                    Piece piece = new Piece(this, buttonIndex, buttonColumn, buttonRow, buttonText);
                    piece.Left = buttonColumn;
                    piece.Top = buttonRow;
                    int n = i * numberOfColumns + j + 1;
                    piece.Text = n.ToString();
                    piece.ButtonIndex = buttonIndex;
                    buttonIndex++;
                    piece.Width = COLUMN;
                    piece.Height = ROW;
                    piece.Click += piece_Click;

                    //go over a column
                    buttonColumn += COLUMN;

                    //add button to form
                    this.Controls.Add(piece);
                    //add button to setup list
                    Piece.setup.Add(piece);

                }
                //if it's the last piece. Don't make it and assign the empty space the position.
                if (breakOut)
                {
                    Piece.emptyColumn = buttonColumn;
                    Piece.emptyRow = buttonRow;
                    break;
                }
                //go down a row
                buttonRow += ROW;
                //reset column back to 1
                buttonColumn = 0;
            }
            foreach (Piece button in Piece.setup)
            {
                winningString += button.ButtonColumn.ToString() + button.ButtonRow.ToString();
            }
            if (!isLoadGame)
            {
                Scramble();
            }
            isLoadGame = false;
        }
Example #2
0
 /// <summary>
 /// Main method to move the pieces
 /// </summary>
 /// <param name="button">whatever Control selected is passed as 'button'</param>
 /// <param name="randomNumber">if scrambled is true, pass a random number from scramble method</param>
 private void MovePiece(Piece button, int randomNumber)
 {
     //TOP OF EMPTY SPACE
     if ((((Piece.emptyColumn - button.ButtonColumn == 0) && (Piece.emptyRow - button.ButtonRow == ROW)) && randomNumber == 0) || (((Piece.emptyColumn - button.ButtonColumn == 0) && (Piece.emptyRow - button.ButtonRow == ROW)) && randomNumber == 1))
     {
         //If it's NOT solved. Add button to move list.
         if (!solved)
         {
             Piece.moves.Add(button);
         }
         //update button and empty space position
         Piece.emptyRow = button.ButtonRow;
         button.ButtonRow = button.Top += ROW;
     }
     //LEFT OF EMPTY SPACE
     else if ((((Piece.emptyColumn - button.ButtonColumn == COLUMN) && (Piece.emptyRow - button.ButtonRow == 0)) && randomNumber == 0) || (((Piece.emptyColumn - button.ButtonColumn == COLUMN) && (Piece.emptyRow - button.ButtonRow == 0)) && randomNumber == 2))
     {
         //If it's NOT solved. Add button to move list.
         if (!solved)
         {
             Piece.moves.Add(button);
         }
         //update button and empty space position
         Piece.emptyColumn = button.ButtonColumn;
         button.ButtonColumn = button.Left += COLUMN;
     }
     //BOTTOM OF EMPTY SPACE
     else if ((((button.ButtonColumn - Piece.emptyColumn == 0) && (button.ButtonRow - Piece.emptyRow == ROW)) && randomNumber == 0) || (((button.ButtonColumn - Piece.emptyColumn == 0) && (button.ButtonRow - Piece.emptyRow == ROW)) && randomNumber == 3))
     {
         //If it's NOT solved. Add button to move list.
         if (!solved)
         {
             Piece.moves.Add(button);
         }
         //update button and empty space position
         Piece.emptyRow = button.ButtonRow;
         button.ButtonRow = button.Top -= ROW;
     }
     //RIGHT OF EMPTY SPACE
     else if ((((button.ButtonColumn - Piece.emptyColumn == COLUMN) && (button.ButtonRow - Piece.emptyRow == 0)) && randomNumber == 0) || (((button.ButtonColumn - Piece.emptyColumn == COLUMN) && (button.ButtonRow - Piece.emptyRow == 0)) && randomNumber == 4))
     {
         //If it's NOT solved. Add button to move list.
         if (!solved)
         {
             Piece.moves.Add(button);
         }
         //update button and empty space position
         Piece.emptyColumn = button.ButtonColumn;
         button.ButtonColumn = button.Left -= COLUMN;
     }
     //If scramble don't check for win
     if (!scrambled)
     {
         //If new game is selected, don't check for win
         if (!newGame)
         {
             CheckWin();
         }
     }
 }