Ejemplo n.º 1
0
        private async void OnNewGame(object sender, RoutedEventArgs e)
        {
            MyMenu.IsPaneOpen = false;
            await Task.Delay(500);

            try
            {
                if (_game != null)
                {
                    if (_game.State != GameState.None)
                    {
                        if (await StaticHelpers.AskUserYesNoQuestion("Cribbage", "Start a new game?", "Yes", "No") ==
                            false)
                        {
                            return;
                        }
                    }

                    _game = null; // what happens if we are in an await???
                }

                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);
                ((Button)sender).IsEnabled = true;
                await StartGame(GameState.Start);
            }
            catch
            {
                // eat this - user won't be able to do anythign anyway
            }
            finally
            {
                ((Button)sender).IsEnabled = true;
            }
        }
Ejemplo n.º 2
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}");
            }
        }