Example #1
0
        void ServerMain(object arg)
        {
            EventWaitHandle serverStartWaitHandle = (EventWaitHandle)arg;

            Thread.CurrentThread.Priority = ThreadPriority.Lowest;
            Thread.CurrentThread.Name     = "SMain";

            if (m_save == Guid.Empty)
            {
                UpdateStatus("Creating Game");
                m_game = m_gameFactory.CreateGame(m_saveManager.GameDir, m_gameOptions);
                UpdateStatus("Game Created");
            }
            else
            {
                UpdateStatus("Loading Game");
                m_game = m_gameFactory.LoadGame(m_saveManager.GameDir, m_save);
                UpdateStatus("Game Loaded");
            }

            UpdateStatus("Starting Game");

            m_game.Run(serverStartWaitHandle);

            m_game = null;

            if (m_serverDomain != null)
            {
                AppDomain.Unload(m_serverDomain);
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        private async Task <float> EvaluateAsync(SnakeIndividual individual)
        {
            var game = _gameFactory.CreateGame();
            await game.Run(individual);

            return(game.Score);
        }
Example #3
0
        private static IGame CreateGame(IGameConfig config)
        {
            var game = GameFactory.CreateGame(config);

            game.Finished += OnGameFinished;
            game.StepDone += OnStepDone;

            return(game);
        }
        public Round[] CreateRoundsPerTournament(Tournament tournament)
        {
            var rounds = new List <Round>();

            for (var i = 0; i < tournament.Teams.Count - 1; i++)
            {
                for (var j = i + 1; j < tournament.Teams.Count; j++)
                {
                    var team1 = tournament.Teams.ElementAt(i);
                    var team2 = tournament.Teams.ElementAt(j);

                    var game1 = _gameFactory.CreateGame(team1, team2);
                    var game2 = _gameFactory.CreateGame(team2, team1);

                    AddGameToRound(rounds, game1);
                    AddGameToRound(rounds, game2);
                }
            }

            return(rounds.ToArray());
        }
Example #5
0
        public async Task Act(string gameId, string userId, string cmd)
        {
            var gameState = await _gameBoardCache.Get(gameId);

            if (gameState != null)
            {
                var game = _gameFactory.CreateGame(gameState.GetType());
                game.GameBoard    = gameState;
                game.TextRenderer = _rendererFactory.Get(gameId);
                if (game.IsMatch(cmd))
                {
                    game.Act(userId, cmd);
                    if (game.GameBoard.IsGameOver())
                    {
                        await _gameBoardCache.Remove(gameId);
                    }
                    else
                    {
                        await _gameBoardCache.Set(gameId, game.GameBoard);
                    }
                }
                else if (cmd == "我認輸了")
                {
                    game.GameOver();
                    await _gameBoardCache.Remove(gameId);
                }
            }
            else
            {
                if (IsCreateGameCmd(cmd))
                {
                    var gameType = _cmdGameTypeDict[cmd];
                    var game     = _gameFactory.CreateGame(gameType);
                    game.TextRenderer = _rendererFactory.Get(gameId);
                    await _gameBoardCache.Set(gameId, game.GameBoard);

                    game.StartGame();
                }
            }
        }
Example #6
0
        public async Task <string> CreateGame(string gameType)
        {
            using (logger.BeginScope("Create game {gameType}", gameType))
            {
                var gameId = Guid.NewGuid().ToString();
                var game   = gameFactory.CreateGame(gameType);
                CreateGame(gameId, game);
                await Task.Yield();

                logger.LogInformation("Created {gameType} game with id {gameId}", gameType, gameId);
                return(gameId);
            }
        }
Example #7
0
        public void StartGame(StartNewGameCommand command)
        {
            Clients.All.SendAsync("StartGameMsgReceived");

            var game = _gameFactory.CreateGame(this, command.RowCount, command.ColumnCount);

            game.StartGame();

            for (var i = 0; i < command.GenCount; i++)
            {
                Thread.Sleep(500);
                game.NextGen();
            }

            Clients.All.SendAsync("GameOver");
        }
Example #8
0
        public async Task <IGame> StartGame(Guid userId)
        {
            EnsureUserIsOnline(userId);
            EnsureGameIsNotStarted();

            if (_state.Players.Count < 2)
            {
                throw UserException.Create(
                          GameplayError.NotEnoughPlayers,
                          "At least 2 players are required to start the game.");
            }

            _state.IsGameStarted = true;
            var game = await _gameFactory.CreateGame(userId);

            return(game);
        }
        public void CreateGameShouldDoIt()
        {
            // Arrange
            var home = new Team {
                Id = 1
            };
            var guest = new Team {
                Id = 2
            };

            // Act
            var game = _factory.CreateGame(home, guest);

            // Assert
            game.GuestTeam.ShouldBe(guest);
            game.GuestTeamId.ShouldBe(guest.Id);
            game.HomeTeam.ShouldBe(home);
            game.HomeTeamId.ShouldBe(home.Id);
            game.Result.ShouldBeNull();
        }
Example #10
0
 public IGame CreateGame(IGameFactory gameFactory, GameType type, IBoard board)
 {
     CurrentGame = gameFactory.CreateGame(type, board);
     return CurrentGame;
 }
        public void StartGame()
        {
            Console.WriteLine("Rock, Paper, Scissors game");

            Console.WriteLine("Please specify your name:");
            string playerName = Console.ReadLine();


            if (!_playerInputValidator.IsNameValid(playerName))
            {
                Console.WriteLine("Invalid name");
                return;
            }

            var game = _gameFactory.CreateGame(
                _humanPlayerFactory.Create(playerName),
                _computerPlayerFactory.Create());

            int roundsCounter = 1;

            while (roundsCounter <= NumberOfRounds)
            {
                Console.WriteLine($"Specify your choice: { Environment.NewLine }" +
                                  $"1.{ Choice.Paper } { Environment.NewLine }" +
                                  $"2.{ Choice.Rock } { Environment.NewLine }" +
                                  $"3.{ Choice.Scissors } { Environment.NewLine }");

                string input = Console.ReadLine();

                if (_playerInputValidator.IsChoiceValid(input))
                {
                    var humanPlayerChoice = (Choice)Convert.ToInt32(input);
                    game.SetHumanPlayerChoice(humanPlayerChoice);
                }
                else
                {
                    Console.WriteLine($"Invalid choice, try again { Environment.NewLine }");
                    continue;
                }

                var computerChoice = game.GenerateChoice();
                game.SetComputerPlayerChoice(computerChoice);

                Console.WriteLine($"Computer choice: { game.GetComputerPlayerChoice }");
                Console.WriteLine($"{ playerName } choice: { game.GetHumanPlayerChoice }");

                var roundResult = game.SelectWinner(game.GetHumanPlayerChoice, game.GetComputerPlayerChoice);
                var roundWinner = game.GetRoundWinner(roundResult);

                if (roundWinner == null)
                {
                    Console.WriteLine("No winner, tie");
                }
                else
                {
                    game.AddPointToTheWinner(roundWinner);

                    Console.WriteLine($"{ roundWinner.Name } wins round { roundsCounter } { Environment.NewLine }");

                    roundsCounter++;
                }
            }

            var winner = game.GetGameWinner();

            Console.WriteLine($"The winner is { winner.Name }");
        }
Example #12
0
 public void Reset()
 {
     CurrentGame = _gameFactory.CreateGame(this);
 }
Example #13
0
 private void GivenMockGame()
 {
     _mockGame = Substitute.For <Game>();
     _gameFactory.CreateGame(Arg.Any <Type>()).Returns(_mockGame);
 }