예제 #1
0
 public TurnInfo(TurnInfo source)
 {
     TurnNumber = source.TurnNumber;
     PlayedCard = new UnoCard(source.PlayedCard);
     DrawnCards = new List <UnoCard>();
     foreach (var card in source.DrawnCards)
     {
         DrawnCards.Add(new UnoCard(card));
     }
 }
예제 #2
0
        public void GameLoop()
        {
            while (true)
            {
                _display.Clear();

                // Set this in case console is bugging out
                Console.CursorVisible = false;

                // Get input
                if (Console.KeyAvailable)
                {
                    _input = Console.ReadKey(true).Key;
                    Debug.WriteLine("Key pressed: " + _input);
                }
                else
                {
                    _input = null;
                }

                if (_selectingWildcardColor)
                {
                    // Check for card selection
                    if (_input is not null)
                    {
                        var pressed = (ConsoleKey)_input;
                        // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
                        switch (pressed)
                        {
                        case ConsoleKey.D1:
                        case ConsoleKey.D2:
                        case ConsoleKey.D3:
                        case ConsoleKey.D4:
                            PlayWildCard(pressed);
                            break;
                        }
                    }

                    var blueCard   = new UnoCard(CardType.Wild, CardColor.Blue, 0);
                    var redCard    = new UnoCard(CardType.Wild, CardColor.Red, 0);
                    var greenCard  = new UnoCard(CardType.Wild, CardColor.Green, 0);
                    var yellowCard = new UnoCard(CardType.Wild, CardColor.Yellow, 0);

                    _view.ResetVisibleIndex();
                    _view.Hand.Clear();
                    _view.Hand.Add(blueCard);
                    _view.Hand.Add(redCard);
                    _view.Hand.Add(greenCard);
                    _view.Hand.Add(yellowCard);

                    _view.Message      = "Choose a color for the wildcard.";
                    _view.MessageColor = ConsoleColor.Green;
                    _view.Draw();

                    _display.Update();

                    Thread.Sleep(100);

                    continue;
                }

                UpdateView();

                switch (_gameState.GameStatus)
                {
                case GameStatus.Started:
                    switch (_gameState.TurnStatus)
                    {
                    case TurnStatus.Waiting:
                        _view.CurrentTurnPlayerId = _gameState.NextTurnPlayerId;

                        _view.Message      = "Waiting for your turn...";
                        _view.MessageColor = ConsoleColor.Yellow;
                        _view.Draw();
                        break;

                    case TurnStatus.Ongoing:
                        _view.CurrentTurnPlayerId = _gameState.LocalPlayer.PlayerID;

                        // Dealer flipping the first card
                        if (_gameState.LocalPlayer.Dealer && _gameState.TurnNumber == 1)
                        {
                            if (WasKeyPressed(ConsoleKey.Spacebar))
                            {
                                _gameState.PlayFirstCard();
                                Debug.WriteLine("Played first card");
                                continue;
                            }

                            // Dealer's turn to play first card
                            _view.Message      = "You are the dealer! Press space to flip the first card,";
                            _view.MessageColor = ConsoleColor.Green;
                            _view.Draw();
                            break;
                        }

                        // Can't play any cards even after drawing a new card
                        if (!_gameState.PlayableCardInHand && _gameState.CardDrawn)
                        {
                            if (WasKeyPressed(ConsoleKey.Spacebar))
                            {
                                _gameCoordinator.EndTurn();
                                _view.ResetVisibleIndex();
                                continue;
                            }

                            _view.Message      = "You can't play any cards, press space to end turn.";
                            _view.MessageColor = ConsoleColor.Yellow;
                            _view.Draw();
                            break;
                        }

                        // "Normal" turn, can choose a card to play or draw a new card
                        if (!_gameState.CardDrawn)
                        {
                            _view.Message =
                                "Your turn! Select a card to play or press space to draw a new card.";
                        }
                        else
                        {
                            _view.Message = "Your turn! Select a card to play.";
                        }

                        _view.MessageColor = ConsoleColor.Green;

                        // Check for input first
                        if (_input is not null)
                        {
                            var pressed = (ConsoleKey)_input;
                            // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
                            switch (pressed)
                            {
                            case ConsoleKey.Spacebar:
                                // Draw card
                                if (_gameState.CardDrawn)
                                {
                                    break;
                                }

                                _gameState.PlayerDrawCard();
                                break;

                            case ConsoleKey.D1:
                            case ConsoleKey.D2:
                            case ConsoleKey.D3:
                            case ConsoleKey.D4:
                            case ConsoleKey.D5:
                                PlayCard(pressed);
                                break;
                            }
                        }

                        _view.Draw();
                        break;

                    case TurnStatus.Ready:
                        _view.CurrentTurnPlayerId = _gameState.LocalPlayer.PlayerID;

                        if (WasKeyPressed(ConsoleKey.Spacebar))
                        {
                            _gameCoordinator.EndTurn();
                            _view.ResetVisibleIndex();
                            continue;
                        }

                        _view.Message      = "Turn done! Press space to send.";
                        _view.MessageColor = ConsoleColor.Red;
                        _view.Draw();
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    break;

                case GameStatus.NotStarted:
                    _view.Message      = "Waiting for game to start...";
                    _view.MessageColor = ConsoleColor.White;
                    _view.Draw();
                    break;

                case GameStatus.Dealing:
                    switch (_gameState.TurnStatus)
                    {
                    case TurnStatus.Waiting:
                        _view.CurrentTurnPlayerId = _gameState.NextTurnPlayerId;

                        _view.Message      = "Waiting for your turn...";
                        _view.MessageColor = ConsoleColor.White;
                        _view.Draw();
                        break;

                    case TurnStatus.Ongoing:
                        _view.CurrentTurnPlayerId = _gameState.LocalPlayer.PlayerID;

                        if (WasKeyPressed(ConsoleKey.Spacebar))
                        {
                            _gameState.DealSelf();
                            continue;
                        }

                        _view.Message      = "Press space to deal yourself a hand.";
                        _view.MessageColor = ConsoleColor.Green;
                        _view.Draw();
                        break;

                    case TurnStatus.Ready:
                        _view.CurrentTurnPlayerId = _gameState.LocalPlayer.PlayerID;

                        if (WasKeyPressed(ConsoleKey.Spacebar))
                        {
                            _gameCoordinator.EndTurn();
                            continue;
                        }

                        _view.Message      = "Turn done! Press space to send.";
                        _view.MessageColor = ConsoleColor.Red;
                        _view.Draw();
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    break;

                case GameStatus.Won:
                    if (!_winTransmitted)
                    {
                        _gameCoordinator.EndTurn();
                        _winTransmitted = true;
                    }

                    _view.Message      = "You've Won!";
                    _view.MessageColor = ConsoleColor.Green;
                    _view.Draw();
                    break;

                case GameStatus.Lost:
                    _view.Message      = "Someone else won!";
                    _view.MessageColor = ConsoleColor.Red;
                    _view.Draw();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                _display.Update();
                Thread.Sleep(100);
            }
        }