/// <summary>
        /// Writes output to the text file on every shuffle or run of the application.
        /// </summary>
        /// <param name="viewModel">View model to display player card information.</param>
        private void WriteOutputToTextFile(PlayerCardViewModel viewModel)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + @"Ouput.txt";

            // Check for file existence, If not exist already then create new file.
            if (!IO.File.Exists(path))
            {
                // Creates a file to write to.
                using (var sw = IO.File.CreateText(path))
                {
                    foreach (var player in viewModel.Cards)
                    {
                        sw.WriteLine(player.CardsInHand);
                    }
                }
            }
            else
            {
                // Clears out all text before append.
                IO.File.WriteAllText(path, string.Empty);

                // Append text if file already exists.
                using (var sw = IO.File.AppendText(path))
                {
                    foreach (var player in viewModel.Cards)
                    {
                        sw.WriteLine(player.CardsInHand);
                    }
                }
            }
        }
Esempio n. 2
0
        public PlayerCardPage(PlayerScore ps)
        {
            InitializeComponent();

            BindingContext = new PlayerCardViewModel();

            // assign PlayerScore property in viewmodel
            viewModel.PlayerScore = ps;
        }
        /// <summary>
        /// Get all cards detail assigned to each hand.
        /// </summary>
        /// <returns>Player card view model data.</returns>
        private PlayerCardViewModel GetCardsInHand()
        {
            // Creates hands of 5 players.
            var hands = new Hand[5];

            // Iterate through card collection for equal distribution of cards to the hands.
            for (var i = 0; i <= 24; i++)
            {
                // Iterate to assign cards to five players.
                for (var j = 0; j <= 4; j++)
                {
                    if (hands[j] == null)
                    {
                        hands[j] = new Hand();
                    }

                    if (i < _deck.Cards.Count)
                    {
                        hands[j].AddCard(_deck.Cards[i]);
                    }

                    ++i;
                }
            }

            var viewModel   = new PlayerCardViewModel();
            var cardBuckets = new List <PlayerCardViewModel.CardBucket>();

            // Counter to display player number.
            var counter = 1;

            // Iterate through hands and sort the values to get sorted cards in hand.
            foreach (var player in hands)
            {
                var sb = new StringBuilder();
                sb.Append($"Player #{counter}: ");
                player.SortByValue();
                foreach (var item in player.hand)
                {
                    sb.Append($"{item.CardNumbers.GetDescription()}{item.Suits.GetDescription()}-");
                }
                cardBuckets.Add(new PlayerCardViewModel.CardBucket
                {
                    // Remove last extra dash from the string.
                    CardsInHand = sb.Remove(sb.Length - 1, 1).ToString()
                });

                counter++;
            }

            viewModel.Cards = cardBuckets;
            WriteOutputToTextFile(viewModel);

            return(viewModel);
        }