Beispiel #1
0
        }     //end openButton method

        /// <summary>
        /// this method lets the user click PuzzleCell objects in the GUI and change them to black or white and also sets the color property for each PuzzleCell object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LabelClicked(object sender, EventArgs e)
        {
            PuzzleCell temp = (PuzzleCell)sender;

            if (temp.getColor() == true)
            {
                temp.ForeColor = Color.White;
                temp.BackColor = Color.Black;
                temp.setColor(false);
                resetVisited();
            }
            else
            {
                temp.ForeColor = Color.Black;
                temp.BackColor = Color.White;
                temp.setColor(true);
                resetVisited();
            }
        }//end label click event handler
Beispiel #2
0
        }//end display solution button method

        /// <summary>
        /// this recursive method determines whether or not all the white PuzzleCells are connected in the puzzle
        /// </summary>
        /// <param name="cur"></param>
        /// <returns>whether all the white PuzzleCells are connected</returns>
        private bool Connected(PuzzleCell cur)
        {
            //For each of cur's neighbors (above, below, left, and right) that are UNVISITED and WHITE:
            //Recursively call Connected with that neighbor.
            //If any of those recursive calls return true, that means that all cells were visited and that all the white cells must be connected
            //Console.WriteLine("Visited: " + cur.getRow().ToString() + " " + cur.getCol().ToString());
            cur.setVisited(true);


            bool done = false;


            if (cur.getRow() - 1 >= 0)
            {
                PuzzleCell upNeighbor = puzzle[cur.getRow() - 1, cur.getCol()];
                if (upNeighbor.getColor() == true && upNeighbor.getVistited() == false)
                {
                    done = Connected(upNeighbor);
                }
                if (done)
                {
                    return(true);
                }
            }

            if (cur.getRow() + 1 <= 4)
            {
                PuzzleCell downNeighbor = puzzle[cur.getRow() + 1, cur.getCol()];
                if (downNeighbor.getColor() == true && downNeighbor.getVistited() == false)
                {
                    done = Connected(downNeighbor);
                }
                if (done)
                {
                    return(true);
                }
            }

            if (cur.getCol() - 1 >= 0)
            {
                PuzzleCell leftNeighbor = puzzle[cur.getRow(), cur.getCol() - 1];
                if (leftNeighbor.getColor() == true && leftNeighbor.getVistited() == false)
                {
                    done = Connected(leftNeighbor);
                }
                if (done)
                {
                    return(true);
                }
            }

            if (cur.getCol() + 1 <= 4)
            {
                PuzzleCell rightNeighbor = puzzle[cur.getRow(), cur.getCol() + 1];
                if (rightNeighbor.getColor() == true && rightNeighbor.getVistited() == false)
                {
                    done = Connected(rightNeighbor);
                }
                if (done)
                {
                    return(true);
                }
            }


            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (puzzle[i, j].getColor() == true)
                    {
                        if (puzzle[i, j].getVistited() == false)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }//end of Connected method