Example #1
0
        public WrongAnswer Guess(string problemId, string program)
        {
            var request = new GuessRequest
            {
                id      = problemId,
                program = program.Replace("=", "")
            };

            var response = webApi.Guess(request);

            if (response.status.Equals("win", StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            if (response.status.Equals("mismatch", StringComparison.OrdinalIgnoreCase))
            {
                return(new WrongAnswer
                {
                    Arg = Convert.ToUInt64(response.values[0], 16),
                    CorrectValue = Convert.ToUInt64(response.values[1], 16),
                    ProvidedValue = Convert.ToUInt64(response.values[2], 16),
                });
            }

            throw new ApplicationException(string.Format("Error GuessResponse ({0}): {1}", response.status, response.message));
        }
Example #2
0
        public static string GetGuessUrl(
            GuessRequest request,
            IAkinatorServer server)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            return($"{server.ServerUrl}/list?session={request.Session}&signature={request.Signature}&step={request.Step}");
        }
Example #3
0
        public void _GuessRequest(PlaneLocator planeLocator)
        {
            GuessRequest guessRequest = new GuessRequest();

            guessRequest.Loc = planeLocator;
            Message message = new Message();

            message.Type         = BombplaneProto.Type.GuessRequest;
            message.Guessrequest = guessRequest;

            NetworkClient.Generate_Package(message);
        }
Example #4
0
        public void botGuess()
        {
            if (!_games.Keys.Contains(Context.ConnectionId))
            {
                Clients.Client(Context.ConnectionId).postHistory("Error", "Start a new game before sending guesses");
                return;
            }

            GameData gameData = _games[Context.ConnectionId];
            string   guess;

            if (gameData.previousAttempts.Count == 0)
            {
                guess = "11112233"; // Fixed first guess;
            }
            else
            {
                MastermindFitnessCalculator mastermindFitnessCalculator = new MastermindFitnessCalculator();
                mastermindFitnessCalculator.setPreviousAttempts(gameData.previousAttempts);

                GeneticAlgorithm ga = new GeneticAlgorithm(mastermindFitnessCalculator);

                char[] guessArray = ga.calculateNextGuess(20);
                guess = new string(guessArray);
            }

            GuessRequest request = new GuessRequest()
            {
                game_key = _games[Context.ConnectionId].game_key, code = AxiomzenMastermind.convertGuess(guess)
            };
            GuessResponse response = AxiomzenMastermind.guess(request);

            gameData.previousAttempts.Add(new Attempt()
            {
                Guess = guess.ToCharArray(), Exact = response.result.exact, Near = response.result.near
            });

            Clients.Client(Context.ConnectionId).postHistory("Guess", string.Format("Code: {0}, Exact: {1}, Near: {2}, Attempts: {3}", guess, response.result.exact, response.result.near, response.num_guesses));

            if (response.solved)
            {
                Clients.Client(Context.ConnectionId).postHistory("Congratulations!", "You guessed the secret code!");
            }
        }
        public static string GetGuessUrl(
            GuessRequest request,
            Language language,
            ServerType serverType)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            var server = ServerSelector.GetServerFor(language, serverType);

            if (string.IsNullOrEmpty(server))
            {
                throw new InvalidOperationException($"No server does match the language {language} and server type {serverType}.");
            }

            var url = $"https://{server}/list?session={request.Session}&signature={request.Signature}&step={request.Step}";

            return(url);
        }
        public static GuessResponse guess(GuessRequest guessRequest)
        {
            try
            {
                string url = serviceUrl + "guess?game_key=" + guessRequest.game_key
                             + "&code=" + guessRequest.code;

                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method      = "POST";
                request.ContentType = "text/json";

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    DataContractJsonSerializer jsonResponseSerializer = new DataContractJsonSerializer(typeof(GuessRequest));
                    jsonResponseSerializer.WriteObject(streamWriter.BaseStream, guessRequest);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
                    }

                    DataContractJsonSerializer jsonResponseSerializer = new DataContractJsonSerializer(typeof(GuessResponse));
                    object        objResponse  = jsonResponseSerializer.ReadObject(response.GetResponseStream());
                    GuessResponse jsonResponse = objResponse as GuessResponse;
                    return(jsonResponse);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(null);
            }
        }
Example #7
0
        public void sendGuess(string guess)
        {
            if (!_games.Keys.Contains(Context.ConnectionId))
            {
                Clients.Client(Context.ConnectionId).postHistory("Error", "Start a new game before sending guesses");
                return;
            }

            int guessInt;

            if (guess.Length != 8 || !int.TryParse(guess, out guessInt))
            {
                Clients.Client(Context.ConnectionId).postHistory("Error", "Wrong guess format, must be 8 digit code, with values from 1 to 8");
                return;
            }

            GuessRequest request = new GuessRequest()
            {
                game_key = _games[Context.ConnectionId].game_key, code = AxiomzenMastermind.convertGuess(guess)
            };
            GuessResponse response = AxiomzenMastermind.guess(request);

            GameData gameData = _games[Context.ConnectionId];

            gameData.previousAttempts.Add(new Attempt()
            {
                Guess = guess.ToCharArray(), Exact = response.result.exact, Near = response.result.near
            });

            Clients.Client(Context.ConnectionId).postHistory("Guess", string.Format("Code: {0}, Exact: {1}, Near: {2}, Attempts: {3}", guess, response.result.exact, response.result.near, response.num_guesses));

            if (response.solved)
            {
                Clients.Client(Context.ConnectionId).postHistory("Congratulations!", "You guessed the secret code!");
            }
        }