Пример #1
0
        private async void Deck_Card_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            CardView userCard = (CardView)sender;

            foreach (CardView card in GridDeck.Items)
            {
                card.PointerReleased -= this.Deck_Card_PointerReleased;
            }

            await userCard.SetOrientation(CardOrientation.FaceUp);

            userCard.BoostZindex();
            await userCard.AnimateScale(2.0, MainPage.AnimationSpeeds.Medium);

            CardView compCard = null;
            Random   r        = new Random();

            do
            {
                int index = r.Next(GridDeck.Items.Count - 1);
                compCard = GridDeck.Items[index] as CardView;
            } while (compCard.Rank == userCard.Rank);

            await compCard.SetOrientation(CardOrientation.FaceUp);

            compCard.BoostZindex();
            await compCard.AnimateScale(2.0, MainPage.AnimationSpeeds.Medium);

            string message = String.Format("\nYou selected the {0}\nThe Computer Selected the {1}\n{2} won the deal.",
                                           userCard.DisplayName, compCard.DisplayName,
                                           compCard.Rank < userCard.Rank ? "The Computer" : "You");

            await HintWindow_ShowAndWait(message);

            List <Task <object> > taskList = new List <Task <object> >();

            userCard.SetOrientation(CardOrientation.FaceDown, taskList);
            userCard.AnimateScale(taskList, 1.0, MainPage.AnimationSpeeds.Medium);
            compCard.SetOrientation(CardOrientation.FaceDown, taskList);
            compCard.AnimateScale(taskList, 1.0, MainPage.AnimationSpeeds.Medium);

            userCard.ResetZIndex();
            compCard.ResetZIndex();


            GridDeck.UpdateCardLayout(taskList, MainPage.AnimationSpeeds.Medium, true);
            await Task.WhenAll(taskList);

            await _stateMachine.StartGame((compCard.Rank < userCard.Rank)?PlayerType.Computer : PlayerType.Player, Deck);

            MainPage.Current.EnableAppBarButtons(true);

            /// _btnNewGame.IsEnabled = true;
        }
        public async Task <Dictionary <string, string> > Load(string s)
        {
            if (s == "")
            {
                return(null);
            }

            Dictionary <string, string> sections = StaticHelpers.GetSections(s);

            if (_game.Deserialize(sections, _view.Deck) == false)
            {
                return(null);
            }

            Dictionary <string, string> game = StaticHelpers.DeserializeDictionary(sections["State"]);

            if (sections == null)
            {
                return(null);
            }

            if (game["Version"] != SERIALIZATION_VERSION)
            {
                return(null);
            }

            _gameState               = (GameState)Enum.Parse(typeof(GameState), game["GameState"]);
            _dealer                  = (PlayerType)Enum.Parse(typeof(PlayerType), game["Dealer"]);
            _totalCardsDropped       = Convert.ToInt32(game["TotalCardsDropped"]);
            _nPlayerCountingPoint    = Convert.ToInt32(game["PlayerCountingPoints"]);
            _nPlayerPointsThisTurn   = Convert.ToInt32(game["PlayerPointsThisTurn"]);
            _nComputerCountingPoint  = Convert.ToInt32(game["ComputerCountingPoints"]);
            _nComputerPointsThisTurn = Convert.ToInt32(game["ComputerPointsThisTurn"]);
            _nCribPointsThisTurn     = Convert.ToInt32(game["CribPointsThisTurn"]);
            int playerScore   = Convert.ToInt32(game["PlayerScore"]);
            int computerScore = Convert.ToInt32(game["ComputerScore"]);


            _hfs      = _game.GetHfs();
            _view.Hfs = _hfs;

            Dictionary <string, string> grids = StaticHelpers.DeserializeDictionary(sections["Grids"]);

            List <CardView> PlayerCards   = StaticHelpers.DeserializeToList(grids["Player"], _view.Deck);
            List <CardView> ComputerCards = StaticHelpers.DeserializeToList(grids["Computer"], _view.Deck);
            List <CardView> cribCards     = StaticHelpers.DeserializeToList(grids["Crib"], _view.Deck);



            CardView        SharedCard  = StaticHelpers.CardFromString(grids["SharedCard"], _view.Deck);
            List <CardView> playedCards = StaticHelpers.DeserializeToList(grids["Played"], _view.Deck);

            List <Task <object> > taskList = new List <Task <object> >();

            _view.MoveCards(taskList, playedCards, _view.GridPlayedCards);
            _view.MoveCards(taskList, cribCards, _view.GridCrib);
            _view.MoveCards(taskList, PlayerCards, _view.GridPlayer);
            _view.MoveCards(taskList, ComputerCards, _view.GridComputer);

            if (_gameState == GameState.PlayerGiveToCrib)
            {
                _crib.AddRange(playedCards);
            }

            await Task.WhenAll(taskList);

            SharedCard.BoostZindex(ZIndexBoost.SmallBoost);

            _game.UpdateScoreDirect(PlayerType.Player, ScoreType.Saved, playerScore);     // give the points to the player
            _game.UpdateScoreDirect(PlayerType.Computer, ScoreType.Saved, computerScore); // give the points to the player

            await UiState.AddScore(PlayerType.Player, playerScore);

            await UiState.AddScore(PlayerType.Computer, computerScore);

            _view.CountControl.Count = _game.CurrentCount;

            await FixUpUiState();

            string scoreHistory = "";

            if (sections.TryGetValue("Score History", out scoreHistory))
            {
                await _view.Load(scoreHistory);
            }

            if (_gameState == GameState.PlayerScoreHand)
            {
                //
                //  if we saved in the state where the player is supposed to score the hard, we need to drive the state machine
                //  through the completion of the GameState.ScoreHand states
                if (_dealer == PlayerType.Player)
                {
                    await SetState(GameState.PlayerScoreHand);
                    await SetState(GameState.ShowCrib);
                    await SetState(GameState.PlayerScoreCrib);

                    _gameState = GameState.EndOfHand;
                }
                else
                {
                    _gameState = GameState.ScoreHands;
                }
            }
            else if (_gameState == GameState.PlayerScoreCrib)
            {
                await SetState(GameState.PlayerScoreCrib);

                _gameState = GameState.EndOfHand;
            }



            SetStateAsync(_gameState);


            return(sections);
        }