Esempio n. 1
0
        public GameResult PlayGame(Match match, MoveChoice moveChoice)
        {
            var rules        = _rulesManager.GetRules();
            var opponentMove = _opponentManager.GetNextMove(match.Opponent.OpponentType, match.Opponent.PreviousMove);
            var result       = new GameResult {
                YourMove = moveChoice, OpponentMove = opponentMove
            };

            if (opponentMove == moveChoice)
            {
                result.Result = Result.Draw;
                return(result);
            }

            var moveChoiceRule = rules.FirstOrDefault(_ => _.MoveChoice == moveChoice);

            if (moveChoiceRule == null)
            {
                throw new RulesException($"Rule not found where MoveChoice is {moveChoice}");
            }

            if (moveChoiceRule.BeatsMoveChoice == opponentMove)
            {
                result.Result = Result.Win;
                return(result);
            }

            result.Result = Result.Lose;
            return(result);
        }
        public MoveChoice GetNextMove(OpponentType opponentType, MoveChoice?previousMove)
        {
            var values = Enum.GetValues(typeof(MoveChoice));

            if (opponentType == OpponentType.Random || !previousMove.HasValue)
            {
                var random = new Random();
                return((MoveChoice)values.GetValue(random.Next(values.Length)));
            }

            var rules = _rulesManager.GetRules();

            var beatenRule = rules.FirstOrDefault(_ => _.BeatsMoveChoice == previousMove.Value);

            if (beatenRule == null)
            {
                throw new RulesException($"Rule not found where BeatsMoveChoice is {previousMove.Value}");
            }

            return(beatenRule.MoveChoice);
        }