Exemplo n.º 1
0
        private bool Play(Rack Player)
        {
            Console.WriteLine($"{Player.Name}'s turn");
            Console.WriteLine("Type 'yeild' to yeild game");

            var  availableLetters = GameBoard.GetAvailableLetters();
            var  selectedLetter   = ' ';
            Tile selectedTile     = null;

            if (availableLetters.Count == 0)
            {
                Console.WriteLine("Form a word from letters from your rack");
            }
            else
            {
                Console.WriteLine("Form a word from letters from your rack and one letter from the board");
                Console.WriteLine("Available letters in rack : " + Player.PrintAvailableLetters());
                Console.Write("Available letters in the board : ");
                var availableLetterChars = new List <char>();
                availableLetters.ForEach(l =>
                {
                    availableLetterChars.Add(l.Letter);
                    Console.Write(l.Letter + " ");
                });
                Console.WriteLine();

                selectedLetter = Player.SelectLetterFromBoard(availableLetterChars);
                selectedTile   = availableLetters.FirstOrDefault(l => l.Letter == selectedLetter);
            }

            bool isValidWord;

            do
            {
                var word = Player.PlayWord();
                if (word.Equals(ConstantStrings.YEILD))
                {
                    Player.SetTurn(false);
                    Player.YeildGame();
                    break;
                }
                else if (!WordIsFormedFromAvailableLetters(Player.Tiles, selectedTile, word.ToUpper()))
                {
                    Console.WriteLine();
                    Console.WriteLine(word + " is not created from the available letters. Please try again or type yeild to give up");
                    Console.WriteLine();
                    Console.WriteLine("Available letters in rack : " + Player.PrintAvailableLetters());
                    Console.Write("Available letters in the board : ");
                    var availableLetterChars = new List <char>();
                    availableLetters.ForEach(l =>
                    {
                        availableLetterChars.Add(l.Letter);
                        Console.Write(l.Letter + " ");
                    });
                    Console.WriteLine();

                    if (availableLetterChars.Count > 0)
                    {
                        selectedLetter = Player.SelectLetterFromBoard(availableLetterChars);
                    }
                    selectedTile = availableLetters.FirstOrDefault(l => l.Letter == selectedLetter);
                    isValidWord  = false;
                }
                else
                {
                    isValidWord = ValidateWord(word);
                    if (!isValidWord)
                    {
                        Console.WriteLine(word + " is not a valid word. Please try again or type yeild to give up");
                        Console.WriteLine();
                        Console.WriteLine("Available letters in rack : " + Player.PrintAvailableLetters());
                        Console.Write("Available letters in the board : ");
                        var availableLetterChars = new List <char>();
                        availableLetters.ForEach(l =>
                        {
                            availableLetterChars.Add(l.Letter);
                            Console.Write(l.Letter + " ");
                        });
                        Console.WriteLine();

                        if (availableLetterChars.Count > 0)
                        {
                            selectedLetter = Player.SelectLetterFromBoard(availableLetterChars);
                        }
                        selectedTile = availableLetters.FirstOrDefault(l => l.Letter == selectedLetter);
                        isValidWord  = false;
                    }
                    // Word is valid. Add to board and calculate points
                    else
                    {
                        var tilesToAddToBoard = RemoveWordTilesFromPlayerRack(Player.Tiles, selectedLetter, word.ToUpper());
                        var wordAdded         = GameBoard.AddWordToBoard(tilesToAddToBoard, selectedTile, word.ToUpper());
                        if (wordAdded)
                        {
                            Player.AddPoints(word.Length);
                            Console.WriteLine();
                            Console.WriteLine($"Current points for {Player.Name} : {Player.Score}");
                            Player.SetTurn(false);
                            Console.WriteLine("Remaining letters : " + Player.PrintAvailableLetters());
                            Console.WriteLine("Adding new letters");
                            AssignTilesToPlayerRack(Player);
                            Console.WriteLine("Available letters : " + Player.PrintAvailableLetters());
                            Console.WriteLine("Remaining tile count : " + Tiles.Count);
                            Console.WriteLine();
                        }
                        else
                        {
                            // add the tiles back
                            AddTilesToPlayerRack(Player.Tiles, tilesToAddToBoard);
                            Console.WriteLine("Cannot add word: " + word + " to board. Please try again or type yeild to give up");
                            Console.WriteLine("Form a word from letters from your rack and one letter from the board");
                            Console.WriteLine("Available letters in rack : " + Player.PrintAvailableLetters());
                            Console.Write("Available letters in the board : ");
                            var availableLetterChars = new List <char>();
                            availableLetters.ForEach(l =>
                            {
                                availableLetterChars.Add(l.Letter);
                                Console.Write(l.Letter + " ");
                            });
                            Console.WriteLine();

                            selectedLetter = Player.SelectLetterFromBoard(availableLetterChars);
                            selectedTile   = availableLetters.FirstOrDefault(l => l.Letter == selectedLetter);
                        }
                    }
                }
            } while (!isValidWord);

            return(true);
        }