Пример #1
0
        static void Main(string[] args)
        {
            game           = GameFactory.Instance.CreateGame(GameType.ghost, "Optimal Ghost");
            humanPlayer    = game.CreatePlayer("Donald Trump", PlayerType.human);
            computerPlayer = game.CreatePlayer("C3PO", PlayerType.perfectIa);
            game.AddPlayer(humanPlayer);
            game.AddPlayer(computerPlayer);

            Console.WriteLine(string.Format("Welcome to the '{0}' game.", game.Name));
            RestartGame();

            while (true)
            {
                ShowAnalysis();
                CheckIfTereIsAWinnerAndAskToLeave();

                Console.Write("$" + game.State.State + ": ");
                game.PlayNextTurn();
                if (game.State.CurrentPlayer == 0)
                {
                    // Has just moved the computer. Give som feedback in the screen
                    Console.Write(game.State.State[game.State.State.Length - 1]);
                    Console.WriteLine("");
                }
            }
        }
Пример #2
0
 private void WhiteBox_Click(object sender, EventArgs e)
 {
     WhiteBox.Location = new Point(391, 69);
     BlackBox.Location = new Point(391, 204);
     _game.AddPlayer(PlayerName.PlayerOne, Color.White);
     _game.AddPlayer(PlayerName.PlayerTwo, Color.Black);
     PrepareToStartGame();
 }
Пример #3
0
 public void MakeGenerationZero()
 {
     Debug.Log("Making Generation Zero");
     for (int i = 0; i < populationSize; i++)
     {
         Simulation simulation = game.AddSimulation();
         game.AddPlayer(simulation);
     }
 }
Пример #4
0
        /// <summary>
        /// Started program method
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            BoardUI.PrintWelcomeMessage();

            MenuEnum option;

            do
            {
                option = BoardUI.PrintAndReadMenuOptions(Game.GetMenuOptions());

                switch (option)
                {
                case MenuEnum.StartGame:
                    StartGame();
                    BoardUI.PrintWelcomeMessage();
                    break;

                case MenuEnum.ListPlayers:
                    BoardUI.PrintPlayers(Game.GetPlayers());
                    break;

                case MenuEnum.AddPlayer:
                    Game.AddPlayer(BoardUI.ReadPlayer(Game.GetPlayers()));
                    break;

                case MenuEnum.ListItems:
                    BoardUI.PrintItems(Game.GetItems());
                    break;

                case MenuEnum.AddItem:
                    Game.AddItem(BoardUI.ReadItem(Game.GetItems()));
                    break;

                case MenuEnum.RemoveItem:
                    Game.RemoveItem(BoardUI.ReadRemoveItem(Game.GetItems()));
                    break;

                case MenuEnum.RemovePlayer:
                    Game.RemovePlayer(BoardUI.ReadRemovePlayer(Game.GetPlayers()));
                    break;

                case MenuEnum.DefaultPlayers:
                    SetTestPlayers();
                    break;

                case MenuEnum.ResetGame:
                    Game = new Game();
                    break;

                default:
                    break;
                }
            } while (!option.Equals(MenuEnum.EndGame));

            BoardUI.PrintByeByeMessage();
        }
Пример #5
0
        public void EnsureMessageIsAddedIfBothPartiesArePresent()
        {
            IGame game = TestGame;

            game.AddPlayer(new Player("ABC", "abc", false));
            game.AddPlayer(new Player("DEF", "def", false));

            game.SendMessage("abc", "def", "hello world");

            Assert.Single(game.AllMessages);
        }
Пример #6
0
        public void JoinGameAsPlayer(User userToJoin, IGame gameToJoin)
        {
            var properties = gameToJoin.GetProperties();

            if (gameToJoin.GetProperties().IsClosed)
            {
                var msg = "The game " + properties.GameIdentifier + " to join was closed!";
                ErrorLogger.Error(msg);
                throw new InvalidExpressionException(msg);
            }

            var userState = userToJoin.GetPlayerState();

            if (userState == PlayerState.InLobby)
            {
                JoinGame(userToJoin, gameToJoin);
                userToJoin.ChangePlayerState(PlayerState.Player);
                gameToJoin.AddPlayer(userToJoin);
            }
            else
            {
                var msg = "User " + userToJoin.PlayerName + "[" + userState + "] can't join the game: " + gameToJoin.GetProperties().GameIdentifier;
                ErrorLogger.Error(msg);
                throw new InvalidOperationException(msg);
            }
        }
Пример #7
0
    public override void StartGameController()
    {
        Simulation simulation = game.AddSimulation();

        game.AddPlayer(simulation);
        StartCoroutine(game.StartAllSimulations());
    }
        public void Given_A_Game()
        {
            var factory    = new PlayerModelFactory(OneFrame, NumberOfPins);
            var repository = new PlayerRepository(factory);

            _game = new Game(repository, OneFrame);
            _game.AddPlayer(TheDude);
        }
Пример #9
0
        public GameCreator WithPlayers(int playerCount)
        {
            for (int currentPlayer = 0; currentPlayer < playerCount; currentPlayer++)
            {
                _game.AddPlayer(new Player());
            }

            return(this);
        }
Пример #10
0
        public void FindGameMasterReturnsGameMasterWhenEntered()
        {
            IGame game = TestGame;

            game.AddPlayer(new Player("ThaGameMaster", "abc123", true));

            var gm = game.GameMaster;

            Assert.NotNull(gm);
        }
Пример #11
0
 public void AddPlayer(PlayerViewModel player)
 {
     _game.AddPlayer(player.displayName, player.imageUrl, Context.ConnectionId, player.id);
     //var card = _game.CurrentlyExtractedCard();
     //var symbols = GetSymbols(card);
     //Clients.Client(Context.ConnectionId).joinGame(symbols, GetSymbols(_game.GetPlayerCurrentCard(player.id)), GetPlayers().First(p  => p.id == player.id));
     //foreach (var p in _game.GetPlayers())
     //{
     //    Clients.Client(p.ConnectionId).setPlayers(GetPlayers());
     //}
 }
Пример #12
0
        private static void PreGame(IGame game)
        {
            Console.Write("enter the number of players: ");
            var line = Console.ReadLine();

            for (var i = 1; i <= Convert.ToInt32(line); i++)
            {
                Console.Write("enter player #{0}'s name: ", i);
                var name = Console.ReadLine();
                game.AddPlayer(name);
            }
        }
Пример #13
0
        public override async Task OnConnectedAsync()
        {
            var playerId = Context.User.FindFirst("Id").Value;
            var player   = new GamePlayer()
            {
                Id = playerId
            };

            await _game.AddPlayer(player);

            await base.OnConnectedAsync();
        }
Пример #14
0
        public int JoinGame(string username, int gameID)
        {
            IGame game = GetGameById(gameID);

            if (game.IsSpectatorExist(username))
            {
                throw new AlreadyParticipatingException("The user " + username + " is already spectating game #" + gameID);
            }
            User   user   = userController.GetUserByName(username);
            Player player = new Player(game, 0, username);

            game.AddPlayer(user, player);
            return(player.PlayerId);
        }
Пример #15
0
 public bool JoinUserToFirstActiveGame(User user, State state)
 {
     try
     {
         IGame client          = _game.GetAllActiveGames()[0];
         UserManagement.User U = new UserManagement.User(user.UserName, user.Password, 0);
         U.SetMoney(100);
         U.ChangePlayerState(PlayerState.Player);
         client.AddPlayer(U);
         _game.JoinGameAsPlayer(U, client);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #16
0
        public async Task <IGame> JoinGame(IGame game, IPlayer player)
        {
            if (game == null)
            {
                throw new ArgumentNullException(nameof(game));
            }
            if (player == null)
            {
                throw new ArgumentNullException(nameof(player));
            }

            game.AddPlayer(player);

            await SaveGame(game);

            return(game);
        }
Пример #17
0
 public void AddPlayer(User user, Player player)
 {
     MyGame.AddPlayer(user, player);
 }
Пример #18
0
        public void Run()
        {
            Console.WriteLine("Starting the \"{0}\" Game(s) Server on port {1}.", Name, Port);
            Console.WriteLine("Press Ctrl-C to shutdown the server at any time.");

            _nextGame = new GuessMyNumberGame(this);

            _listener.Start();
            Running = true;
            List <Task> newConnectionTasks = new List <Task>();

            Console.WriteLine("Waiting for incomming connections...");

            while (Running)
            {
                if (_listener.Pending())
                {
                    newConnectionTasks.Add(_handleNewConnection());
                }

                if (_waitingLobby.Count >= _nextGame.RequiredPlayers)
                {
                    int numPlayers = 0;
                    while (numPlayers < _nextGame.RequiredPlayers)
                    {
                        TcpClient player = _waitingLobby[0];
                        _waitingLobby.RemoveAt(0);

                        if (_nextGame.AddPlayer(player))
                        {
                            numPlayers++;
                        }
                        else
                        {
                            _waitingLobby.Add(player);
                        }
                    }

                    Console.WriteLine("Starting a \"{0}\" game.", _nextGame.Name);
                    Thread gameThread = new Thread(new ThreadStart(_nextGame.Run));
                    gameThread.Start();
                    _games.Add(_nextGame);
                    _gameThreads.Add(gameThread);

                    _nextGame = new GuessMyNumberGame(this);
                }

                foreach (TcpClient client in _waitingLobby.ToArray())
                {
                    EndPoint endPoint     = client.Client.RemoteEndPoint;
                    bool     disconnected = false;

                    Packet p = ReceivePacket(client).GetAwaiter().GetResult();
                    disconnected = (p?.Command == "bye") || IsDisconnected(client);

                    if (disconnected)
                    {
                        HandleDisconnectedClient(client);
                        Console.WriteLine("Client {0} has disconnected from the Game(s) Server.", endPoint);
                    }
                }

                Thread.Sleep(10);
            } // while running

            Task.WaitAll(newConnectionTasks.ToArray(), 1000);

            foreach (Thread thread in _gameThreads)
            {
                thread.Abort();
            }

            Parallel.ForEach(_clients, (client) =>
            {
                DisconnectClient(client, "The Game(s) Server is being shutdown.");
            });

            _listener.Stop();

            Console.WriteLine("The server has been shut down.");
        }
Пример #19
0
        // The main loop for the games server
        public void Run()
        {
            Console.WriteLine($"Starting the \"{Name}\" Game(s) Server on port {Port}");
            Console.WriteLine("Press Ctrl-C to shutdown the server at any time.");

            // Start the next game
            _nextGame = new GuessMyNumberGame(this);

            // Start running the server
            _listener.Start();
            Running = true;
            List <Task> newConnectionTasks = new List <Task>();

            Console.WriteLine("Waiting for incoming connections...");

            while (Running)
            {
                // Handle any new clients
                if (_listener.Pending())
                {
                    newConnectionTasks.Add(_handleNewConnections());
                }

                // Once we have enough clients for the next game, add them in and start the game
                if (_waitingLobby.Count >= _nextGame.RequiredPlayers)
                {
                    // Get that many players from the waiting lobby and start the game
                    int numPlayers = 0;
                    while (numPlayers < _nextGame.RequiredPlayers)
                    {
                        // Pop the first one off
                        TcpClient player = _waitingLobby[0];
                        _waitingLobby.RemoveAt(0);

                        // Try adding it to the game. If failure, put it back in the lobby
                        if (_nextGame.AddPlayer(player))
                        {
                            numPlayers++;
                        }
                        else
                        {
                            _waitingLobby.Add(player);
                        }
                    }

                    // Start the game in a new thread!
                    Console.WriteLine($"Starting a \"{_nextGame.Name}\" game.");
                    Thread gameThread = new Thread(new ThreadStart(_nextGame.Run));
                    gameThread.Start();
                    _games.Add(_nextGame);
                    _gameThreads.Add(gameThread);

                    // Create a new game
                    _nextGame = new GuessMyNumberGame(this);
                }

                // Check if any clients have disconnected in wiating, gracefully or not
                // This should be parallelized
                foreach (TcpClient client in _waitingLobby.ToArray())
                {
                    EndPoint endPoint     = client.Client.RemoteEndPoint;
                    bool     disconnected = false;

                    // Check for graceful disconnect first
                    Packet pack = ReceivePacket(client).GetAwaiter().GetResult();
                    disconnected = (pack?.Command == "bye");

                    // Then ungraceful
                    disconnected |= IsDisconnected(client);

                    if (disconnected)
                    {
                        HandleDisconnectedClient(client);
                        Console.WriteLine($"Client {endPoint} has disconnected from the Game(s) Server.");
                    }
                }

                // Take a short nap
                Thread.Sleep(10);
            }

            // Give time for clients that just connected to finish connection.
            Task.WaitAll(newConnectionTasks.ToArray(), 1000);

            // Shutdown all of the threads, whether they are done or not
            foreach (Thread thread in _gameThreads)
            {
                thread.Abort();
            }

            // Disconnect any clients still here
            Parallel.ForEach(_clients, (client) =>
            {
                DisconnectClient(client, "The Game(s) Server is being shutdown.");
            });

            // Cleanup our resources
            _listener.Stop();

            // Info
            Console.WriteLine("The server has been shut down.");
        }
Пример #20
0
        public void Register(IPlayer player, IGame game)
        {
            game.AddPlayer(player);

            game.Announce(player.Name + " has joined the adventure!");
        }
 private void PrepareGame()
 {
     game.AddPlayer(PlayerA);
     game.AddPlayer(PlayerB);
 }
Пример #22
0
 public void AddPlayer()
 {
     game.AddPlayer();
 }
Пример #23
0
 public void AddPlayer() => _game.AddPlayer();