Beispiel #1
0
        public void Clone()
        {
            Position position = new Position();

            position.PutNumberAt(1, 0, 0);
            position.PutNumberAt(2, 1, 0);
            position.PutNumberAt(3, 2, 0);

            position.PutNumberAt(3, 3, 3);
            position.PutNumberAt(4, 4, 3);
            position.PutNumberAt(5, 5, 3);

            position.PutNumberAt(6, 6, 6);
            position.PutNumberAt(7, 7, 6);
            position.PutNumberAt(8, 8, 6);

            Position newposition = position.Clone();

            Assert.AreEqual(position.Size, newposition.Size);
            Assert.AreEqual(position.Range, newposition.Range);

            for (int x = 0; x < position.Size; x++)
                for (int y = 0; y < position.Size; y++)
                    Assert.AreEqual(newposition.GetNumberAt(x, y), position.GetNumberAt(x, y));
        }
Beispiel #2
0
        public List<List<CellInfo>> GetPossibleMoves(Position position)
        {
            List<List<CellInfo>> possibles = new List<List<CellInfo>>();

            for (int number = 1; number <= position.Size; number++)
            {
                possibles.AddRange(this.GetPossibleMoves(number, position));
            }

            for (int x = 0; x < position.Size; x++)
                for (int y = 0; y < position.Size; y++)
                {
                    List<CellInfo> cells = new List<CellInfo>();

                    foreach (int number in position.GetPossibleNumbersAt(x, y))
                    {
                        cells.Add(new CellInfo() { Number = number, X = x, Y = y });
                    }

                    if (cells.Count > 0)
                        possibles.Add(cells);
                }

            return possibles;
        }
Beispiel #3
0
        public void CreatePosition()
        {
            Position position = new Position();

            Assert.AreEqual(9, position.Size);
            Assert.AreEqual(3, position.Range);
        }
Beispiel #4
0
        public List<List<CellInfo>> GetPossibleMoves(int number, Position position)
        {
            List<List<CellInfo>> results = new List<List<CellInfo>>();

            for (int x = 0; x < position.Size; x++)
            {
                List<CellInfo> cells = new List<CellInfo>();

                for (int y = 0; y < position.Size; y++)
                    if (position.CanPutNumberAt(number, x, y))
                    {
                        cells.Add(new CellInfo() { Number = number, X = x, Y = y });
                    }

                if (cells.Count > 0)
                    results.Add(cells);
            }

            for (int y = 0; y < position.Size; y++)
            {
                List<CellInfo> cells = new List<CellInfo>();

                for (int x = 0; x < position.Size; x++)
                    if (position.CanPutNumberAt(number, x, y))
                    {
                        cells.Add(new CellInfo() { Number = number, X = x, Y = y });
                    }

                if (cells.Count > 0)
                    results.Add(cells);
            }

            for (int ix = 0; ix < position.Size; ix += position.Range)
                for (int iy = 0; iy < position.Size; iy += position.Range)
                {
                    List<CellInfo> cells = new List<CellInfo>();

                    for (int x = 0; x < position.Range; x++)
                        for (int y = 0; y < position.Range; y++)
                        {
                            if (position.CanPutNumberAt(number, ix + x, iy + y))
                            {
                                cells.Add(new CellInfo() { Number = number, X = ix + x, Y = iy + y });
                            }
                        }

                    if (cells.Count > 0)
                        results.Add(cells);
                }

            return results;
        }
Beispiel #5
0
        public void CreatePositionFromEmptyString()
        {
            Position position = new Position(string.Empty);

            Assert.AreEqual(9, position.Size);
            Assert.AreEqual(3, position.Range);

            Assert.AreEqual(0, position.GetCellsWithNumbers().Count);

            var result = position.GetPossibleNumbersAt(0, 0);

            for (int k = 1; k <= 9; k++)
                Assert.IsTrue(result.Contains(k));
        }
Beispiel #6
0
        public void CreatePositionWithOneNumber()
        {
            Position position = new Position("9");

            Assert.AreEqual(9, position.Size);
            Assert.AreEqual(3, position.Range);

            var cells = position.GetCellsWithNumbers();

            Assert.AreEqual(1, cells.Count);
            Assert.AreEqual(9, cells[0].Number);
            Assert.AreEqual(0, cells[0].X);
            Assert.AreEqual(0, cells[0].Y);

            var result = position.GetPossibleNumbersAt(1, 0);

            for (int k = 1; k < 9; k++)
                Assert.IsTrue(result.Contains(k));
        }
Beispiel #7
0
        private static void DumpPosition(Position p)
        {
            for (int y = 0; y < p.Size; y++)
            {
                for (int x = 0; x < p.Size; x++)
                {
                    int number = p.GetNumberAt(x, y);

                    char cell = (char)(number + '0');

                    if (number == 0)
                        cell = '.';

                    System.Console.Write(' ');
                    System.Console.Write(cell);
                }

                System.Console.WriteLine();
            }
        }
Beispiel #8
0
        public CellInfo Resolve(int number, Position position)
        {
            for (int x = 0; x < position.Size; x++)
            {
                int npossible = 0;
                int ny = 0;

                for (int y = 0; y < position.Size; y++)
                    if (position.CanPutNumberAt(number, x, y))
                    {
                        npossible++;
                        ny = y;
                    }

                if (npossible == 1)
                    return new CellInfo() { Number = number, X = x, Y = ny };
            }

            for (int y = 0; y < position.Size; y++)
            {
                int npossible = 0;
                int nx = 0;

                for (int x = 0; x < position.Size; x++)
                    if (position.CanPutNumberAt(number, x, y))
                    {
                        npossible++;
                        nx = x;
                    }

                if (npossible == 1)
                    return new CellInfo() { Number = number, X = nx, Y = y };
            }

            for (int ix = 0; ix < position.Size; ix += position.Range)
                for (int iy = 0; iy < position.Size; iy += position.Range)
                {
                    int npossible = 0;
                    int nx = 0;
                    int ny = 0;

                    for (int x = 0; x < position.Range; x++)
                        for (int y = 0; y < position.Range; y++)
                        {
                            if (position.CanPutNumberAt(number, ix + x, iy + y))
                            {
                                npossible++;
                                nx = ix + x;
                                ny = iy + y;
                            }
                        }

                    if (npossible == 1)
                        return new CellInfo() { Number = number, X = nx, Y = ny };
                }

            return null;
        }
Beispiel #9
0
        public Position Clone()
        {
            Position position = new Position(this.size);

            Array.Copy(this.numbers, position.numbers, this.numbers.Length);
            Array.Copy(this.impossible, position.impossible, this.impossible.Length);

            return position;
        }
Beispiel #10
0
        public void ValidMoves()
        {
            Position position = new Position();

            position.PutNumberAt(5, 1, 1);

            Assert.IsFalse(position.CanPutNumberAt(5, 1, 1));

            Assert.IsTrue(position.CanPutNumberAt(4, 0, 0));
            Assert.IsTrue(position.CanPutNumberAt(4, 1, 8));
            Assert.IsTrue(position.CanPutNumberAt(4, 8, 1));

            Assert.IsTrue(position.CanPutNumberAt(4, 8, 8));
            Assert.IsTrue(position.CanPutNumberAt(4, 2, 8));
            Assert.IsTrue(position.CanPutNumberAt(4, 8, 2));
        }
Beispiel #11
0
        public void RaiseIfNumbersAreInARow()
        {
            Position position = new Position();

            position.PutNumberAt(1, 0, 0);
            position.PutNumberAt(1, 5, 0);
        }
Beispiel #12
0
        public void RaiseIfNumbersAreInAColumn()
        {
            Position position = new Position();

            position.PutNumberAt(1, 5, 5);
            position.PutNumberAt(1, 5, 2);
        }
Beispiel #13
0
        public void RaiseIfNumberIsZero()
        {
            Position position = new Position();

            position.PutNumberAt(0, 0, 0);
        }
Beispiel #14
0
        public void RaiseIfNumberIsTooLarge()
        {
            Position position = new Position();

            position.PutNumberAt(10, 0, 0);
        }
Beispiel #15
0
        public CellInfo Resolve(Position position)
        {
            CellInfo ci = null;

            for (int number = 1; number <= position.Size; number++)
            {
                ci = this.Resolve(number, position);

                if (ci != null)
                    return ci;
            }

            int n;

            for (int x = 0; x < position.Size; x++)
                for (int y = 0; y < position.Size; y++)
                    if ((n = position.GetUniqueNumberAt(x, y)) > 0)
                        return new CellInfo() { Number = n, X = x, Y = y };

            return null;
        }
Beispiel #16
0
        public void RaiseIfNumberAreInSameSquare()
        {
            Position position = new Position();

            position.PutNumberAt(1, 0, 0);
            position.PutNumberAt(1, 2, 2);
        }
Beispiel #17
0
        public void RaiseIfCellIsNotEmpty()
        {
            Position position = new Position();

            position.PutNumberAt(1, 0, 0);
            position.PutNumberAt(2, 0, 0);
        }
Beispiel #18
0
        public void PutANumber()
        {
            Position position = new Position();

            position.PutNumberAt(1, 0, 0);

            Assert.AreEqual(1, position.GetNumberAt(0, 0));
        }
Beispiel #19
0
        public void DetectColisions()
        {
            Position position = new Position();

            position.PutNumberAt(5, 1, 1);

            Assert.IsFalse(position.CanPutNumberAt(5, 1, 1));
            Assert.IsFalse(position.CanPutNumberAt(5, 0, 0));
            Assert.IsFalse(position.CanPutNumberAt(5, 1, 8));
            Assert.IsFalse(position.CanPutNumberAt(5, 8, 1));
        }
Beispiel #20
0
        public static void Main(string[] args)
        {
            string gametxt = args[0];
            Position position = new Position(gametxt);

            positions.Push(position);

            while (positions.Count > 0)
            {
                position = positions.Pop();

                DumpPosition(position);

                CellInfo ci = solver.Resolve(position);

                while (ci != null)
                {
                    System.Console.WriteLine(String.Format("{0} at {1} {2}", ci.Number, ci.X + 1, ci.Y + 1));

                    position.PutNumberAt(ci.Number, ci.X, ci.Y);
                    DumpPosition(position);

                    ci = solver.Resolve(position);
                }

                if (position.Solved)
                    break;

                List<List<CellInfo>> results = solver.GetPossibleMoves(position);

                if (results.Count == 0)
                {
                    System.Console.WriteLine("No Branch");
                    continue;
                }

                bool hasbranches = false;

                foreach (List<CellInfo> cells in results)
                {
                    if (cells.Count != 2)
                        continue;

                    foreach (CellInfo cell in cells)
                    {
                        System.Console.WriteLine(String.Format("Branch {0} at {1} {2}", cell.Number, cell.X + 1, cell.Y + 1));
                        Position newposition = position.Clone();
                        newposition.PutNumberAt(cell.Number, cell.X, cell.Y);
                        DumpPosition(newposition);
                        positions.Push(newposition);
                    }

                    hasbranches = true;

                    break;
                }

                if (!hasbranches)
                    System.Console.WriteLine("No Branch 2");
            }

            System.Console.ReadLine();
        }
Beispiel #21
0
        public void CreatePositionWithTwoNumbersAtFirstColumn()
        {
            Position position = new Position("8........9");

            Assert.AreEqual(9, position.Size);
            Assert.AreEqual(3, position.Range);

            var cells = position.GetCellsWithNumbers();

            Assert.AreEqual(2, cells.Count);
            Assert.AreEqual(8, cells[0].Number);
            Assert.AreEqual(0, cells[0].X);
            Assert.AreEqual(0, cells[0].Y);
            Assert.AreEqual(9, cells[1].Number);
            Assert.AreEqual(0, cells[1].X);
            Assert.AreEqual(1, cells[1].Y);
        }
Beispiel #22
0
        public void RaiseIfNumberIsNegative()
        {
            Position position = new Position();

            position.PutNumberAt(-1, 0, 0);
        }