Ejemplo n.º 1
0
 private IPeg GetPegByNumber(PegNumber pegNumber)
 {
     return(pegNumber switch
     {
         PegNumber.One => _peg1,
         PegNumber.Two => _peg2,
         PegNumber.Three => _peg3,
         _ => throw new InvalidOperationException("Invalid peg number")
     });
        public IEnumerable <TowerGameMoveModel> Solve(int gameMaxDiscsPerPeg, PegNumber start, PegNumber destination, PegNumber spare)
        {
            if (gameMaxDiscsPerPeg > 0)
            {
                foreach (var solution in Solve(gameMaxDiscsPerPeg - 1, start, spare, destination))
                {
                    yield return(solution);
                }

                yield return(new TowerGameMoveModel {
                    From = start, To = destination
                });

                foreach (var solution in Solve(gameMaxDiscsPerPeg - 1, spare, destination, start))
                {
                    yield return(solution);
                }
            }
        }
Ejemplo n.º 3
0
        public void PerformMove(PegNumber from, PegNumber to)
        {
            if (from == to)
            {
                return;
            }

            var fromPeg = GetPegByNumber(from);
            var toPeg   = GetPegByNumber(to);

            var disc = fromPeg.TakeTopDisc();

            try
            {
                toPeg.PlaceDisc(disc);
            }
            catch
            {
                fromPeg.PlaceDisc(disc);
                throw;
            }
        }