public static Match GetMatchFromMatchState(MatchState matchState) { return new Match { id = matchState.id, game_type = (int)matchState.gameType, winner_id = matchState.winnerId, game_start_time = matchState.gameStartTime, game_end_time = matchState.gameEndTime, }; }
public static Guid AlternateTurn(MatchState currentState) { var turn = Guid.Empty; if (currentState.playerTurnId == currentState.inGameState.firstPlayer) { turn = currentState.inGameState.secondPlayer; } else { turn = currentState.inGameState.firstPlayer; } return turn; }
public void CalculateNextState_GameIsATie_ReturnsUpdatedMatch() { var playerOne = Guid.NewGuid(); var playerTwo = Guid.NewGuid(); var move = MovePosition.MiddleCenter; var currentState = new MatchState { operationState = GameOperationState.InProgress, playerTurnId = playerOne, players = new List<Guid> { playerOne, playerTwo }, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.X, PlayerMark.O, PlayerMark.O, PlayerMark.O, PlayerMark.Empty, PlayerMark.X, PlayerMark.O, PlayerMark.X, PlayerMark.O, } }, }; var expectedState = new MatchState { operationState = GameOperationState.Completed, playerTurnId = Guid.Empty, winnerId = Guid.Empty, players = new List<Guid> { playerOne, playerTwo }, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.X, PlayerMark.O, PlayerMark.O, PlayerMark.O, PlayerMark.X, PlayerMark.X, PlayerMark.O, PlayerMark.X, PlayerMark.O, } }, }; var nextState = TicTacToeLogic.CalculateNextState(currentState, playerOne, move); Assert.Equal(expectedState.operationState, nextState.operationState); Assert.Equal(expectedState.playerTurnId, nextState.playerTurnId); Assert.Equal(expectedState.winnerId, nextState.winnerId); Assert.Equal(expectedState.inGameState.board, nextState.inGameState.board); }
public static MatchState CalculateNextState(MatchState currentState, Guid playerId, MovePosition move) { var nextState = currentState; if (nextState.operationState == GameOperationState.Completed) { throw new GameIsOverException(); } if (nextState.playerTurnId != playerId) { throw new NotPlayersTurnException(); } if (nextState.inGameState.board[(int)move] != PlayerMark.Empty) { throw new InvalidPlayerMoveException(); } if (playerId == nextState.inGameState.firstPlayer) { nextState.inGameState.board[(int)move] = PlayerMark.X; } else { nextState.inGameState.board[(int)move] = PlayerMark.O; } if (DoesGameHaveWinner(nextState.inGameState)) { nextState.playerTurnId = Guid.Empty; nextState.winnerId = playerId; nextState.gameEndTime = DateTime.Now; nextState.operationState = GameOperationState.Completed; } else if (IsGameTie(nextState.inGameState)) { nextState.playerTurnId = Guid.Empty; nextState.winnerId = Guid.Empty; nextState.gameEndTime = DateTime.Now; nextState.operationState = GameOperationState.Completed; } else { nextState.playerTurnId = AlternateTurn(nextState); } return nextState; }
public void AlternateTurn_PlayerTwo() { var one = Guid.NewGuid(); var two = Guid.NewGuid(); var match = new MatchState { playerTurnId = two, inGameState = new TicTacToeState { firstPlayer = one, secondPlayer = two, }, }; var result = TicTacToeLogic.AlternateTurn(match); Assert.Equal(one, result); }
public void StoreMatch_DoesNotThrowException() { var match = new MatchState { id = Guid.NewGuid(), gameStartTime = DateTime.Now, gameEndTime = DateTime.Now.AddHours(1), gameType = GameType.TicTacToe, operationState = GameOperationState.Completed, winnerId = _firstPlayer, playerTurnId = _firstPlayer, players = new List<Guid> { _firstPlayer, _secondPlayer }, inGameState = new TicTacToeState { firstPlayer = _firstPlayer, secondPlayer = _secondPlayer, board = new List<PlayerMark>(), } }; _controller.StoreMatch(match); using (var context = new GameServerEntities()) { var storedMatches = context.Matches.ToList(); Assert.Contains(match.id, storedMatches.Select(x => x.id)); } }
public void CalculateNextState_PlayerTwoMakesMove_IsSuccessful() { var matchId = Guid.NewGuid(); var playerOne = Guid.NewGuid(); var playerTwo = Guid.NewGuid(); var move = MovePosition.MiddleCenter; var currentState = new MatchState { id = matchId, operationState = GameOperationState.InProgress, playerTurnId = playerTwo, players = new List<Guid> { playerOne, playerTwo }, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, } }, }; var expectedState = new MatchState { id = matchId, operationState = GameOperationState.InProgress, playerTurnId = playerOne, players = new List<Guid> { playerOne, playerTwo }, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.O, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, } }, }; var nextState = TicTacToeLogic.CalculateNextState(currentState, playerTwo, move); Assert.Equal(expectedState.playerTurnId, nextState.playerTurnId); Assert.Equal(expectedState.inGameState.board, nextState.inGameState.board); }
public void CalculateNextState_NotPlayersTurn_ThrowsException() { var playerOne = Guid.NewGuid(); var playerTwo = Guid.NewGuid(); var move = MovePosition.MiddleCenter; var currentState = new MatchState { operationState = GameOperationState.InProgress, playerTurnId = playerTwo, }; Assert.Throws(typeof(NotPlayersTurnException), () => TicTacToeLogic.CalculateNextState(currentState, playerOne, move)); }
public void CalculateNextState_InvalidMovePositionIsNotEmpty_ThrowsException() { var playerOne = Guid.NewGuid(); var playerTwo = Guid.NewGuid(); var move = MovePosition.MiddleCenter; var currentState = new MatchState { operationState = GameOperationState.InProgress, playerTurnId = playerOne, inGameState = new TicTacToeState { firstPlayer = playerOne, board = new List<PlayerMark> { PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.X, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, } }, }; Assert.Throws(typeof(InvalidPlayerMoveException), () => TicTacToeLogic.CalculateNextState(currentState, playerOne, move)); }
public void CalculateNextState_GameIsOver_ThrowsException() { var playerOne = Guid.NewGuid(); var move = MovePosition.MiddleCenter; var currentState = new MatchState { operationState = GameOperationState.Completed, }; Assert.Throws(typeof(GameIsOverException), () => TicTacToeLogic.CalculateNextState(currentState, playerOne, move)); }
public void GetMatchState_IsSuccessful() { var match = new MatchState { id = Guid.NewGuid() }; _onlineMatchList.Add(match); var result = _service.GetMatchState(match.id); Assert.Equal(match, result); }
public void Quit_WithInvalidPlayerId_ThrowsException() { var matchId = Guid.NewGuid(); var playerOne = Guid.Parse("12514A18-9A18-443A-9805-79BC51D38B6F"); var playerTwo = Guid.Parse("8701DD3B-00BB-4517-9BAB-453965DB1E81"); var wrongPlayer = Guid.NewGuid(); var expected = new MatchState { id = matchId, players = new List<Guid> { playerOne, playerTwo }, }; _onlineMatchList.Add(expected); Assert.Throws(typeof(FaultException<PlayerNotInSpecifiedGameFault>), () => _service.Quit(matchId, wrongPlayer)); }
public void Quit_IsSuccessful() { var matchId = Guid.NewGuid(); var playerOne = Guid.Parse("12514A18-9A18-443A-9805-79BC51D38B6F"); var playerTwo = Guid.Parse("8701DD3B-00BB-4517-9BAB-453965DB1E81"); var existingMatch = new MatchState { id = matchId, players = new List<Guid> { playerOne, playerTwo }, gameStartTime = DateTime.Now, gameType = GameType.TicTacToe, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, }, }, operationState = GameOperationState.InProgress, }; var expected = new MatchState { id = matchId, players = new List<Guid> { playerOne, playerTwo }, winnerId = playerTwo, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, }, }, operationState = GameOperationState.Completed, }; _onlineMatchList.Add(existingMatch); _service.Quit(matchId, playerOne); var result = _service.GetMatchState(matchId); Assert.Equal(expected.id, result.id); Assert.Equal(expected.winnerId, result.winnerId); Assert.Equal(expected.operationState, result.operationState); }
public void PlayerMove_WaitingForPlayers_ThrowsException() { var matchId = Guid.NewGuid(); var playerId = Guid.Parse("12514A18-9A18-443A-9805-79BC51D38B6F"); var move = MovePosition.MiddleCenter; var match = new MatchState { id = matchId, operationState = GameOperationState.WaitingForPlayers, }; _onlineMatchList.Add(match); Assert.Throws(typeof(FaultException<WaitingForPlayersFault>), () => _service.PlayerMove(matchId, playerId, move)); }
public void JoinGame_MatchListIsNotEmpty_PlayerJoinsMatch() { var playerOne = Guid.NewGuid(); var playerTwo = Guid.NewGuid(); var type = GameType.TicTacToe; var newMatch = MatchFactory.CreateNewMatch(type, playerOne); var expectedMatch = new MatchState { id = newMatch.id, gameStartTime = newMatch.gameStartTime, gameType = type, players = new List<Guid> { playerOne, playerTwo }, inGameState = new TicTacToeState { firstPlayer = playerOne, secondPlayer = playerTwo, board = new List<PlayerMark> { PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, PlayerMark.Empty, }, }, operationState = GameOperationState.InProgress, }; _onlineMatchList.Add(newMatch); var result = _service.JoinGame(type, playerTwo); Assert.Equal(expectedMatch.id, result.id); Assert.Equal(expectedMatch.gameStartTime, result.gameStartTime); Assert.Equal(expectedMatch.gameType, result.gameType); Assert.Equal(expectedMatch.players, result.players); Assert.Equal(expectedMatch.inGameState.firstPlayer, result.inGameState.firstPlayer); Assert.Equal(expectedMatch.inGameState.secondPlayer, result.inGameState.secondPlayer); Assert.Equal(expectedMatch.inGameState.board, result.inGameState.board); Assert.Equal(expectedMatch.operationState, result.operationState); }