Exemple #1
0
        public override void visualize(State s)
        {
            g.Clear(Color.White);
            if (!(s is FifteenPuzzleState))
            {
                throw new ArgumentException();
            }
            FifteenPuzzleState state = (FifteenPuzzleState)s;

            tileSize = (int)Math.Min(screen.Width / state.numbers.GetLength(0), screen.Height / state.numbers.GetLength(1));
            Font numberFont = new Font("Arial", tileSize / (int)Math.Max(state.numbers.GetLength(0), state.numbers.GetLength(1)));

            for (int i = 0; i < state.numbers.GetLength(0); i++)
            {
                for (int j = 0; j < state.numbers.GetLength(1); j++)
                {
                    if (state.numbers[i, j] == 0)
                    {
                        continue;
                    }
                    g.DrawRectangle(gridPen, (i + 1) * gapSize + i * tileSize,
                                    (j + 1) * gapSize + j * tileSize, tileSize, tileSize);
                    g.DrawString(state.numbers[i, j].ToString(), numberFont, numberBrush,
                                 (float)((i + 1) * gapSize + i * tileSize + gapSize), (float)((j + 1) * gapSize + j * tileSize + gapSize));
                }
            }
            refresh();
        }
Exemple #2
0
 public FifteenPuzzleState(FifteenPuzzleState template)
 {
     this.p = template.p;
     this.voidCoordinateX = template.voidCoordinateX;
     this.voidCoordinateY = template.voidCoordinateY;
     this.numbers         = new int[template.numbers.GetLength(0), template.numbers.GetLength(1)];
     for (int i = 0; i < template.numbers.GetLength(0); i++)
     {
         for (int j = 0; j < template.numbers.GetLength(1); j++)
         {
             this.numbers[i, j] = template.numbers[i, j];
         }
     }
 }
Exemple #3
0
        public override bool Equals(object obj)
        {
            if (!(obj is FifteenPuzzleState))
            {
                return(false);
            }
            FifteenPuzzleState s = (FifteenPuzzleState)obj;

            for (int i = 0; i < s.numbers.GetLength(0); i++)
            {
                for (int j = 0; j < s.numbers.GetLength(1); j++)
                {
                    if (this.numbers[i, j] != s.numbers[i, j])
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemple #4
0
        public override void getSuccessors(List <StateCostPair> result)
        {
            result.Clear();
            FifteenPuzzleState succ = (FifteenPuzzleState)this.clone();

            if (succ.voidCoordinateX > 0)
            {
                succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY]     = succ.numbers[succ.voidCoordinateX - 1, succ.voidCoordinateY];
                succ.numbers[succ.voidCoordinateX - 1, succ.voidCoordinateY] = 0;
                succ.voidCoordinateX--;
                result.Add(new StateCostPair(succ, 1));
                succ = (FifteenPuzzleState)this.clone();
            }
            if (succ.voidCoordinateY > 0)
            {
                succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY]     = succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY - 1];
                succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY - 1] = 0;
                succ.voidCoordinateY--;
                result.Add(new StateCostPair(succ, 1));
                succ = (FifteenPuzzleState)this.clone();
            }
            if (succ.voidCoordinateX < numbers.GetLength(0) - 1)
            {
                succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY]     = succ.numbers[succ.voidCoordinateX + 1, succ.voidCoordinateY];
                succ.numbers[succ.voidCoordinateX + 1, succ.voidCoordinateY] = 0;
                succ.voidCoordinateX++;
                result.Add(new StateCostPair(succ, 1));
                succ = (FifteenPuzzleState)this.clone();
            }
            if (succ.voidCoordinateY < numbers.GetLength(1) - 1)
            {
                succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY]     = succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY + 1];
                succ.numbers[succ.voidCoordinateX, succ.voidCoordinateY + 1] = 0;
                succ.voidCoordinateY++;
                result.Add(new StateCostPair(succ, 1));
                //succ = (FifteenPuzzleState)this.clone();
            }
        }
Exemple #5
0
        public override bool isFinal(State s)
        {
            if (!(s is FifteenPuzzleState))
            {
                return(false);
            }
            FifteenPuzzleState state = (FifteenPuzzleState)s;

            for (int i = 0; i < state.numbers.GetLength(1); i++)
            {
                for (int j = 0; j < state.numbers.GetLength(0); j++)
                {
                    if (i == state.numbers.GetLength(1) - 1 && j >= state.numbers.GetLength(1) - 3)
                    {
                        continue;
                    }
                    if (state.numbers[j, i] != i * state.numbers.GetLength(1) + j + 1)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }