Beispiel #1
0
        private void RunGame(int gameNumber, ChosenStartingConfiguration startingConfig)
        {
            var game = startingConfig.CreateGame(Players.Keys);
            var clients = new List<IGameClient>();

            var host = new LockingGameHost(game);

            foreach (var player in game.Players)
            {
                IGameClient client = new GameClient(player.Id, player.Name);
                clients.Add(client);

                var ai = _aiFactory.Create(Players[player.Name]);
                ai.Attach(client);

                host.RegisterGameClient(client, player);
            }

            host.AcceptMessage(new BeginGameMessage());

            var firstClient = clients.First();

            while (!firstClient.GetGameState().Status.GameIsComplete)
                Thread.Sleep(500);

            var state = firstClient.GetGameState();

            File.WriteAllText(Path.Combine(Name, string.Format("game_{0}.txt", gameNumber)), state.Log);

            _results[gameNumber] = state.Results;            
        }
Beispiel #2
0
 public void GivenANewGameWithPlayersAndBank(int playerCount, string bankCardNames)
 {
     var cardNameList = bankCardNames.Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries);
     var startingConfig = new ChosenStartingConfiguration(playerCount, cardNameList, false);
     var names = playerCount.Items(i => "Player" + i);
     _game = startingConfig.CreateGame(names);
 }
Beispiel #3
0
        public string CreateNewGame(IEnumerable<string> playerNames, int numberOfPlayers, IEnumerable<string> selectedCardNames, bool useProsperity)
        {
            var key = _gameData.Count.ToString();
            var startingConfig = new ChosenStartingConfiguration(numberOfPlayers, selectedCardNames, useProsperity);
            var game = startingConfig.CreateGame(playerNames);

            var host = new LockingGameHost(game);            
            _gameData[key] = new GameData { GameKey = key };

            int aiPlayerCount = 0;
            foreach (var player in game.Players)
            {
                IGameClient client = new GameClient(player.Id, player.Name);

                if (_aiTypeList.Select(x => x.Name).Contains(player.Name))
                {                    
                    var ai = (BaseAIClient) Activator.CreateInstance(_aiTypeList.Single(x => x.Name == player.Name));
                    ai.Attach(client);
                    string newName = string.Format("{0} (Comp {1})", player.Name, ++aiPlayerCount);
                    player.Rename(newName);
                }

                host.RegisterGameClient(client, player);
                _clients[player.Id] = client;
                _gameData[key].Slots[player.Id] = player.Name;
            }

            host.AcceptMessage(new BeginGameMessage());

            return key;
        }    
Beispiel #4
0
        public string CreateNewGame(IEnumerable <string> playerNames, int numberOfPlayers, IEnumerable <string> selectedCardNames, bool useProsperity)
        {
            var key            = _gameData.Count.ToString();
            var startingConfig = new ChosenStartingConfiguration(numberOfPlayers, selectedCardNames, useProsperity);
            var game           = startingConfig.CreateGame(playerNames);

            var host = new LockingGameHost(game);

            _gameData[key] = new GameData {
                GameKey = key
            };

            int aiPlayerCount = 0;

            foreach (var player in game.Players)
            {
                IGameClient client = new GameClient(player.Id, player.Name);

                if (_aiTypeList.Select(x => x.Name).Contains(player.Name))
                {
                    var ai = (BaseAIClient)Activator.CreateInstance(_aiTypeList.Single(x => x.Name == player.Name));
                    ai.Attach(client);
                    string newName = string.Format("{0} (Comp {1})", player.Name, ++aiPlayerCount);
                    player.Rename(newName);
                }

                host.RegisterGameClient(client, player);
                _clients[player.Id]             = client;
                _gameData[key].Slots[player.Id] = player.Name;
            }

            host.AcceptMessage(new BeginGameMessage());

            return(key);
        }
Beispiel #5
0
        public void Run(Action<Task<ResultsSummary>> onUpdateResults, Action<Task> onDone)
        {
            _results = new GameResultsViewModel[NumberOfGamesToExecute];
            var startingConfig = new ChosenStartingConfiguration(Players.Count, Cards, false);

            TaskScheduler syncContext = TaskScheduler.FromCurrentSynchronizationContext();

            Task[] tasks = new Task[NumberOfGamesToExecute];

            for (int i = 0; i < NumberOfGamesToExecute; i++)
            {
                int temp = i;
                tasks[i] = Task.Factory.StartNew(() => RunGame(temp, startingConfig))
                    .ContinueWith(t => CreateSummary())
                    .ContinueWith(onUpdateResults, syncContext);
            }

            Task.Factory
                .ContinueWhenAll(tasks, WriteResults)
                .ContinueWith(onDone, syncContext);
        }