Exemple #1
0
        private PuzzleState FindParent(PuzzleState parent)
        {
            bool value = true;
            PuzzleState stateValue = new PuzzleState();

            foreach (PuzzleState state in closeState)
            {
                for (int i = 0; i < 9; i++)
                {
                    if (state.allButton[i].curentBlock.Corner == parent.allButton[i].curentBlock.Corner)
                    {
                        value = true;
                    }
                    else
                    {
                        value = false;
                        break;
                    }
                }

                if (value)
                {
                    stateValue = state;
                    break;
                }
            }

            return stateValue.parent;
        }
Exemple #2
0
        private MyPuzzle SearchNext()
        {
            PuzzleState newState = new PuzzleState();
            int index = 0;

            for(int i=0; i< Open.Count ;i++)
            {
                PuzzleState old = new PuzzleState();
                old = CopyState(curentState);
                MyPuzzle bufor = new MyPuzzle();
                bool canAdd = true;

                if (first)
                {
                    closeState.Add(old);
                    first = false;
                }

                for (int j = 0; j < 9; j++)
                {
                    if (Open[i].curentBlock.Corner == Puzzle.AllCorner.LeftUP + j)
                    {
                        bufor.curentBlock.Corner = Puzzle.AllCorner.LeftUP + j;
                        bufor.curentBlock.Name = Open[i].curentBlock.Name; // zmienić w przyszłośći na nameInt
                        break;
                    }
                }

                curentPuzzle = bufor;
                MovePuzzle();
                newState = CopyState(curentState);
                newState.numberOfMove += steps;

                //if (allState.Count != 0)
                //{
                //    foreach (PuzzleState myState in allState)
                //    {
                //        if (myState == newState)
                //        {
                //            canAdd = false;
                //        }
                //    }
                //}

                if (canAdd)
                {
                    if (!IsOnAllState(newState))
                    {
                        newState.parent = old;
                        allState.Add(newState);
                    }
                }

                curentState = CopyState(old);

            }

            foreach (PuzzleState state in allState)
            {
                CalculateManhatanState(state);
                state.f = state.numberOfMove + state.allManhatan;
            }

            for (int i = 0; i < allState.Count; i++)
            {
                if (allState[index].f > allState[i].f)
                {
                    index = i;
                }
            }

            curentState = CopyState(allState[index]);

            closeState.Add(allState[index]);
            allState.Remove(allState[index]);
            Open.Clear();

            return closeState[closeState.Count-1].allButton[8];
        }
Exemple #3
0
        private bool CheckBoolState(PuzzleState myState, List<PuzzleState> ListState)
        {
            bool value = false;
            bool next = false;

            foreach (PuzzleState state in ListState)
            {

                if (state.allButton[8].curentBlock.Corner != myState.allButton[8].curentBlock.Corner)
                {
                    value = false;
                    next = false;
                }
                else
                {
                    foreach (MyPuzzle puzzle in state.allButton)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            if (myState.allButton[i].curentBlock.IntName == puzzle.curentBlock.IntName)
                            {
                                if (myState.allButton[i].curentBlock.Corner != puzzle.curentBlock.Corner)
                                {
                                    value = false;
                                    next = false;
                                    break;
                                }
                                else
                                {
                                    value = true;
                                    next = false;
                                }
                            }
                            else
                            {
                                next = true;
                            }
                        }

                        if (!next)
                        {
                            if (value)
                            {
                                return true;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

            }

            return false;
        }
Exemple #4
0
        private PuzzleState CopyState(PuzzleState myState)
        {
            PuzzleState buforState = new PuzzleState();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (myState.allButton[i].curentBlock.Corner == Puzzle.AllCorner.LeftUP + j)
                    {
                        MyPuzzle bufor = new MyPuzzle();
                        bufor.curentBlock.Name = myState.allButton[i].curentBlock.Name;
                        bufor.curentBlock.IntName = myState.allButton[i].curentBlock.IntName;
                        bufor.curentBlock.Corner = Puzzle.AllCorner.LeftUP + j;
                        buforState.allButton.Add(bufor);
                    }
                }
            }

            return buforState;
        }
Exemple #5
0
        private void CalculateManhatanState(PuzzleState buforState)
        {
            CalculateLocation(buforState);
            CalculateLocation(endState);

            foreach (MyPuzzle state in buforState.allButton)
            {
                CalculateManhatan(state);
            }

            buforState.CalculateAllMoves();
        }
Exemple #6
0
        private void CalculateLocation(PuzzleState state)
        {
            for(int i=0; i < 9; i++)
            {
             //   foreach (MyPuzzle myPuzzle in state.allButton)

                #region Up
                ///////////////////////////////Up//////////////////////////////////
                if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.LeftUP)
                {
                    state.allButton[i].curentBlock.Location = map[0][0];
                    break;
                }

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.MiddleUp)
                {
                    state.allButton[i].curentBlock.Location = map[0][1];
                    break;
                }

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.RightUp)
                {
                    state.allButton[i].curentBlock.Location = map[0][2];
                    break;
                }

                #endregion

                #region Middle
                ////////////////////////////Middle/////////////////////
                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.LeftMiddle)
                {
                    state.allButton[i].curentBlock.Location = map[1][0];
                    break;
                }

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.MiddleMiddle)
                {
                    state.allButton[i].curentBlock.Location = map[1][1];
                    break;
                }

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.RightMiddle)
                {
                    state.allButton[i].curentBlock.Location = map[1][2];
                    break;
                }

                #endregion

                #region Down
                ////////////////// Down ///////////////////////////////

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.LeftDown)
                {
                    state.allButton[i].curentBlock.Location = map[2][0];
                    break;
                }

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.MiddleDown)
                {
                    state.allButton[i].curentBlock.Location = map[2][1];
                    break;
                }

                else if (state.allButton[i].curentBlock.Corner == Puzzle.AllCorner.RightDown)
                {
                    state.allButton[i].curentBlock.Location = map[2][2];
                    break;
                }

                #endregion

            }
        }
Exemple #7
0
        public string ReturnSolve()
        {
            bool calculate = true;
            Puzzle puzzle = new Puzzle();
            PuzzleState parent = new PuzzleState();

            List<PuzzleState> finish = new List<PuzzleState>();

            finish.Add(closeState[closeState.Count - 1]);

            parent = closeState[closeState.Count - 1].parent;
            finish.Add(parent);

            while (calculate)
            {
                parent = FindParent(parent);
                finish.Add(parent);
                calculate = CheckStatus(parent, closeState[0]);
            }

            //foreach (PuzzleState state in finish)
            //{
            //    foreach (MyPuzzle myPuzzle in state.allButton)
            //    {
            //        if (myPuzzle.curentBlock.Name == "button9")
            //        {
            //            numberStep++;
            //            nextStep += numberStep.ToString() + " - " + myPuzzle.curentBlock.Corner + "\r\n";
            //        }
            //    }
            //}
            for (int i = finish.Count - 1; i != -1; i--)
            {
                for (int j = 8; j != -1; j--)
                {
                    numberStep++;
                    nextStep += numberStep.ToString() + " - " + finish[i].allButton[j].curentBlock.Corner + "\r\n";
                    break;
                }
            }

            return nextStep;
        }
Exemple #8
0
        public bool IsOnAllState(PuzzleState myState)
        {
            PuzzleState value = new PuzzleState();
            bool valueCloseState = true;
            bool valueAllState = true;

            if (allState.Count == 0)
            {
                return false;
            }

            valueAllState = CheckBoolState(myState, allState);

            if (!valueAllState)
            {

                valueCloseState = CheckBoolState(myState, closeState);
            }

            if (valueAllState || valueCloseState)
            {
                if (myState.f > value.f)
                {
                    myState.f = value.f;
                    myState.parent = value.parent;
                }
                return true;
            }

            return false;
        }
Exemple #9
0
        public bool CheckStatus(PuzzleState curentState, PuzzleState endState)
        {
            bool calculate = false;

            for (int i = 0; i < curentState.allButton.Count; i++)
            {
                if (curentState.allButton[i].curentBlock.Corner != endState.allButton[i].curentBlock.Corner)
                {
                    calculate = true;
                    break;
                }
                else
                {
                    calculate = false;
                }
            }

            return calculate;
        }