Example #1
0
        public void PlayerWithRedCowsGoesFirst()
        {
            //TODO: Make sure that the the player who goes first has the dark cows
            IGameSession gameSession = GameSessionFactory.CreateGameSession();

            Assert.AreEqual(gameSession.Current_Player.Color, Color.Red);
        }
Example #2
0
        public async Task <GameSession> InitializeGame(Guid gameDefinitionId, int numberOfPlayers)
        {
            var gameDefinition = await gameDefinitionAccess.GetGameDefinition(gameDefinitionId);

            var createdPlayers = await CreatePlayers(numberOfPlayers, gameDefinition);

            var gameSession = GameSessionFactory.Create(gameDefinitionId, createdPlayers.Select(i => i.Id).ToArray());
            await gameSessionAccess.CreateGameSession(gameSession.Convert());

            await CreateTicTacToeBoard(gameSession);

            return(gameSession);
        }
Example #3
0
        public async Task <GameSession> InitializeGame(Guid gameDefinitionId, int numberOfPlayers)
        {
            var gameDefinitions = await gameDefinitionAccess.FindGameDefinitions(i => i.Id == gameDefinitionId);

            var gameDefinition = gameDefinitions.FirstOrDefault();

            gameDefinition.NotNull();

            var createdPlayers = await CreatePlayers(numberOfPlayers, gameDefinition);

            var gameSession = GameSessionFactory.Create(gameDefinitionId, createdPlayers.Select(i => i.Id).ToArray());
            await gameSessionAccess.ProvisionGameSession(gameSession);

            var tiles = TicTacToeBoardFactory.Create(gameSession.Id, new[] { "A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3" });
            await tileAccess.ProvisionTiles(tiles.ToArray());

            return(gameSession);
        }
Example #4
0
        static void Main(string[] args)
        {
            // Set the stage
            SetConsoleProperties();

            IGameSession gameSession = GameSessionFactory.CreateGameSession();

            gameSession.Start();

            if (Console.ReadLine() == "y")
            {
                Main(null);
            }
            else
            {
                Environment.Exit(0);
            }
        }
Example #5
0
        public Response StartNewGame(string userName, bool multiplayer)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return(new Response("Please provide a valid player name."));
            }
            Response response;

            try
            {
                var session = GameSessionFactory.GetSession(_colors, userName, multiplayer);
                response = new Response(_colors, session.Players[0].GameKey, multiplayer);
            }
            catch (GameSessionException ex)
            {
                response = new Response(ex);
            }
            return(response);
        }
Example #6
0
        public async Task <InitializeGameResponse> InitializeGameAsync(InitializeGameRequest request)
        {
            var response = ServiceMessageFactory <InitializeGameResponse> .CreateFrom(request);

            var gameDefinitionRequest = ServiceMessageFactory <GetGameDefinitionRequest> .CreateFrom(request);

            gameDefinitionRequest.GameDefinitionId = request.GameDefinitionId;

            var gameDefinitionResponse = await gameDefinitionAccess.GetGameDefinitionAsync(gameDefinitionRequest);

            // Create Players
            var players = new List <Player>();

            for (var idx = 0; idx < gameDefinitionResponse.GameDefinition.GamePieces.Length; idx++)
            {
                var name      = $"Player {idx + 1}";
                var gamePiece = gameDefinitionResponse.GameDefinition.GamePieces[idx];
                var isMachine = request.NumberOfPlayers <= idx;

                var player = PlayerFactory.Create(name, gamePiece, isMachine);
                players.Add(player);
            }

            var createPlayersRequest = ServiceMessageFactory <CreatePlayersRequest> .CreateFrom(request);

            createPlayersRequest.Players = players.Convert();
            var createPlayerResponse = await playerAccess.CreatePlayersAsync(createPlayersRequest);

            if (createPlayerResponse.HasErrors)
            {
                logger.LogError($"{createPlayerResponse.Errors}");
            }


            var playerIds   = players.Select(i => i.Id).ToArray();
            var gameSession = GameSessionFactory.Create(request.GameDefinitionId, playerIds);

            var gameSessionRequest = ServiceMessageFactory <CreateGameSessionRequest> .CreateFrom(request);

            gameSessionRequest.GameSession = gameSession.Convert();

            var gameSessionResponse = await gameSessionAccess.CreateGameSessionAsync(gameSessionRequest);

            if (!gameSessionResponse.HasErrors)
            {
                response.GameSession = gameSessionResponse.GameSession.Convert();
            }
            else
            {
                response.Errors += "Unable to create game session.";
                logger.LogError($"Unable to create game session ({response.GameSession.Id}).");
            }

            var tilesRequest = ServiceMessageFactory <CreateTilesRequest> .CreateFrom(request);

            var tiles = TicTacToeBoardFactory.Create(gameSession.Id, new[] { "A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3" });

            tilesRequest.Tiles = tiles.Convert();

            var tilesResponse = await tileAccess.CreateTilesAsync(tilesRequest);

            if (!tilesResponse.HasErrors)
            {
                return(response);
            }

            response.Errors += "Unable to create game tiles.";
            logger.LogError($"Unable to create game tiles for game session ({response.GameSession.Id}).");

            return(response);
        }