示例#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
示例#2
0
        public Form1()
        {
            InitializeComponent();
            int yCoorCounter = 5;

            for (int i = 0; i < 5; i++)
            {
                int xCoorCounter = 60;
                yCoorCounter += 60;
                for (int j = 0; j < 5; j++)
                {
                    PuzzleCell temp = new PuzzleCell(0, 0, 0);
                    temp.Location  = new Point(xCoorCounter, yCoorCounter);
                    temp.Size      = new Size(50, 50);
                    temp.BackColor = Color.White;
                    temp.Click    += new EventHandler(LabelClicked);
                    puzzle[i, j]   = temp;
                    Controls.Add(puzzle[i, j]);
                    xCoorCounter += 60;
                }
            }
        }
示例#3
0
        }//end solve puzzle method

        /// <summary>
        /// this method checks for adjacent black cells and duplicate numbers in columns and rows
        /// </summary>
        /// <returns>whether or not a solution is correct </returns>
        public bool checkSolution()
        {
            if (puzzleLoaded == false)
            {
                MessageBox.Show("Puzzle not loaded");
                return(false);
            }//end check to see if puzzle was loaded

            resetVisited();

            for (int i = 0; i < 5; i++)
            {
                int num1 = puzzle[0, i].getNum();
                int num2 = puzzle[1, i].getNum();
                int num3 = puzzle[2, i].getNum();
                int num4 = puzzle[3, i].getNum();
                int num5 = puzzle[4, i].getNum();

                if (puzzle[0, i].getColor() == false)
                {
                    num1 = -1;
                }
                if (puzzle[1, i].getColor() == false)
                {
                    num2 = -2;
                }
                if (puzzle[2, i].getColor() == false)
                {
                    num3 = -3;
                }
                if (puzzle[3, i].getColor() == false)
                {
                    num4 = -4;
                }
                if (puzzle[4, i].getColor() == false)
                {
                    num5 = -5;
                }

                if (num1 == num2 || num1 == num3 || num1 == num4 || num2 == num3 || num2 == num4 || num3 == num4 || num1 == num5 || num5 == num2 || num3 == num5 || num4 == num5)
                {
                    return(false);
                }
            }//checking for duplicates in columns

            for (int i = 0; i < 5; i++)
            {
                int num1 = puzzle[i, 0].getNum();
                int num2 = puzzle[i, 1].getNum();
                int num3 = puzzle[i, 2].getNum();
                int num4 = puzzle[i, 3].getNum();
                int num5 = puzzle[i, 4].getNum();

                if (puzzle[i, 0].getColor() == false)
                {
                    num1 = -1;
                }
                if (puzzle[i, 1].getColor() == false)
                {
                    num2 = -2;
                }
                if (puzzle[i, 2].getColor() == false)
                {
                    num3 = -3;
                }
                if (puzzle[i, 3].getColor() == false)
                {
                    num4 = -4;
                }
                if (puzzle[i, 4].getColor() == false)
                {
                    num5 = -5;
                }

                if (num1 == num2 || num1 == num3 || num1 == num4 || num2 == num3 || num2 == num4 || num3 == num4 || num1 == num5 || num5 == num2 || num3 == num5 || num4 == num5)
                {
                    return(false);
                }
            }//checking for duplicates in rows

            //ensure that no two black cells are adjacent horizontally or vertically
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (puzzle[i, j].getColor() == false)
                    {
                        int left  = j - 1;
                        int right = j + 1;
                        int up    = i - 1;
                        int down  = i + 1;

                        if (left >= 0)
                        {
                            if (puzzle[i, left].getColor() == false)
                            {
                                return(false);
                            }
                        }
                        if (right <= 4)
                        {
                            if (puzzle[i, right].getColor() == false)
                            {
                                return(false);
                            }
                        }

                        if (up >= 0)
                        {
                            if (puzzle[up, j].getColor() == false)
                            {
                                return(false);
                            }
                        }
                        if (down <= 4)
                        {
                            if (puzzle[down, j].getColor() == false)
                            {
                                return(false);
                            }
                        }
                    } //end checking for adjacent black cells
                }     //end inner for loop
            }         //end outer for loop
            PuzzleCell tempo = null;

            for (int h = 0; h < 5; h++)
            {
                for (int w = 0; w < 5; w++)
                {
                    if (puzzle[h, w].getColor() == true)
                    {
                        tempo = puzzle[h, w];
                        break;
                    }
                }
                if (tempo != null)
                {
                    break;
                }
            }

            if (Connected(tempo))
            {
                return(true);
            }

            /*for (int h = 0; h < 5; h++)
             * {
             *  for (int w = 0; w < 5; w++)
             *  {
             *      if (puzzle[h, w].getColor() == true)
             *      {
             *          tempo = puzzle[h, w];
             *          if (puzzle[h, w].getVistited() == false)
             *          {
             *              Console.WriteLine("" + h + " " + w);
             *          }
             *      }
             *
             *  }
             *
             * }*/

            return(false);
        }//end check solution method
示例#4
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
示例#5
0
        }//end label click event handler

        /// <summary>
        /// this method controls when the user clicks on the display solution button. This method calls the check solution method and adds objects into the allMoves List.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxDisplaySolutionButton_Click(object sender, EventArgs e)
        {
            if (puzzleLoaded == false)
            {
                MessageBox.Show("First please load a puzzle.");
                return;
            }


            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    puzzle[i, j].BackColor = Color.White;
                    puzzle[i, j].ForeColor = Color.Black;
                    puzzle[i, j].setColor(true);
                    puzzle[i, j].setVisited(false);
                }
            }


            List <PuzzleCell> allMoves = new List <PuzzleCell>();

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    PuzzleCell temp = puzzle[i, j];
                    for (int h = 0; h < 5; h++)
                    {
                        if (puzzle[i, h].getNum() == temp.getNum() && h != j)
                        {
                            allMoves.Add(temp);
                            break;
                        }
                        else if (puzzle[h, j].getNum() == temp.getNum() && h != i)
                        {
                            allMoves.Add(temp);
                            break;
                        }
                    }
                }
            }

            Console.WriteLine("done finding moves");

            /*int[,] nums = new int[5, 5];
             *
             *
             * for (int i = 0; i < 5; i++) {
             *  for (int j = 0; j < 5; j++) {
             *      nums[i, j] = puzzle[i, j].getNum();
             *  }
             * }
             *
             * for (int i = 0; i < 5; i++) {
             *  if (nums[i, 0] == nums[i, 1]) {
             *      allMoves.Add(puzzle[i, 0]);
             *      allMoves.Add(puzzle[i, 1]);
             *  }
             *  if (nums[i, 1] == nums[i, 2])
             *  {
             *      allMoves.Add(puzzle[i, 1]);
             *      allMoves.Add(puzzle[i, 2]);
             *  }
             *  if (nums[i, 2] == nums[i, 3])
             *  {
             *      allMoves.Add(puzzle[i, 2]);
             *      allMoves.Add(puzzle[i, 3]);
             *  }
             *  if (nums[i, 3] == nums[i, 4])
             *  {
             *      allMoves.Add(puzzle[i, 3]);
             *      allMoves.Add(puzzle[i, 4]);
             *  }
             *  if (nums[i, 1] == nums[i, 3])
             *  {
             *      allMoves.Add(puzzle[i, 3]);
             *      allMoves.Add(puzzle[i, 1]);
             *  }
             *  if (nums[i, 1] == nums[i, 4])
             *  {
             *      allMoves.Add(puzzle[i, 4]);
             *      allMoves.Add(puzzle[i, 1]);
             *  }
             *  if (nums[i, 2] == nums[i, 4])
             *  {
             *      allMoves.Add(puzzle[i, 2]);
             *      allMoves.Add(puzzle[i, 4]);
             *  }
             *
             * }
             *
             * for (int i = 0; i < 5; i++)
             * {
             *  if (nums[0, i] == nums[1, i])
             *  {
             *      allMoves.Add(puzzle[0, i]);
             *      allMoves.Add(puzzle[1, i]);
             *  }
             *  if (nums[1, i] == nums[2, i])
             *  {
             *      allMoves.Add(puzzle[1, i]);
             *      allMoves.Add(puzzle[2, i]);
             *  }
             *  if (nums[2, i] == nums[3, i])
             *  {
             *      allMoves.Add(puzzle[2, i]);
             *      allMoves.Add(puzzle[3, i]);
             *  }
             *  if (nums[3, i] == nums[4, i])
             *  {
             *      allMoves.Add(puzzle[3, i]);
             *      allMoves.Add(puzzle[4, i]);
             *  }
             *  if (nums[1, i] == nums[3, i])
             *  {
             *      allMoves.Add(puzzle[3, i]);
             *      allMoves.Add(puzzle[1, i]);
             *  }
             *  if (nums[1, i] == nums[4, i])
             *  {
             *      allMoves.Add(puzzle[4, i]);
             *      allMoves.Add(puzzle[1, i]);
             *  }
             *  if (nums[2, i] == nums[4, i])
             *  {
             *      allMoves.Add(puzzle[2, i]);
             *      allMoves.Add(puzzle[4, i]);
             *  }
             *
             * }*/



            if (SolvePuzzle(allMoves, 0) == false)
            {
                MessageBox.Show("No solution");
            }
            else
            {
                MessageBox.Show("done");
            }
        }//end display solution button method