Ejemplo n.º 1
0
        private async Task StartGame(GameState state)
        {
            var winner = await _game.StartGame(state);

            var msg = "";

            msg = winner == PlayerType.Player ? "Congratulations, you won!" : "Oh well. \n\nThe computer won.  Better luck next time!";

            await StaticHelpers.ShowErrorText(msg, "");

            SetState(GameState.GameOver);
        }
Ejemplo n.º 2
0
        private async void OnGetSuggestion(object sender, RoutedEventArgs e)
        {
            if (_game == null)
            {
                await StaticHelpers.ShowErrorText("Hit + to start a game.", "Cribbage");

                return;
            }

            if (_game.State == GameState.PlayerSelectsCribCards)
            {
                if (_cgPlayer.Cards.Count != 6)
                {
                    return;
                }

                foreach (var c in _cgPlayer.Cards)
                {
                    c.Selected = false;
                }

                _cgPlayer.SelectedCards?.Clear();
                _cgPlayer.SelectedCards = _game.ComputerSelectCrib(_cgPlayer.Cards, _game.Dealer == PlayerType.Player);

                if (_cgPlayer.SelectedCards?.Count == 2)
                {
                    _cgPlayer.SelectedCards[0].Selected = true;
                    _cgPlayer.SelectedCards[1].Selected = true;
                }

                return;
            }

            if (_game.State == GameState.CountPlayer)
            {
                var cardPlayed = await _game.GetSuggestionForCount();

                _cgPlayer.SelectCard(cardPlayed);
            }
        }
Ejemplo n.º 3
0
        private async void OnOpenGame(object sender, RoutedEventArgs e)
        {
            MyMenu.IsPaneOpen = false;

            if (_game != null)
            {
                if (_game.State != GameState.None)
                {
                    if (await StaticHelpers.AskUserYesNoQuestion("Cribbage", "End this game and open an old one?",
                                                                 "yes", "no") == false)
                    {
                        return;
                    }
                }
            }

            var openPicker = new FileOpenPicker
            {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary
            };

            openPicker.FileTypeFilter.Add(".crib");

            var file = await openPicker.PickSingleFileAsync();

            if (file == null)
            {
                return;
            }

            await Reset();

            _txtInstructions.Text = "";
            var player   = new InteractivePlayer(_cgDiscarded, _cgCrib, 0);
            var computer = new DefaultPlayer(0);

            computer.Init("-usedroptable");
            _game = new Game(this, computer, player, 0);


            try
            {
                var contents = await FileIO.ReadTextAsync(file);

                var settings = await StaticHelpers.LoadSettingsFile(contents, file.Name);

                if (settings["Game"]["Version"] != "1.0")
                {
                    await StaticHelpers.ShowErrorText($"Bad Version {settings["Game"]["Version"]}");

                    return;
                }

                _game.CurrentCount   = int.Parse(settings["Game"]["CurrentCount"]);
                _game.Player.Score   = int.Parse(settings["Game"]["PlayerBackScore"]);
                _game.AutoEnterScore = bool.Parse(settings["Game"]["AutoEnterScore"]);

                _board.AnimateScoreAsync(PlayerType.Player, _game.Player.Score);
                var scoreDelta = int.Parse(settings["Game"]["PlayerScoreDelta"]);
                _board.AnimateScoreAsync(PlayerType.Player, scoreDelta);
                _game.Player.Score += scoreDelta;

                _game.Computer.Score = int.Parse(settings["Game"]["ComputerBackScore"]);
                _board.AnimateScoreAsync(PlayerType.Computer, _game.Computer.Score);
                scoreDelta = int.Parse(settings["Game"]["ComputerScoreDelta"]);
                _board.AnimateScoreAsync(PlayerType.Computer, scoreDelta);
                _game.Computer.Score += scoreDelta;

                _game.Dealer = StaticHelpers.ParseEnum <PlayerType>(settings["Game"]["Dealer"]);
                _game.State  = StaticHelpers.ParseEnum <GameState>(settings["Game"]["State"]);
                SetState(_game.State);
                await MoveCrib(_game.Dealer);

                var retTuple = LoadCardsIntoGrid(_cgComputer, settings["Cards"]["Computer"]);
                if (!retTuple.ret)
                {
                    throw new Exception(retTuple.badToken);
                }

                retTuple = LoadCardsIntoGrid(_cgPlayer, settings["Cards"]["Player"]);
                if (!retTuple.ret)
                {
                    throw new Exception(retTuple.badToken);
                }

                retTuple = LoadCardsIntoGrid(_cgDiscarded, settings["Cards"]["Counted"]);
                if (!retTuple.ret)
                {
                    throw new Exception(retTuple.badToken);
                }

                var countedCards = new List <CardCtrl>();
                var count        = 0;
                foreach (var card in _cgDiscarded.Cards)
                {
                    count += card.Value;
                    if (count <= 31)
                    {
                        countedCards.Add(card);
                    }
                    else
                    {
                        foreach (var cc in countedCards)
                        {
                            cc.Counted = true;
                            cc.Opacity = 0.8;
                        }

                        countedCards.Clear();
                        count = 0;
                    }
                }

                retTuple = LoadCardsIntoGrid(_cgCrib, settings["Cards"]["Crib"]);
                if (!retTuple.ret)
                {
                    throw new Exception(retTuple.badToken);
                }

                retTuple = LoadCardsIntoGrid(_cgDeck, settings["Cards"]["SharedCard"]);
                if (!retTuple.ret)
                {
                    throw new Exception(retTuple.badToken);
                }

                var  taskList = new List <Task>();
                Task t        = null;

                if ((int)_game.State > (int)GameState.GiveToCrib)
                {
                    t = _cgDeck.Cards[0].SetOrientationTask(CardOrientation.FaceUp, 500, 0);
                    taskList.Add(t);
                }

                foreach (var card in _cgPlayer.Cards)
                {
                    t = card.SetOrientationTask(CardOrientation.FaceUp, 0, 0);
                    taskList.Add(t);
                }

                foreach (var card in _cgDiscarded.Cards)
                {
                    t = card.SetOrientationTask(CardOrientation.FaceUp, 0, 0);
                    taskList.Add(t);
                }

                if ((int)_game.State >= (int)GameState.ScoreComputerHand)
                {
                    foreach (var card in _cgComputer.Cards)
                    {
                        t = card.SetOrientationTask(CardOrientation.FaceUp, 0, 0);
                        taskList.Add(t);
                    }
                }

                await Task.WhenAll(taskList);

                await StartGame(_game.State);
            }
            catch (Exception ex)
            {
                await StaticHelpers.ShowErrorText(
                    $"Error loading file {file.Name}\n\nYou should delete the file.\n\nTechnical details:\n{ex}");
            }
        }