示例#1
0
 public void Map2DimensionalParamsToGameSimpleField_ParamsValid_AllParamsCorrectlyMapped(
     int i,
     int j,
     GameFieldFields field
     )
 {
     Assert.IsTrue(_gameService.Map2DimensionalParamsToGameSimpleField(i, j) == field);
 }
        public bool CanMakeMove(GameField field, GameFieldFields simpleField)
        {
            string fieldValue = (string)field.GetType()
                                .GetProperty(simpleField.ToString())
                                .GetValue(field, null);

            return(string.IsNullOrEmpty(fieldValue));
        }
        public async Task MakeMove(int i, int j, string playerId, string password)
        {
            int roomId = (int)Context.Items[RoomIdKey];
            var room   = await _roomService.GetRoomWithGameAndGameField(roomId);

            if (!_gameService.ValidatePlayer(room, playerId, password))
            {
                await _notifyCallerAccesDenied();

                return;
            }

            if (!_gameService.IsPlayerTurn(room.Game, playerId))
            {
                await Clients.Caller.NotYourTurn("It`s not your turn now!");

                return;
            }

            GameFieldFields simpleField = _gameService.Map2DimensionalParamsToGameSimpleField(i, j);

            if (!_gameService.CanMakeMove(room.Game.Field, simpleField))
            {
                await Clients.Caller.FieldAlreadyOccupied("This field is not empty");

                return;
            }

            _gameService.MakeMove(room.Game, simpleField);

            if (room.Game.Field.IsFull)
            {
                if (_gameService.IsWinner(room.Game.Field))
                {
                    await Clients.Caller.Win();

                    await Clients.OthersInGroup(roomId.ToString()).Lose();
                }
                else
                {
                    await Clients.Group(roomId.ToString()).Draw();
                }
            }
            else if (_gameService.IsWinner(room.Game.Field))
            {
                await Clients.Caller.Win();

                await Clients.OthersInGroup(roomId.ToString()).Lose();
            }

            _gameService.NextPlayerTurn(room);
            await _gameService.SaveChangesAsync();

            await Clients
            .Group(roomId.ToString())
            .PlayerMadeMove(i, j);
        }
        public void MakeMove(Game game, GameFieldFields simpleField)
        {
            GameField field = game.Field;

            field
            .GetType()
            .GetProperty(simpleField.ToString())
            .SetValue(field, game.CurrentPlayerId);
        }