public IActionResult Moves(Guid gameId, [FromBody] UserInputForMovesPost userInput) { var directionRaw = userInput.KeyPressed; var game = repo.GetGame(); if (!directions.ContainsKey(directionRaw)) { return(new ObjectResult(game.ToDto())); } var direction = directions[directionRaw]; game.UpdateBoard(direction); repo.SaveGame(game); return(new ObjectResult(game.ToDto())); }
/// <summary> /// Save Human player's most recent move ('X') and the Cpu player's next move ('O') /// </summary> /// <param name="move"></param> /// <returns></returns> public async Task <NextMove> PlayNextMove(PlayerMove move) { var newMove = new NextMove(); try { // Save human player's move await _gameRepo.SaveMove(move); // Get saved board including player's latest move var currentGame = await _gameRepo.GetGame(move.GameId); var isPlayerWinner = IsPlayerWinner(currentGame, GamePlayer.Human); newMove.GameCompleted = (currentGame.Status != GameStatus.Incomplete.ToString()); newMove.Winner = currentGame.Winner; if (!isPlayerWinner) { if (currentGame.Status == GameStatus.Incomplete.ToString()) { var nextCpuMove = GetCpuMove(currentGame); newMove.Cell = nextCpuMove.Cell; newMove.Value = nextCpuMove.Value; // Save cpu player's move await _gameRepo.SaveMove(nextCpuMove); var isCpuWinner = IsPlayerWinner(currentGame, GamePlayer.Cpu); newMove.GameCompleted = (currentGame.Status != GameStatus.Incomplete.ToString()); newMove.Winner = currentGame.Winner; } } } catch (Exception ex) { throw ex; } return(newMove); }