Exemple #1
0
        static void Main(string[] args)
        {
            var game  = new Game();
            var robot = new Robot();

            Console.WriteLine("Import a game file? (Y/N)");
            var importFile = Console.ReadLine().ToLower().Equals("y");

            if (importFile)
            {
                Console.WriteLine("Give the file path!!");
                var filePath = Console.ReadLine();

                game.ImportGameFile(filePath);
            }

            game.Display();

            while (!game.IsComplete())
            {
                var result = false;

                while (!result)
                {
                    var userInput = Console.ReadLine().Split(" ").Select(word => int.Parse(word)).ToList();
                    var(line, column, value) = (userInput[0], userInput[1], userInput[2]);
                    result = game.SetAndValidate(line, column, value);
                    if (!result)
                    {
                        Console.WriteLine("Try again");
                    }
                }

                game.Display();

                robot.Move(game);

                game.Display();
            }
        }
Exemple #2
0
        public static bool Backtrack(Game game)
        {
            if (game.IsComplete())
            {
                return(true);
            }

            for (var i = 0; i < 9; i++)
            {
                for (var j = 0; j < 9; j++)
                {
                    if (game.IsEmpty(i, j))
                    {
                        var possibilities = GetLinePossibilities(game, i)
                                            .Intersect(GetColumnPossibilities(game, j))
                                            .Intersect(GetSquarePossibilities(game, i, j));

                        foreach (var possibility in possibilities)
                        {
                            game.Set(i, j, possibility);
                            var result = Backtrack(game);

                            if (result == true)
                            {
                                return(true);
                            }
                        }

                        game.EmptyUp(i, j);

                        return(false);
                    }
                }
            }

            return(false);
        }