Ejemplo n.º 1
0
    protected override void DrawCards(Deck deck)
    {
        var keepMask = Strategy.KeepMask ?? Hand.KeepMask;
        var count    = 0;

        for (var i = 1; i <= 5; i++)
        {
            if ((keepMask & (1 << (i - 1))) == 0)
            {
                Hand = Hand.Replace(i, deck.DealCard());
                count++;
            }
        }

        _io.WriteLine();
        _io.Write($"I am taking {count} card");
        if (count != 1)
        {
            _io.WriteLine("s");
        }

        Strategy = (Hand.IsWeak, Hand.Rank < HandRank.Three, Hand.Rank < HandRank.FullHouse) switch
        {
            _ when Strategy is Bluff => Strategy.Bluff(28),
            (true, _, _) => Strategy.Fold,
            (false, true, _) => _random.Next(10) == 0 ? Strategy.Bet(19) : Strategy.Raise,
            (false, false, true) => _random.Next(10) == 0 ? Strategy.Bet(11) : Strategy.Bet(19),
            (false, false, false) => Strategy.Raise
        };
    }
Ejemplo n.º 2
0
    protected override CommandResult ExecuteCommandCore(Quadrant quadrant)
    {
        var index = GetFunctionIndex();

        _io.WriteLine();

        _functions[index].Execute(quadrant);

        return(CommandResult.Ok);
    }
Ejemplo n.º 3
0
    private bool HasTorpedoes()
    {
        if (TorpedoCount > 0)
        {
            return(true);
        }

        _io.WriteLine("All photon torpedoes expended");
        return(false);
    }
    protected override CommandResult ExecuteCommandCore(Quadrant quadrant)
    {
        _io.WriteLine($"Long range scan for quadrant {quadrant.Coordinates}");
        _io.WriteLine("-------------------");
        foreach (var quadrants in _galaxy.GetNeighborhood(quadrant))
        {
            _io.WriteLine(": " + string.Join(" : ", quadrants.Select(q => q?.Scan() ?? "***")) + " :");
            _io.WriteLine("-------------------");
        }

        return(CommandResult.Ok);
    }
Ejemplo n.º 5
0
        private bool TryGetWarpFactor(out float warpFactor)
        {
            var maximumWarp = IsDamaged ? 0.2f : 8;

            if (_io.TryReadNumberInRange("Warp Factor", 0, maximumWarp, out warpFactor))
            {
                return(warpFactor > 0);
            }

            _io.WriteLine(
                IsDamaged && warpFactor > maximumWarp
                    ? "Warp engines are damaged.  Maximum speed = warp 0.2"
                    : $"  Chief Engineer Scott reports, 'The engines won't take warp {warpFactor} !'");

            return(false);
        }
Ejemplo n.º 6
0
    internal static float ReadParameter(this IReadWrite io, string parameter)
    {
        var value = io.ReadNumber(parameter);

        io.WriteLine();
        return(value);
    }
Ejemplo n.º 7
0
    protected override CommandResult ExecuteCommandCore(Quadrant quadrant)
    {
        _io.WriteLine($"Energy available = {_enterprise.TotalEnergy}");
        var requested = _io.ReadNumber($"Number of units to shields");

        if (Validate(requested))
        {
            ShieldEnergy = requested;
            _io.Write(Strings.ShieldsSet, requested);
        }
        else
        {
            _io.WriteLine("<SHIELDS UNCHANGED>");
        }

        return(CommandResult.Ok);
    }
Ejemplo n.º 8
0
    internal bool Resolve(Scoreboard scoreboard)
    {
        var winner = _random.NextFloat() <= _probability ? scoreboard.Home : scoreboard.Visitors;

        scoreboard.Offense = winner;
        _io.WriteLine(_messageFormat, winner);
        return(false);
    }
Ejemplo n.º 9
0
    public void Play()
    {
        while (true)
        {
            _io.Write(Streams.Introduction);

            var limit = _io.ReadNumber(Prompts.Limit);
            _io.WriteLine();

            // There's a bug here that exists in the original code.
            // If the limit entered is <= 0 then the program will crash.
            var targetGuessCount = checked ((int)Math.Log2(limit) + 1);

            PlayGuessingRounds(limit, targetGuessCount);

            _io.Write(Streams.BlankLines);
        }
    }
Ejemplo n.º 10
0
    public void Play()
    {
        _io.Write(Streams.Introduction);

        do
        {
            var board = new Board();
            do
            {
                _io.WriteLine(board);
                _io.WriteLine();
            } while (board.PlayMove(_io));

            _io.WriteLine(board.GetReport());
        } while (_io.ReadYesNo(Prompts.TryAgain) == "yes");

        _io.Write(Streams.Bye);
    }
Ejemplo n.º 11
0
    protected override void DrawCards(Deck deck)
    {
        var count = _io.ReadNumber("How many cards do you want", 3, "You can't draw more than three cards.");

        if (count == 0)
        {
            return;
        }

        _io.WriteLine("What are their numbers:");
        for (var i = 1; i <= count; i++)
        {
            Hand = Hand.Replace((int)_io.ReadNumber(), deck.DealCard());
        }

        _io.WriteLine("Your new hand:");
        _io.Write(Hand);
    }
Ejemplo n.º 12
0
    protected bool IsOperational(string notOperationalMessage)
    {
        if (IsDamaged)
        {
            _io.WriteLine(notOperationalMessage.Replace("{name}", Name));
            return(false);
        }

        return(true);
    }
    protected override CommandResult ExecuteCommandCore(Quadrant quadrant)
    {
        if (_enterprise.IsDocked)
        {
            _io.WriteLine(Strings.ShieldsDropped);
        }

        if (Condition < 0)
        {
            _io.WriteLine(Strings.ShortRangeSensorsOut);
        }

        _io.WriteLine("---------------------------------");
        quadrant.GetDisplayLines()
        .Zip(GetStatusLines(), (sectors, status) => $" {sectors}         {status}")
        .ToList()
        .ForEach(l => _io.WriteLine(l));
        _io.WriteLine("---------------------------------");

        return(CommandResult.Ok);
    }
Ejemplo n.º 14
0
    protected override CommandResult ExecuteCommandCore(Quadrant quadrant)
    {
        if (!quadrant.HasKlingons)
        {
            _io.WriteLine(Strings.NoEnemyShips);
            return(CommandResult.Ok);
        }

        if (_enterprise.Computer.IsDamaged)
        {
            _io.WriteLine("Computer failure hampers accuracy");
        }

        _io.Write($"Phasers locked on target;  ");

        var phaserStrength = GetPhaserStrength();

        if (phaserStrength < 0)
        {
            return(CommandResult.Ok);
        }

        _enterprise.UseEnergy(phaserStrength);

        var perEnemyStrength = GetPerTargetPhaserStrength(phaserStrength, quadrant.KlingonCount);

        foreach (var klingon in quadrant.Klingons.ToList())
        {
            ResolveHitOn(klingon, perEnemyStrength, quadrant);
        }

        return(quadrant.KlingonsFireOnEnterprise());
    }
Ejemplo n.º 15
0
    private void BuildBugs()
    {
        var yourBug = new Bug();
        var myBug   = new Bug();

        while (true)
        {
            var partAdded = TryBuild(yourBug, m => m.You);
            Thread.Sleep(500);
            _io.WriteLine();
            partAdded |= TryBuild(myBug, m => m.I);

            if (partAdded)
            {
                if (yourBug.IsComplete)
                {
                    _io.WriteLine("Your bug is finished.");
                }
                if (myBug.IsComplete)
                {
                    _io.WriteLine("My bug is finished.");
                }

                if (!_io.ReadString("Do you want the picture").Equals("no", InvariantCultureIgnoreCase))
                {
                    _io.Write(yourBug.ToString("Your", 'A'));
                    _io.WriteLine();
                    _io.WriteLine();
                    _io.WriteLine();
                    _io.WriteLine();
                    _io.Write(myBug.ToString("My", 'F'));
                }
            }

            if (yourBug.IsComplete || myBug.IsComplete)
            {
                break;
            }
        }
    }
 internal static int ReadNumber(this IReadWrite io, string prompt, int max, string maxPrompt)
 {
     io.Write(prompt);
     while (true)
     {
         var response = io.ReadNumber();
         if (response <= max)
         {
             return((int)response);
         }
         io.WriteLine(maxPrompt);
     }
 }
 internal static Strategy ReadHumanStrategy(this IReadWrite io, bool noCurrentBets)
 {
     while (true)
     {
         io.WriteLine();
         var bet = io.ReadNumber("What is your bet");
         if (bet != (int)bet)
         {
             if (noCurrentBets && bet == .5)
             {
                 return(Strategy.Check);
             }
             io.WriteLine("No small change, please.");
             continue;
         }
         if (bet == 0)
         {
             return(Strategy.Fold);
         }
         return(Strategy.Bet(bet));
     }
 }
Ejemplo n.º 18
0
    public void Draw()
    {
        var diamondSize = _io.ReadNumber(Prompts.TypeNumber);

        _io.WriteLine();

        var diamondCount = (int)(60 / diamondSize);

        var diamondLines = new List <string>(GetDiamondLines(diamondSize)).AsReadOnly();

        for (int patternRow = 0; patternRow < diamondCount; patternRow++)
        {
            for (int diamondRow = 0; diamondRow < diamondLines.Count; diamondRow++)
            {
                var line = new StringBuilder();
                for (int patternColumn = 0; patternColumn < diamondCount; patternColumn++)
                {
                    line.PadToLength((int)(patternColumn * diamondSize)).Append(diamondLines[diamondRow]);
                }
                _io.WriteLine(line);
            }
        }
    }
    // Implements original code:
    //   120 PRINT "YOUR MOVE";
    //   121 INPUT M1,M2
    //   122 IF M1=INT(M1)AND M2=INT(M2)AND M1>0 AND M1<10 AND M2>0 AND M2<10 THEN 130
    //   123 PRINT "ILLEGAL CO-ORDINATES."
    //   124 GOTO 120
    internal static Move ReadMove(this IReadWrite io, string prompt)
    {
        while (true)
        {
            var(from, to) = io.Read2Numbers(prompt);

            if (Move.TryCreate(from, to, out var move))
            {
                return(move);
            }

            io.WriteLine("Illegal Coordinates.");
        }
    }
Ejemplo n.º 20
0
    internal void Play()
    {
        _io.Write(Resource.Streams.Introduction);
        if (_io.ReadNumber("Do you want the rules (1=Yes, 0=No!)") != 0)
        {
            _io.Write(Resource.Streams.Rules);
        }

        while (true)
        {
            _io.Write(Resource.Streams.HereWeGo);

            var(playerCount, rowCount, columnCount) = _io.ReadParameters();

            var loser = Play(new Cookie(rowCount, columnCount), new PlayerNumber(playerCount));

            _io.WriteLine(string.Format(Resource.Formats.YouLose, loser));

            if (_io.ReadNumber("Again (1=Yes, 0=No!)") != 1)
            {
                break;
            }
        }
    }
 internal static bool ReadYesNo(this IReadWrite io, string prompt)
 {
     while (true)
     {
         var response = io.ReadString(prompt);
         if (response.Equals("YES", InvariantCultureIgnoreCase))
         {
             return(true);
         }
         if (response.Equals("NO", InvariantCultureIgnoreCase))
         {
             return(false);
         }
         io.WriteLine("Answer Yes or No, please.");
     }
 }
Ejemplo n.º 22
0
    internal void Play()
    {
        DoIntroduction();

        var result = _io.ReadNumber(Prompts.Answer);

        if (_io.ReadYes(Formats.Bet, Math.CalculateOriginal(result)))
        {
            _io.Write(Streams.Bye);
            return;
        }

        var original = _io.ReadNumber(Prompts.Original);

        _io.WriteLine(Math.ShowWorking(original));

        if (_io.ReadYes(Prompts.Believe))
        {
            _io.Write(Streams.Bye);
            return;
        }

        _io.Write(Streams.Lightning);
    }
Ejemplo n.º 23
0
    public void Play(Func <bool> playAgain)
    {
        _io.Write(Streams.Title);
        _io.Write(Streams.Instructions);

        while (playAgain.Invoke())
        {
            var timeIncrement = _io.ReadParameter("Time increment (sec)");
            var velocity      = _io.ReadParameter("Velocity (fps)");
            var elasticity    = _io.ReadParameter("Coefficient");

            var bounce      = new Bounce(velocity);
            var bounceCount = (int)(Graph.Row.Width * timeIncrement / bounce.Duration);
            var graph       = new Graph(bounce.MaxHeight, timeIncrement);

            var time = 0f;
            for (var i = 0; i < bounceCount; i++, bounce = bounce.Next(elasticity))
            {
                time = bounce.Plot(graph, time);
            }

            _io.WriteLine(graph);
        }
    }
Ejemplo n.º 24
0
    protected override CommandResult ExecuteCommandCore(Quadrant quadrant)
    {
        if (IsDamaged)
        {
            _io.WriteLine("Damage Control report not available");
        }
        else
        {
            _io.WriteLine();
            WriteDamageReport();
        }

        if (_enterprise.DamagedSystemCount > 0 && _enterprise.IsDocked)
        {
            if (quadrant.Starbase.TryRepair(_enterprise, out var repairTime))
            {
                WriteDamageReport();
                return(CommandResult.Elapsed(repairTime));
            }
        }

        return(CommandResult.Ok);
    }
Ejemplo n.º 25
0
        public void Play()
        {
            _shotCount = 0;
            var target = _firingRange.NextTarget();

            _io.WriteLine(target.GetBearing());
            _io.WriteLine($"Target sighted: approximate coordinates:  {target}");

            while (true)
            {
                _io.WriteLine($"     Estimated distance: {target.EstimateDistance()}");
                _io.WriteLine();

                var explosion = Shoot();

                if (explosion.IsTooClose)
                {
                    _io.WriteLine("You blew yourself up!!");
                    return;
                }

                _io.WriteLine(explosion.GetBearing());

                if (explosion.IsHit)
                {
                    ReportHit(explosion.DistanceToTarget);
                    return;
                }

                ReportMiss(explosion);
            }
        }
Ejemplo n.º 26
0
    internal void PlayHand()
    {
        while (true)
        {
            _io.WriteLine();
            Computer.CheckFunds();
            if (Computer.IsBroke)
            {
                return;
            }

            _io.WriteLine($"The ante is ${Ante}.  I will deal:");
            _io.WriteLine();
            if (Human.Balance <= Ante)
            {
                Human.RaiseFunds();
                if (Human.IsBroke)
                {
                    return;
                }
            }

            Deal(_random);

            _io.WriteLine();
            GetWagers("I'll open with ${0}", "I check.", allowRaiseAfterCheck: true);
            if (SomeoneIsBroke() || SomeoneHasFolded())
            {
                return;
            }

            Draw();

            GetWagers();
            if (SomeoneIsBroke())
            {
                return;
            }
            if (!Human.HasBet)
            {
                GetWagers("I'll bet ${0}", "I'll check");
            }
            if (SomeoneIsBroke() || SomeoneHasFolded())
            {
                return;
            }
            if (GetWinner() is { } winner)
            {
                winner.TakeWinnings();
                return;
            }
        }
    }
Ejemplo n.º 27
0
 protected Probably Resolve(string message, float defenseFactor)
 {
     _io.WriteLine(message);
     return(new Probably(defenseFactor, _random));
 }
Ejemplo n.º 28
0
 internal void ProtectEnterprise() => _io.WriteLine(Strings.Protected);