private void NextPlayer() { if (CurrentPlayer.HasNoCardsLeft()) { CurrentPlayState = PlayState.WON; stopWatch.Stop(); stopWatch.Start(); Broadcast(MessageParser.PlayerWonToString(CurrentPlayer)); } else { hasPlayerPickedFromDeck = false; int index = Players.IndexOf(CurrentPlayer); if (index + 1 == Players.Count) { index = 0; } else { index++; } CurrentPlayer = Players.ElementAt(index); Broadcast(MessageParser.ToPlayerTurn(CurrentPlayer)); } }
public void Update() { if (CurrentPlayState == PlayState.NONE && Players.Count == 4) { StartGame(); } foreach (Player player in Players.ToList()) { if (player is NetworkPlayer) { NetworkPlayer networkPlayer = (NetworkPlayer)player; if (!networkPlayer.IsConnected()) { Broadcast(MessageParser.PlayerRemovedToString(player.Name)); Players.Remove(player); Console.WriteLine("Player {0} left", player.Name); if (CurrentPlayer == player) { NextPlayer(); } } } } if (CurrentPlayState == PlayState.DEAL) { if (stopWatch.ElapsedMilliseconds > 300) { foreach (Player player in Players) { if (player.HandCardCount() < 3) { Card card = Deck.DrawCard(); player.AddHandCards(card); Console.WriteLine("{0} received {1}", player.Name, card.ToString()); Broadcast(MessageParser.PlayerReceivedToString(player, card)); } else if (player.TableFlippedCardCount() < 3) { Card card = Deck.DrawCard(); player.AddTableFlippedCards(card); Console.WriteLine("{0} received {1}", player.Name, card.ToString()); Broadcast(MessageParser.PlayerReceivedToString(player, card)); } else { Card card = Deck.DrawCard(); player.AddTableCards(card); Console.WriteLine("{0} received {1}", player.Name, card.ToString()); Broadcast(MessageParser.PlayerReceivedToString(player, card)); } } if (Players.ElementAt(0).TableCardCount() == 3) { CurrentPlayState = PlayState.SWAP; Console.WriteLine("Changed STATE to SWAP!"); Broadcast(MessageParser.PlayStateToString(CurrentPlayState)); } stopWatch.Restart(); } } else if (CurrentPlayState == PlayState.SWAP) { if (stopWatch.ElapsedMilliseconds > 15000) { CurrentPlayState = PlayState.PLAY; Card drawCard = Deck.DrawCard(); AddPlayedCards(drawCard); Console.WriteLine("Changed STATE to PLAY!"); Broadcast(MessageParser.FirstCardToString(drawCard)); Console.WriteLine("First card is {0}", drawCard.ToString()); Broadcast(MessageParser.PlayStateToString(CurrentPlayState)); stopWatch.Stop(); } else { foreach (Player player in Players) { PlayerAction action = player.HandleInput(this); if (action != null && action.Type == PlayerActionType.SWAP_CARD && action.Cards.Count > 1) { Card handCard = action.Cards[0]; Card swapCard = action.Cards[1]; player.RemoveCard(handCard); player.RemoveCard(swapCard); player.AddHandCards(swapCard); player.AddTableCards(handCard); Console.WriteLine("{0} swapped {1} with {2}", player.Name, handCard.ToString(), swapCard.ToString()); Broadcast(MessageParser.SwapCardToString(player, handCard, swapCard)); } } } } else if (CurrentPlayState == PlayState.PLAY) { // Handle play state if (CurrentPlayer != null) { if (CurrentPlayer.HasNoCardsLeft()) { CurrentPlayState = PlayState.WON; Console.WriteLine("Changed STATE to WON!"); stopWatch.Start(); Broadcast(MessageParser.PlayerWonToString(CurrentPlayer)); } else { PlayerAction action = CurrentPlayer.HandleInput(this); if (action != null) { if (action.Type == PlayerActionType.PLAY_CARD) { if (!PlayCards(action.Cards.ToArray())) { if (CurrentPlayer.HandCardCount() == 0 || CurrentPlayer.HandCardCount() == 0 && CurrentPlayer.TableCardCount() == 0) { // Trying to play a flipped card foreach (Card card in action.Cards) { CurrentPlayer.RemoveCard(card); CurrentPlayer.AddHandCards(card); } Console.WriteLine("{0} tried {1}", CurrentPlayer.Name, action.Cards[0].ToString()); Broadcast(MessageParser.PlayerTriedCards(action.Cards)); List <Card> pickedUpCards = new List <Card>(); while (Pile.Count > 0) { Card pickedUpCard = Pile.Pop(); pickedUpCards.Add(pickedUpCard); CurrentPlayer.AddHandCards(pickedUpCard); } Console.WriteLine("{0} picked up cards", CurrentPlayer.Name); Broadcast(MessageParser.PlayerPickupCardsToString(CurrentPlayer, pickedUpCards.ToArray())); NextPlayer(); } } else { // Played a normal hand while (Deck.CanDrawCard() && CurrentPlayer.HandCardCount() < 3) { Card card = Deck.DrawCard(); CurrentPlayer.AddHandCards(card); Broadcast(MessageParser.PlayerDeckCardToString(card)); } Console.WriteLine("{0} played {1}", CurrentPlayer.Name, action.Cards[0].ToString()); Broadcast(MessageParser.CardsPlayedToString(CurrentPlayer, action.Cards.ToArray())); if (!EvaluateCards(action.Cards.ElementAt(0))) { NextPlayer(); } } } else if (action.Type == PlayerActionType.PICK_UP_CARDS) { List <Card> pickedUpCards = new List <Card>(); while (Pile.Count > 0) { Card pickedUpCard = Pile.Pop(); pickedUpCards.Add(pickedUpCard); CurrentPlayer.AddHandCards(pickedUpCard); } Console.WriteLine("{0} picked up cards", CurrentPlayer.Name); Broadcast(MessageParser.PlayerPickupCardsToString(CurrentPlayer, pickedUpCards.ToArray())); NextPlayer(); } else if (action.Type == PlayerActionType.TRY_CARD) { Card card = DrawCard(); if (card != null) { CurrentPlayer.AddHandCards(card); Console.WriteLine("{0} tried {1}", CurrentPlayer.Name, card.ToString()); Broadcast(MessageParser.PlayerTriedFromDeckToString(card)); } } } } } else { // Set random player as the current player Random random = new Random(); CurrentPlayer = Players.ElementAt(random.Next(Players.Count - 1)); Broadcast(MessageParser.ToPlayerTurn(CurrentPlayer)); } } else if (CurrentPlayState == PlayState.WON) { if (stopWatch.ElapsedMilliseconds > 5000) { foreach (Player player in Players) { player.ResetCards(); } Deck = Deck.CreateFullDeck(); Deck.Shuffle(); Pile.Clear(); CurrentPlayState = PlayState.DEAL; stopWatch.Restart(); } } }