예제 #1
0
        public IEnumerable <TowerGameMoveModel> SolveFromStart(IReadonlyTowerGame game)
        {
            if (game.MaxDiscsPerPeg != 3)
            {
                throw new NotSupportedException("I'm a SIMPLE solver, I can only solve towers that have 3 discs");
            }

            yield return(new TowerGameMoveModel {
                From = PegNumber.One, To = PegNumber.Three
            });

            yield return(new TowerGameMoveModel {
                From = PegNumber.One, To = PegNumber.Two
            });

            yield return(new TowerGameMoveModel {
                From = PegNumber.Three, To = PegNumber.Two
            });

            yield return(new TowerGameMoveModel {
                From = PegNumber.One, To = PegNumber.Three
            });

            yield return(new TowerGameMoveModel {
                From = PegNumber.Two, To = PegNumber.One
            });

            yield return(new TowerGameMoveModel {
                From = PegNumber.Two, To = PegNumber.Three
            });

            yield return(new TowerGameMoveModel {
                From = PegNumber.One, To = PegNumber.Three
            });
        }
        public IEnumerable <TowerGameMoveModel> SolveFromStart(IReadonlyTowerGame game)
        {
            if (!game.IsGameUntouched())
            {
                throw new NotSupportedException("This solver can only solve games from the start");
            }

            foreach (var solution in Solve(game.MaxDiscsPerPeg, (PegNumber)1, (PegNumber)3, (PegNumber)2))
            {
                yield return(solution);
            }
        }
예제 #3
0
        public IEnumerable <TowerGameMoveModel> SolveFromStart(IReadonlyTowerGame game)
        {
            var lastError = "";

            while (true)
            {
                var validInput = false;
                var sourcePeg  = 0;
                var destPeg    = 0;
                try
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(lastError);
                    lastError = "";
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Enter the Source Peg Number:");
                    sourcePeg = int.Parse(Console.ReadLine() ?? string.Empty);
                    Console.WriteLine("Enter the Destination Peg Number:");
                    destPeg    = int.Parse(Console.ReadLine() ?? string.Empty);
                    validInput = true;
                }
                catch (Exception e)
                {
                    lastError = e.Message;
                }

                if (!validInput)
                {
                    continue;
                }

                yield return(new TowerGameMoveModel
                {
                    From = (PegNumber)sourcePeg,
                    To = (PegNumber)destPeg,
                });

                if (game.IsGameOver())
                {
                    yield break;
                }
            }
        }