public IGuessResult CheckGuess(int guess) { var guessResult = new GuessResult(); var guessList = NumToListConverter.Convert(guess); var secretNumberList = NumToListConverter.Convert(this.SecretNumberProvider.GetSecretNumber()); for (int i = 0; i < secretNumberList.Count; i++) { var guessDigit = guessList[i]; if (secretNumberList.Contains(guessDigit)) { if (secretNumberList.IndexOf(guessDigit) == i) { guessResult.Bulls++; } else { guessResult.Cows++; } } } return(guessResult); }
public IHttpActionResult CreateSecret(string playerId, int gameId, int secret) { var player = this.context.GetPlayer(playerId); var game = this.context.GetGame(gameId); var secretLen = NumToListConverter.Convert(secret).Count; if (player == null || game == null) { return(NotFound()); } var opponent = this.context.GetOpponentPlayer(player, game); if (opponent == this.context.GetComputerPlayer()) { var secretNumberForComputer = new SecretNumber() { Game = game, Player = opponent, Number = SecretNumberGenerator.GenerateUniqueSecretNumber(secretLen) }; this.context.SecretNumbers.Add(secretNumberForComputer); } var secretNumber = new SecretNumber { Game = game, Player = player, Number = secret }; context.SecretNumbers.Add(secretNumber); context.SaveChanges(); return(Ok()); }
public IHttpActionResult GuessSecret(string playerId, int gameId, int guess) { var player = this.context.GetPlayer(playerId); var game = this.context.GetGame(gameId); var guessLen = NumToListConverter.Convert(guess).Count; if (player == null || game == null) { return(NotFound()); } var opponent = this.context.GetOpponentPlayer(player, game); opponent.SecretNumberProvider = new DbSecretNumberProvider(gameId, opponent.PlayerId); var playerGuessResult = opponent.CheckGuess(guess); var hasFoundSecret = playerGuessResult.Bulls == guessLen; if (hasFoundSecret) { game.Finish(); player.Score += 100; } var playerGuessViewModel = new PlayerGuessResultViewModel() { PlayerId = player.PlayerId, BullsGuessed = playerGuessResult.Bulls, CowsGuessed = playerGuessResult.Cows, Guess = guess, HasGameFinished = hasFoundSecret, Winner = hasFoundSecret ? player.Name : null }; this.context.PlayerGuessResults.Add(new PlayerGuessResult() { Game = game, Player = player, BullsGuessed = playerGuessResult.Bulls, CowsGuessed = playerGuessResult.Cows, Guess = guess, }); PlayerGuessResultViewModel computerGuessResultViewModel = null; if (opponent == this.context.GetComputerPlayer()) { if (!hasFoundSecret) { player.SecretNumberProvider = new DbSecretNumberProvider(gameId, playerId); var computerGuess = SecretNumberGenerator.GenerateUniqueSecretNumber(guessLen); var opponentGuessResult = player.CheckGuess(computerGuess); var hasGuessedSecret = playerGuessResult.Bulls == guessLen; if (hasGuessedSecret) { opponent.Score += 100; game.Finish(); } computerGuessResultViewModel = new PlayerGuessResultViewModel() { PlayerId = opponent.PlayerId, BullsGuessed = opponentGuessResult.Bulls, CowsGuessed = opponentGuessResult.Cows, Guess = computerGuess, HasGameFinished = hasGuessedSecret, Winner = hasGuessedSecret ? opponent.Name : null }; this.context.PlayerGuessResults.Add(new PlayerGuessResult() { Game = game, Player = opponent, BullsGuessed = opponentGuessResult.Bulls, CowsGuessed = opponentGuessResult.Cows, Guess = computerGuess }); } } this.context.SaveChanges(); var guessResultViewModel = new List <PlayerGuessResultViewModel>(); guessResultViewModel.Add(playerGuessViewModel); if (computerGuessResultViewModel != null) { guessResultViewModel.Add(computerGuessResultViewModel); } return(Ok(guessResultViewModel)); }