예제 #1
0
        public IHttpActionResult Shoot([FromBody] object input)
        {
            var Match = (input as JObject).ToObject <Match>();

            if (Match.PlayerName == null)
            {
                return(BadRequest("PlayerName is invalid"));
            }

            //Initialize some stuff
            Choice AIChoice = new Choice();

            Match.Timestamp = DateTime.Now;
            var numMatchesToTrainOn = 14;

            //Set up our data for training
            var MatchHistoryTable = Logic.MyMatchHistoryAsTable(Match.PlayerName, numMatchesToTrainOn);
            var InTrain           = Logic.FromMatchHistoryTableOmitWinners(MatchHistoryTable);
            var OutTrain          = Logic.FromMatchHistoryTableGenerateWinners(MatchHistoryTable);


            //Give it a random Value if we don't have enough training data
            if (InTrain.Length <= 7)
            {
                Array  Values = Enum.GetValues(typeof(Choice));
                Random Random = new Random();
                AIChoice = (Choice)Values.GetValue(Random.Next(Values.Length));
            }
            else
            {
                //Train a new Model
                var Training = new Training();
                Training.Run(InTrain, OutTrain);

                //Get the most recent match frame and make sure we put in this match
                var ThisFrame = Logic.MostRecentMatchFrame(Match);

                //Decide on the AI choice
                var PredictedPlayerChoice = (Choice)Training.Decide(ThisFrame);
                AIChoice = Logic.WinAgainst(PredictedPlayerChoice);
            }



            Match.P2Choice = AIChoice;

            using (var context = new RPSContext())
            {
                return(Ok(CreateMatch(Match, context)));
            }
        }