Exemplo n.º 1
0
 public void CopyTo(Board b)
 {
     b.shapes.Clear();
     foreach (var s in this.shapes)
     {
         b.shapes.Add(new Shape(s.Dimensions, s.TopLeftPosition, s.IsEmpty, s.Name, s.hCode));
     }
 }
Exemplo n.º 2
0
        public List<Shape> MovableAroundEmptySpace(Point p, List<KeyValuePair<Board, int>> result, int parent)
        {
            List<Shape> temp = new List<Shape>();

            Point spaceAboveEmpty = p;
            spaceAboveEmpty.X--;

            Point spaceBelowEmpty = p;
            spaceBelowEmpty.X++;

            Point spaceLeftEmpty = p;
            spaceLeftEmpty.Y--;

            Point spaceRightEmpty = p;
            spaceRightEmpty.Y++;

            foreach (Shape s in shapes)
            {
                if (s.IsEmpty)
                    continue;

                if (s.spacesOccupied.Contains(spaceAboveEmpty) || s.spacesOccupied.Contains(spaceBelowEmpty))
                {
                    if (s.Size == 1 || (s.Size == 2 && s.IsVertical))
                    {
                        string hTemp;
                        if (!temp.Contains(s))
                        {
                            Board tempBoard = new Board();
                            if (s.spacesOccupied.Contains(spaceAboveEmpty))
                            {
                                this.CopyTo(tempBoard);
                                tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Down;
                                tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                hTemp = tempBoard.GetHuffmanCode();
                                if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                {
                                    HuffmanCode.HuffmanHistory.Add(hTemp);
                                    result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                }

                            }
                            else
                            {
                                this.CopyTo(tempBoard);
                                tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Up;
                                tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                hTemp = tempBoard.GetHuffmanCode();
                                if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                {
                                    HuffmanCode.HuffmanHistory.Add(hTemp);
                                    result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                }
                            }
                            temp.Add(s);
                        }
                    }
                }
                else if (s.spacesOccupied.Contains(spaceLeftEmpty) || s.spacesOccupied.Contains(spaceRightEmpty))
                {
                    if (s.Size == 1 || (s.Size == 2 && !s.IsVertical))
                    {
                        string hTemp;
                        if (!temp.Contains(s))
                        {
                            Board tempBoard = new Board();
                            if (s.spacesOccupied.Contains(spaceLeftEmpty))
                            {
                                this.CopyTo(tempBoard);
                                tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Right;

                                tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                hTemp = tempBoard.GetHuffmanCode();
                                if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                {
                                    HuffmanCode.HuffmanHistory.Add(hTemp);
                                    result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                }
                            }
                            else
                            {
                                this.CopyTo(tempBoard);
                                tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Left;
                                tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                hTemp = tempBoard.GetHuffmanCode();
                                if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                {
                                    HuffmanCode.HuffmanHistory.Add(hTemp);
                                    result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                }
                            }
                            temp.Add(s);
                        }
                    }
                }

            }

            return temp;
        }
Exemplo n.º 3
0
        public List<Shape> MovableDoubleBlock(Point first, Point second, List<KeyValuePair<Board, int>> result, int parent)
        {
            List<Shape> temp = new List<Shape>();

            string typeOfContiguous = ContiguousEmptySpaces(first, second);
            if (typeOfContiguous != string.Empty)
            {

                Point spaceAboveEmpty = first;
                spaceAboveEmpty.X--;

                Point spaceBelowEmpty = first;
                spaceBelowEmpty.X++;

                Point spaceLeftEmpty = first;
                spaceLeftEmpty.Y--;

                Point spaceRightEmpty = first;
                spaceRightEmpty.Y++;

                // TODO add check confirm that a double shape can move into contiguous space
                foreach (Shape s in shapes)
                {
                    if (s.IsEmpty)
                        continue;

                    if (typeOfContiguous == "H")
                    {
                        if ((s.spacesOccupied.Contains(spaceAboveEmpty) || s.spacesOccupied.Contains(spaceBelowEmpty)) &&
                    ((s.TopLeftPosition.Y + 1) - (LeastOf(shapes[8].TopLeftPosition.Y, shapes[9].TopLeftPosition.Y) + 1)) == 0)
                        {
                            if (s.Size == 4 || (s.Size == 2 && !s.IsVertical))
                            {
                                string hTemp;
                                if (!temp.Contains(s))
                                {
                                    Board tempBoard = new Board();
                                    if (s.spacesOccupied.Contains(spaceAboveEmpty))
                                    {
                                        this.CopyTo(tempBoard);
                                        tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Down;
                                        tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                        hTemp = tempBoard.GetHuffmanCode();
                                        if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                        {
                                            HuffmanCode.HuffmanHistory.Add(hTemp);
                                            result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                        }
                                    }
                                    else
                                    {
                                        this.CopyTo(tempBoard);
                                        tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Up;
                                        tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                        hTemp = tempBoard.GetHuffmanCode();
                                        if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                        {
                                            HuffmanCode.HuffmanHistory.Add(hTemp);
                                            result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                        }
                                    }

                                    temp.Add(s);
                                }
                            }
                        }
                    }

                    if (typeOfContiguous == "V")
                    {
                        if ((s.spacesOccupied.Contains(spaceLeftEmpty) || s.spacesOccupied.Contains(spaceRightEmpty))&&
                    ((s.TopLeftPosition.X+1) - (LeastOf(shapes[8].TopLeftPosition.X, shapes[9].TopLeftPosition.X)+1)) == 0)
                        {
                            if (s.Size == 4 || (s.Size == 2 && s.IsVertical))
                            {
                                string hTemp;
                                if (!temp.Contains(s))
                                {
                                    Board tempBoard = new Board();
                                    if (s.spacesOccupied.Contains(spaceLeftEmpty))
                                    {
                                        this.CopyTo(tempBoard);
                                        tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Right;
                                        tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                        hTemp = tempBoard.GetHuffmanCode();
                                        if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                        {
                                            HuffmanCode.HuffmanHistory.Add(hTemp);
                                            result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                        }
                                    }
                                    else
                                    {
                                        this.CopyTo(tempBoard);
                                        tempBoard.shapes[this.shapes.IndexOf(s)].DirectionMovable = Shape.Direction.Left;
                                        tempBoard.ShiftShapes(shapes.IndexOf(s), 8, 9);
                                        hTemp = tempBoard.GetHuffmanCode();
                                        if (!HuffmanCode.HuffmanHistory.Contains(hTemp))
                                        {
                                            HuffmanCode.HuffmanHistory.Add(hTemp);
                                            result.Add(new KeyValuePair<Board, int>(tempBoard, parent));
                                        }
                                    }

                                    temp.Add(s);
                                }
                            }
                        }
                    }

                }
            }

            return temp;
        }
Exemplo n.º 4
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Board ourBoard = new Board();
            Board currentBoard = new Board();
            List<KeyValuePair<Board, int>> result = new List<KeyValuePair<Board, int>>();

            #region original Start
            //int test = 4;

            Shape oneByTwoA = new Shape(new Point(1, 2), new Point(0, 0), false, "A", "11");
            Shape oneByTwoB = new Shape(new Point(1, 2), new Point(0, 2), false, "B", "11");
            Shape oneByTwoC = new Shape(new Point(1, 2), new Point(3, 0), false, "C", "11");
            Shape oneByTwoD = new Shape(new Point(1, 2), new Point(3, 2), false, "D", "11");
            Shape oneByOneF = new Shape(new Point(1, 1), new Point(0, 4), false, "F", "0");
            Shape oneByOneG = new Shape(new Point(1, 1), new Point(1, 3), false, "G", "0");
            Shape oneByOneH = new Shape(new Point(1, 1), new Point(2, 3), false, "H", "0");
            Shape oneByOneI = new Shape(new Point(1, 1), new Point(3, 4), false, "I", "0");
            Shape emptyOne = new Shape(new Point(1, 1), new Point(1, 4), true, "1", "101");
            Shape emptyTwo = new Shape(new Point(1, 1), new Point(2, 4), true, "2", "101");
            Shape twoByOneE = new Shape(new Point(2, 1), new Point(1, 2), false, "E", "1001");
            Shape twoByTwoJ = new Shape(new Point(2, 2), new Point(1, 0), false, "J", "1000");

            #endregion

            //Shape oneByTwoA = new Shape(new Point(1, 2), new Point(0, 0), false, "A", "11");
            //Shape oneByTwoB = new Shape(new Point(1, 2), new Point(0, 2), false, "B", "11");
            //Shape oneByTwoC = new Shape(new Point(1, 2), new Point(3, 0), false, "C", "11");
            //Shape oneByTwoD = new Shape(new Point(1, 2), new Point(3, 2), false, "D", "11");
            //Shape oneByOneF = new Shape(new Point(1, 1), new Point(1, 0), false, "F", "0");
            //Shape oneByOneG = new Shape(new Point(1, 1), new Point(2, 4), false, "G", "0");
            //Shape oneByOneH = new Shape(new Point(1, 1), new Point(2, 0), false, "H", "0");
            //Shape oneByOneI = new Shape(new Point(1, 1), new Point(3, 4), false, "I", "0");
            //Shape emptyOne = new Shape(new Point(1, 1), new Point(0, 4), true, "1", "101");
            //Shape emptyTwo = new Shape(new Point(1, 1), new Point(1, 4), true, "2", "101");
            //Shape twoByOneE = new Shape(new Point(2, 1), new Point(1, 1), false, "E", "1001");
            //Shape twoByTwoJ = new Shape(new Point(2, 2), new Point(1, 2), false, "J", "1000");

            ourBoard.shapes.Add(oneByTwoA);
            ourBoard.shapes.Add(oneByTwoB);
            ourBoard.shapes.Add(oneByTwoC);
            ourBoard.shapes.Add(oneByTwoD);
            ourBoard.shapes.Add(oneByOneF); //4
            ourBoard.shapes.Add(oneByOneG);
            ourBoard.shapes.Add(oneByOneH);
            ourBoard.shapes.Add(oneByOneI);
            ourBoard.shapes.Add(emptyOne);  //8
            ourBoard.shapes.Add(emptyTwo); //9
            ourBoard.shapes.Add(twoByOneE); //10
            ourBoard.shapes.Add(twoByTwoJ); //11

            //Test check in hahahah

            Point positionOfEmptySpace = ourBoard.FindEmptySpace(new Point(-1, -1));
            Point positionOfSecondEmptySpace = ourBoard.FindEmptySpace(positionOfEmptySpace);

            result.Add(new KeyValuePair<Board,int>(ourBoard, -1));
            ourBoard.CopyTo(currentBoard);

            //ourBoard.GetHuffmanCode();

            int i;

            for (i = 0; !currentBoard.winningPosition(currentBoard.shapes[11].TopLeftPosition.X, currentBoard.shapes[11].TopLeftPosition.Y); i++)
            {
                currentBoard.move(currentBoard.shapes[8].TopLeftPosition, currentBoard.shapes[9].TopLeftPosition, result, i);
                result[i + 1].Key.CopyTo(currentBoard);
                Console.WriteLine("{0}", i);

            }

            Console.WriteLine("Victory! {0}", i);
            //List<Board> boardList = new List<Board>();

            //for (int i = 0; i < 3; i++)
            //{
            //    Board t = new Board();

            //    ourBoard.CopyTo(t);

            //    boardList.Add(t);

            //    if (i == 0)
            //    {
            //        ourBoard.shapes[4].DirectionMovable = Shape.Direction.Down;
            //        ourBoard.ShiftShapes(4, 8, 9);
            //    }

            //    if (i == 1)
            //    {
            //        ourBoard.shapes[11].DirectionMovable = Shape.Direction.Right;
            //        ourBoard.ShiftShapes(11, 8, 9);
            //    }
            //}

            //foreach (Board b in boardList)
            //{
            //    Console.WriteLine("After Shift");

            //    foreach (Shape s in b.shapes)
            //    {
            //        Console.WriteLine("Name: {0} - Position: {1}", s.Name, s.TopLeftPosition);
            //    }
            //}

            //List<Shape> movablePieces = ourBoard.GetAllMovableShapes();
        }