Exemplo n.º 1
0
        public virtual bool PerformNextJump(Dictionary <char, bool> pegs)
        {
            var jumps = GameInterface.GetPossibleJumps(pegs);

            Console.Write("Choose the peg to jump with: ");

            var left = Console.CursorLeft;
            var top  = Console.CursorTop;

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            GameInterface.PrintJumps(jumps);

            Console.SetCursorPosition(left, top);

            Func <char, bool> CanJumpFrom = (char selectedPeg) => CanJump(jumps, selectedPeg);

            var from = ReadPeg(CanJumpFrom);

            Console.WriteLine(from);

            if (from == null)
            {
                GameInterface.PrintPegs(pegs);

                return(false);
            }

            Console.Write("Choose the peg to jump over: ");

            Func <char, bool> CanJumpTo = (char selectedPeg) => CanJump(jumps, from.Value, selectedPeg);

            var over = ReadPeg(CanJumpTo);

            Console.WriteLine(over);

            if (over != null)
            {
                foreach (var jump in jumps)
                {
                    if (jump.From == from && jump.Over == over)
                    {
                        GameInterface.PerformJump(pegs, jump);

                        return(true);
                    }
                }
            }

            GameInterface.PrintPegs(pegs);

            // Over selection was aborted, ask for the From selection again
            return(PerformNextJump(pegs));
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            if (Array.IndexOf(args, "-stats") >= 0)
            {
                ShowStats();
                return;
            }

            IGameModel model;

            if (Array.IndexOf(args, "-paths") >= 0)
            {
                model = new AllPathsModel(args);
            }
            else if (Array.IndexOf(args, "-first") >= 0)
            {
                model = new FirstWinFromAllPathsModel();
            }
            else if (Array.IndexOf(args, "-expert") >= 0)
            {
                model = new InteractiveModel();
            }
            else
            {
                model = new InteractiveWithHintsModel(args);
            }

            Dictionary <char, bool> pegs;

            do
            {
                pegs = GameInterface.InitializePegs();
                GameInterface.PrintPegs(pegs);

                if (!model.RemoveStartingPeg(pegs))
                {
                    model.PrintStats();
                    return;
                }

                GameInterface.PrintPegs(pegs);

                do
                {
                    model.PrintStats();

                    if (Console.KeyAvailable == true)
                    {
                        Console.WriteLine("Game paused. Press a key to continue.");

                        while (Console.KeyAvailable)
                        {
                            Console.ReadKey(true);
                        }

                        var unpause = Console.ReadKey(true).Key;

                        if (unpause == ConsoleKey.Escape)
                        {
                            return;
                        }
                    }

                    if (!model.PerformNextJump(pegs))
                    {
                        break;
                    }

                    GameInterface.PrintPegs(pegs);
                }while (GameInterface.GetPossibleJumps(pegs).Length > 0);
            }while (model.PlayAgain(pegs));
        }
Exemplo n.º 3
0
        public virtual bool PerformNextJump(Dictionary <char, bool> pegs)
        {
            var jumps = GameInterface.GetPossibleJumps(pegs);

            History.JumpRecord thisJump;

            // If the last path got as far as our previous jump
            if (lastPath != null && lastPath.Count > currentPath.Count - 1)
            {
                // If the paths have been the sae so far, then we need to
                // decide what to do on this jump based on the last path
                if (PathsEqual(lastPath, currentPath))
                {
                    // The previous jumps were the same (or this is the first jump)
                    // Now we need to look ahead at the next jump to determine if
                    // this jump should be the same
                    var lastPathThisJumpIndex  = lastPath[currentPath.Count].JumpIndex;
                    var hasRemainingDecrements = false;

                    for (var i = currentPath.Count + 1; i < lastPath.Count; i++)
                    {
                        if (lastPath[i].JumpIndex > 0)
                        {
                            hasRemainingDecrements = true;
                            break;
                        }
                    }

                    if (hasRemainingDecrements == true)
                    {
                        // There are remaining jumps to decrement,
                        // so we will keep this jump the same
                        thisJump = new History.JumpRecord(jumps[lastPathThisJumpIndex], lastPathThisJumpIndex);
                    }
                    else
                    {
                        // There are no remaining jumps to decrement,
                        // so we need to decrement this one if possible

                        if (lastPathThisJumpIndex > 0)
                        {
                            thisJump = new History.JumpRecord(jumps[lastPathThisJumpIndex - 1], lastPathThisJumpIndex - 1);
                        }
                        else
                        {
                            throw new Exception($"Cannot decrement this jump. Jump number: {currentPath.Count}. Has remaining decrements: {hasRemainingDecrements}.");
                        }
                    }
                }
                else
                {
                    // The previous jumps were different
                    // We will choose the last option for this jump
                    thisJump = new History.JumpRecord(jumps[jumps.Length - 1], jumps.Length - 1);
                }
            }
            else
            {
                // The last path didn't get as far as we have gotten
                // We will choose the last option for this jump
                thisJump = new History.JumpRecord(jumps[jumps.Length - 1], jumps.Length - 1);
            }

            currentPath.Add(thisJump);
            GameInterface.PerformJump(pegs, thisJump.Jump);

            return(true);
        }