Ejemplo n.º 1
0
        protected override Result Apply(ref GoFishGame game)
        {
            ResultBuilder resultBuilder = new ResultBuilder();

            if (game.Deck.TryDraw(out Card card))
            {
                game.CurrentPlayer.Cards.Add(card);
                resultBuilder.AddFeedback($"{game.CurrentPlayer.Username} drew a card from the deck.");


                string      collection          = card.Collection;
                ISet <Card> cardsFromCollection = new HashSet <Card>(game.CurrentPlayer.Cards.Where(c => c.Collection.Equals(collection)));
                if (cardsFromCollection.Count >= 4)
                {
                    game.CurrentPlayer.FinishedCollections.Add(collection, cardsFromCollection);
                    game.CurrentPlayer.Cards.ExceptWith(cardsFromCollection);
                    resultBuilder.AddFeedback($"{game.CurrentPlayer.Username} finished the {collection} collection!");
                }
            }

            game.CurrentPlayer = _to;
            resultBuilder.AddFeedback($"The turn was passed to {game.CurrentPlayer.Username}");

            return(resultBuilder.Build());
        }
Ejemplo n.º 2
0
        protected override Result Apply(ref GoFishGame game)
        {
            var resultBuilder = new ResultBuilder();

            switch (game.GameState)
            {
            case GameState.Waiting:
                game.Players.Remove(_player);
                resultBuilder.AddFeedback($"{_player.Username} left the game.");
                break;

            case GameState.Playing:
                Player gameplayer = game.Players.First(p => p.Equals(_player));
                foreach (Card card in gameplayer.Cards.Concat(gameplayer.FinishedCollections.SelectMany(c => c.Value)))
                {
                    game.Deck.InsertAt(game.Deck.Count - 1, card);
                }
                game.Deck.Shuffle();
                game.Players.Remove(gameplayer);
                resultBuilder.AddFeedback($"{_player.Username} left the game.");
                break;

            case GameState.Finished:
                break;

            default:
                throw new Exception("The given option for GameState is unknown.");
            }

            return(resultBuilder.Build());
        }
Ejemplo n.º 3
0
        public override Result Validate(GoFishGame game)
        {
            if (game is null)
            {
                throw new ArgumentNullException(nameof(game));
            }

            ResultBuilder resultBuilder = new ResultBuilder();

            // cannot pass the turn if the game is not running
            if (game.GameState != GameState.Playing)
            {
                resultBuilder.AddFeedback("Turns can only be passed of the game is in the 'playing' state.", false);
            }

            // cannot pass the turn if you are not the current player
            if (!_from.Equals(game.CurrentPlayer))
            {
                resultBuilder.AddFeedback($"{_from.Username} cannot pass the turn because it's not their turn.", false);
            }

            // can only pass the turn according to the rules
            switch (game.RuleSet.TurnBehaviour)
            {
            case TurnBehaviour.ClockWise:

                // can not pass the turn to a player that is not the next
                if (!_to.Equals(game.NextPlayer))
                {
                    resultBuilder.AddFeedback($"The turn cannot be passed to {_to.Username}, because they're not the next clockwise player.", false);
                }
                break;

            case TurnBehaviour.LastAskedPlayer:
                // cannot pass the turn to somebody who is not in the game
                if (!game.Players.Contains(_to))
                {
                    resultBuilder.AddFeedback("The turn can only be passed to a player that actually participates in this game.", false);
                }

                // cannot pass the turn to yourself
                if (_to.Equals(_from))
                {
                    resultBuilder.AddFeedback("The turn must be passed to a different player.", false);
                }

                // cannot pass the turn to somebody who has no cards
                if (_to.Cards.Count == 0)
                {
                    resultBuilder.AddFeedback("The turn cannot be passed to a player who has no cards.", false);
                }
                break;

            default:
                throw new Exception("The given option for TurnBehaviour is unknown.");
            }

            return(resultBuilder.Build());
        }
Ejemplo n.º 4
0
        public override Result Validate(GoFishGame game)
        {
            if (game is null)
            {
                throw new ArgumentNullException(nameof(game));
            }

            ResultBuilder resultBuilder = new ResultBuilder();

            // You can only give cards when the game is running
            if (game.GameState != GameState.Playing)
            {
                resultBuilder.AddFeedback("Cards can only be given when the game is in the 'playing' state.", false);
            }

            // You can only give cards if it is not your turn
            if (_player.Equals(game.CurrentPlayer))
            {
                resultBuilder.AddFeedback($"{_player.Username} cannot give any cards away because it's their turn to ask.", false);
            }

            // You can only give cards that you have
            if (!_cards.IsSubsetOf(_player.Cards))
            {
                resultBuilder.AddFeedback($"{_player.Username} cannot give away these cards, because one or more are not in their possession.", false);
            }

            // You can only give cards according to the card giving rule
            switch (game.RuleSet.GiveCardBehaviour)
            {
            case GiveCardBehaviour.AllOfCollection:
                string collection = _cards.First().Collection;

                // Cards must all be from the same collection
                if (!_cards.All(c => c.Collection.Equals(collection)))
                {
                    resultBuilder.AddFeedback($"Only cards from the same collection can be given away at once.", false);
                }

                // All cards of the same collection must be given
                if (_player.Cards.Any(c => c.Collection.Equals(collection) && !_cards.Contains(c)))
                {
                    resultBuilder.AddFeedback($"All cards from the same collection must be given away at once.", false);
                }
                break;

            case GiveCardBehaviour.Single:
                if (_cards.Count != 1)
                {
                    resultBuilder.AddFeedback($"Only 1 card can be given away at once.", false);
                }
                break;

            default:
                throw new Exception("The given option for GiveCardBehaviour is unknown.");
            }

            return(resultBuilder.Build());
        }
Ejemplo n.º 5
0
        protected override Result Apply(ref GoFishGame game)
        {
            ResultBuilder resultBuilder = new ResultBuilder();

            game.Players.Add(new Player(_guid, _username));
            resultBuilder.AddFeedback($"{_username} joined the game.");

            return(resultBuilder.Build());
        }
Ejemplo n.º 6
0
        public Result Join(string username)
        {
            JoinGameAction gameAction = new JoinGameAction(_userContextProvider.UserId, username);

            GoFishGame game   = _gameAccessor.Game;
            Result     result = _gameManager.PerformAction(gameAction, ref game);

            return(result);
        }
Ejemplo n.º 7
0
        public Result Start()
        {
            StartGameAction gameAction = new StartGameAction();

            GoFishGame game   = _gameAccessor.Game;
            Result     result = _gameManager.PerformAction(gameAction, ref game);

            return(result);
        }
Ejemplo n.º 8
0
        public void Validate_PlayerIsCurrent_ReturnsFalse()
        {
            GoFishGame          game   = GameProvider.Game;
            Player              player = game.Players[0];
            GiveCardsGameAction ga     = new GiveCardsGameAction(new HashSet <Card>(player.Cards.Take(1)), player);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 9
0
        public GoFishGame Create(Settings settings)
        {
            GoFishGame result = new GoFishGame
            {
                RuleSet = settings.RuleSet,
                Deck    = _deckFactory.Create(settings.DeckSettings)
            };

            return(result);
        }
Ejemplo n.º 10
0
        public void GameManager_ValidTurn_PerformsTurn()
        {
            ConstantValidityGameAction action = new ConstantValidityGameAction(true);
            GameManager <GoFishGame>   gm     = new GameManager <GoFishGame>();
            GoFishGame game = GameProvider.Game;

            gm.PerformAction(action, ref game);

            Assert.IsTrue(action.IsApplied);
        }
Ejemplo n.º 11
0
        public void GameManager_InvalidTurn_ThrowsInvalidOperationException()
        {
            ConstantValidityGameAction action = new ConstantValidityGameAction(false);
            GameManager <GoFishGame>   gm     = new GameManager <GoFishGame>();
            GoFishGame game = GameProvider.Game;

            Result result = gm.PerformAction(action, ref game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 12
0
        private async Task SendGameChange(Result result, GoFishGame game)
        {
            GameViewModel model = _gameMapper.Map(game);

            foreach (KeyValuePair <string, Guid> user in _connectedUsers)
            {
                Player player = game.Players.FirstOrDefault(p => p.Id.Equals(user.Value));
                model.UserCards = !(player is null) ? _cardMapper.MapRange(player.Cards) : null;
                await _gameHub.Clients.Client(user.Key).SendAsync("ReceiveGameChange", result, model);
            }
        }
Ejemplo n.º 13
0
        public override Result Validate(GoFishGame game)
        {
            ResultBuilder resultBuilder = new ResultBuilder();

            if (!game.Players.Contains(_player))
            {
                resultBuilder.AddFeedback($"{_player.Username} cannot leave this game because they're not participating.", false);
            }

            return(resultBuilder.Build());
        }
Ejemplo n.º 14
0
        public void Validate_PlayerIsNotcurrent_ReturnsFalse()
        {
            GoFishGame         game = GameProvider.Game;
            Player             from = game.Players[1];
            Player             to   = game.Players[2];
            PassTurnGameAction ga   = new PassTurnGameAction(from, to);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 15
0
        public void Validate_PlayerDoesNotHaveCard_ReturnsFalse()
        {
            GoFishGame          game   = GameProvider.Game;
            Player              player = game.Players[1];
            HashSet <Card>      cards  = new HashSet <Card>(player.Cards.Skip(1).Append(game.Deck.First()));
            GiveCardsGameAction ga     = new GiveCardsGameAction(cards, player);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 16
0
        public void Validate_NotallOfSameCollection_ReturnsFalse()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.GiveCardBehaviour = GiveCardBehaviour.AllOfCollection;
            Player player          = game.Players[1];
            GiveCardsGameAction ga = new GiveCardsGameAction(player.Cards, player);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 17
0
        public void Validate_ValidSingle_ReturnsTrue()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.GiveCardBehaviour = GiveCardBehaviour.Single;
            Player              player = game.Players[1];
            HashSet <Card>      cards  = new HashSet <Card>(player.Cards.Where(c => c.Collection == "C4").Take(1));
            GiveCardsGameAction ga     = new GiveCardsGameAction(cards, player);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsTrue(result.Success);
        }
Ejemplo n.º 18
0
        public void Validate_LastAskedPlayerIsSelf_ReturnsFalse()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.TurnBehaviour = TurnBehaviour.LastAskedPlayer;
            Player             from = game.CurrentPlayer;
            Player             to   = from;
            PassTurnGameAction ga   = new PassTurnGameAction(from, to);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 19
0
        public void Validate_ClockwiseValid_ReturnsTrue()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.TurnBehaviour = TurnBehaviour.ClockWise;
            Player             from = game.CurrentPlayer;
            Player             to   = game.NextPlayer;
            PassTurnGameAction ga   = new PassTurnGameAction(from, to);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsTrue(result.Success);
        }
Ejemplo n.º 20
0
        public void Apply_ValidAction_SetsCurrentPlayerToNewPlayer()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.TurnBehaviour = TurnBehaviour.LastAskedPlayer;
            Player             from = game.CurrentPlayer;
            Player             to   = game.Players[2];
            PassTurnGameAction ga   = new PassTurnGameAction(from, to);

            ga.Perform(ref game);

            Assert.AreEqual(to, game.CurrentPlayer);
        }
Ejemplo n.º 21
0
        public void Validate_AllOfCollectionNotAllGiven_ReturnsFalse()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.GiveCardBehaviour = GiveCardBehaviour.AllOfCollection;
            Player              player = game.Players[1];
            HashSet <Card>      cards  = new HashSet <Card>(player.Cards.Where(c => c.Collection.Equals("C4")).Take(1));
            GiveCardsGameAction ga     = new GiveCardsGameAction(cards, player);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 22
0
        public Result Give(IEnumerable <CardViewModel> cards)
        {
            GoFishGame game         = _gameAccessor.Game;
            Guid       sourceGuid   = _userContextProvider.UserId;
            Player     sourcePlayer = game.Players.FirstOrDefault(p => p.Id.Equals(sourceGuid));

            HashSet <Card> cardset = new HashSet <Card>(cards.Select(c1 => sourcePlayer.Cards.FirstOrDefault(c => c.Collection.Equals(c1.Collection) && c.Index.Equals(c1.Index))));

            GiveCardsGameAction gameAction = new GiveCardsGameAction(cardset, sourcePlayer);

            Result result = _gameManager.PerformAction(gameAction, ref game);

            return(result);
        }
Ejemplo n.º 23
0
        public Result Pass(Guid id)
        {
            GoFishGame game = _gameAccessor.Game;

            Guid   fromGuid = _userContextProvider.UserId;
            Player from     = game.Players.FirstOrDefault(p => p.Id.Equals(fromGuid));
            Player to       = game.Players.FirstOrDefault(p => p.Id.Equals(id));

            PassTurnGameAction gameAction = new PassTurnGameAction(from, to);

            Result result = _gameManager.PerformAction(gameAction, ref game);

            return(result);
        }
Ejemplo n.º 24
0
        public void Apply_Valid_GivesCardToCurrentPlayer()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.GiveCardBehaviour = GiveCardBehaviour.Single;
            Player              player = game.Players[1];
            HashSet <Card>      cards  = new HashSet <Card>(player.Cards.Where(c => c.Collection == "C4").Take(1));
            GiveCardsGameAction ga     = new GiveCardsGameAction(cards, player);

            ga.Perform(ref game);

            CollectionAssert.Contains(game.CurrentPlayer.Cards, cards.First());
            CollectionAssert.DoesNotContain(game.Players[1].Cards, cards.First());
        }
Ejemplo n.º 25
0
        public void Validate_LastAskedPlayerHasNoCards_ReturnsFalse()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.TurnBehaviour = TurnBehaviour.LastAskedPlayer;
            Player from = game.CurrentPlayer;
            Player to   = game.Players[2];

            to.Cards = new HashSet <Card>();
            PassTurnGameAction ga = new PassTurnGameAction(from, to);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 26
0
        public async Task Initialise()
        {
            Guid          userId = _userContextProvider.UserId;
            GoFishGame    game   = _gameService.Get();
            GameViewModel model  = _gameMapper.Map(game);

            if (model != null)
            {
                Player player = game.Players.FirstOrDefault(p => p.Id.Equals(_userContextProvider.UserId));
                model.UserCards = !(player is null) ? _cardMapper.MapRange(player.Cards) : null;
            }

            await Clients.Caller.SendAsync("ReceiveGameData", userId, model);

            await _userActivityEvent.TriggerAsync(this, new UserActivity(userId));
        }
Ejemplo n.º 27
0
        public void Validate_LastAskedPlayerNotInTheGame_ReturnsFalse()
        {
            GoFishGame game = GameProvider.Game;

            game.RuleSet.TurnBehaviour = TurnBehaviour.LastAskedPlayer;
            Player from = game.CurrentPlayer;
            Player to   = new Player(Guid.NewGuid(), "Test")
            {
                Cards = CardProvider.HashSetCards
            };
            PassTurnGameAction ga = new PassTurnGameAction(from, to);

            Game.Lib.Result result = ga.Validate(game);

            Assert.IsFalse(result.Success);
        }
Ejemplo n.º 28
0
        public override Result Validate(GoFishGame game)
        {
            if (game is null)
            {
                throw new ArgumentNullException(nameof(game));
            }

            ResultBuilder resultBuilder = new ResultBuilder();

            if (game.GameState != GameState.Waiting)
            {
                resultBuilder.AddFeedback($"Cannot start the game because it is not in the 'waiting' state.", false);
            }
            if (game.Players.Count < 2)
            {
                resultBuilder.AddFeedback($"Cannot start the game because there are less than 2 players.", false);
            }

            return(resultBuilder.Build());
        }
Ejemplo n.º 29
0
        public override Result Validate(GoFishGame game)
        {
            if (game is null)
            {
                throw new ArgumentNullException(nameof(game));
            }

            ResultBuilder resultBuilder = new ResultBuilder();

            if (game.GameState != GameState.Waiting)
            {
                resultBuilder.AddFeedback("Games can only be joined if they are in the 'waiting' state.", false);
            }

            if (game.Players.Any(p => p.Id.Equals(_guid)))
            {
                resultBuilder.AddFeedback($"Games can only be joined if the player is not already part of them.", false);
            }

            return(resultBuilder.Build());
        }
Ejemplo n.º 30
0
        public Game ChooseGame(Person player)
        {
            Game   game;
            string choice = "";

            do
            {
                Console.WriteLine("What game would you like to play? BlackJack, Slots, Craps, Go Fish, or War?");
                choice = Console.ReadLine().ToLower();
                if (choice == "blackjack" || choice == "black jack")
                {
                    game = new BlackJackGame(player);
                }
                else if (choice == "go fish" || choice == "gofish")
                {
                    game = new GoFishGame(player);
                }
                else if (choice == "war")
                {
                    game = new War(player);
                }
                else if (choice == "craps")
                {
                    game = new Craps(player);
                }
                else if (choice == "slots")
                {
                    game = new Slots(player);
                }
                else
                {
                    game = null;
                }
            } while(choice != "blackjack" && choice != "black jack" && choice != "go fish" && choice != "gofish" && choice != "war" &&
                    choice != "slots" && choice != "craps");
            Console.WriteLine("Going to " + choice + "!");
            return(game);
        }