Ejemplo n.º 1
0
        /// <summary>
        /// A new player join the game session
        /// </summary>
        /// <param name="id"></param>
        public void Join(string id)
        {
            CheckCurrentState(EnumGameSessionState.WaitingForPlayers, "join is only possible during WaitingForPlayers state");

            const int BOARD_SIZE = 10;

            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException("invalid player id");
            }

            if (P1 == null)
            {
                P1 = playerFactory.New(id, BOARD_SIZE);
            }
            else
            {
                if (id.ToLower() == P1.ID.ToLower())
                {
                    throw new ArgumentException("A player cannot play with him/her self");
                }

                P2    = playerFactory.New(id, BOARD_SIZE);
                State = EnumGameSessionState.WaitingPlayerConfirmation;
            }
        }
Ejemplo n.º 2
0
 private void CheckCurrentState(EnumGameSessionState state, string message)
 {
     if (State != state)
     {
         throw new ArgumentException(message);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a new instance of GameSession
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pf"></param>
        public GameSession(string id, IPlayerFactory pf)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentException("invalid game session id");
            }

            ID            = id;
            State         = EnumGameSessionState.Created;
            playerFactory = pf ?? throw new ArgumentNullException(nameof(pf));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Signal when player is ready
        /// </summary>
        /// <param name="playerId"></param>
        public void Ready(string playerId)
        {
            CheckCurrentState(EnumGameSessionState.WaitingPlayerConfirmation, "game Session is not waiting for player confirmation");

            var player = GetRequiredPlayer(playerId);

            GetReady(player);

            //When both ready, start the game
            if (P1.Ready && P2.Ready)
            {
                State   = EnumGameSessionState.Playing;
                Current = P1;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Register player shoot
        /// </summary>
        /// <param name="playerId"></param>
        /// <param name="pos"></param>
        public BoardShip Shoot(string playerId, Coordinates pos)
        {
            CheckCurrentState(EnumGameSessionState.Playing, "invalid game state");

            if (pos == null)
            {
                throw new ArgumentNullException(nameof(pos));
            }

            if (string.IsNullOrEmpty(playerId))
            {
                throw new ArgumentException("invalid player id");
            }

            if (Current.ID != playerId)
            {
                throw new InvalidOperationException("invalid player turn");
            }

            var opponent = Current.ID == P1.ID ? P2 : P1;
            var target   = opponent.Board.Get(pos);
            var result   = null as BoardShip;

            if (target != null)
            {
                result = opponent.Board.FindShipById(target.ID);

                Current.Points++;
                opponent.Board.Remove(target);
            }

            //Is it a game over?
            if (opponent.Board.Fleet.Count() == 0)
            {
                State  = EnumGameSessionState.Finished;
                Winner = Current;
            }

            //Turn changes
            Current = opponent;

            return(result);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a game session
 /// </summary>
 public void Init()
 {
     State = EnumGameSessionState.WaitingForPlayers;
 }