Difference between revisions of "Allow a player to buy a vowel for 10 points"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 87: Line 87:
 
<h1> Alternative Method </h1>
 
<h1> Alternative Method </h1>
  
 +
This is the <b>existing</b> method for a player's turn. You only need to add/edit one line of this method.
 +
<syntaxhighlight lang="c#">
 +
 +
        private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
 +
        {
 +
            Console.WriteLine();
 +
            Console.WriteLine(PlayerName + " it is your turn.");
 +
            DisplayTilesInHand(PlayerTiles);
 +
            string NewTileChoice = "2";
 +
            bool ValidChoice = false;
 +
            bool ValidWord = false;
 +
            string Choice = "";
 +
            while (!ValidChoice)
 +
            {
 +
                Choice = GetChoice();
 +
                if (Choice == "1")
 +
                {
 +
                    DisplayTileValues(TileDictionary, AllowedWords);
 +
                }
 +
                else if (Choice == "4")
 +
                {
 +
                    TileQueue.Show();
 +
                }
 +
                else if (Choice == "7")
 +
                {
 +
                    DisplayTilesInHand(PlayerTiles);
 +
                }
 +
                else if (Choice == "0")
 +
                {
 +
                    ValidChoice = true;
 +
                    FillHandWithTiles(ref TileQueue, ref PlayerTiles, MaxHandSize);
 +
                }
 +
                else
 +
                {
 +
                    ValidChoice = true;
 +
                    if (Choice.Length == 0)
 +
                    {
 +
                        ValidWord = false;
 +
                    }
 +
                    else
 +
                    {
 +
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
 +
                    }
 +
                    if (ValidWord)
 +
                    {
 +
                        ValidWord = CheckWordIsValid(Choice, AllowedWords);
 +
                        if (ValidWord)
 +
                        {
 +
                            Console.WriteLine();
 +
                            Console.WriteLine("Valid word");
 +
                            Console.WriteLine();
 +
                            UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
 +
                            Console.WriteLine("Your current score is: " + PlayerScore);
 +
                            NewTileChoice = GetNewTileChoice();
 +
                        }
 +
                    }
 +
                    if (!ValidWord)
 +
                    {
 +
                        Console.WriteLine();
 +
                        Console.WriteLine("Not a valid attempt, you lose your turn.");
 +
                        Console.WriteLine();
 +
                    }
 +
                    if (NewTileChoice != "4")
 +
                    {
 +
                        AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, ref NewTileChoice, Choice, ref PlayerScore);
 +
                    }
 +
                    Console.WriteLine();
 +
                    Console.WriteLine("Your word was:" + Choice);
 +
                    Console.WriteLine("Your new score is:" + PlayerScore);
 +
                    Console.WriteLine("You have played " + PlayerTilesPlayed + " tiles so far in this game.");
 +
                }
 +
            }
 +
        }
 +
 +
</syntaxhighlight>
 +
 +
This is a method to generate a random number, which is then used to select a random vowel:
 
<syntaxhighlight lang="c#">
 
<syntaxhighlight lang="c#">
 
private static string RandomVowel()
 
private static string RandomVowel()
Line 178: Line 255:
 
             return NewTileChoice; ;
 
             return NewTileChoice; ;
 
         }
 
         }
</syntaxhighlight>
 
 
<syntaxhighlight lang="c#">
 
 
        private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
 
        {
 
            Console.WriteLine();
 
            Console.WriteLine(PlayerName + " it is your turn.");
 
            DisplayTilesInHand(PlayerTiles);
 
            string NewTileChoice = "2";
 
            bool ValidChoice = false;
 
            bool ValidWord = false;
 
            string Choice = "";
 
            while (!ValidChoice)
 
            {
 
                Choice = GetChoice();
 
                if (Choice == "1")
 
                {
 
                    DisplayTileValues(TileDictionary, AllowedWords);
 
                }
 
                else if (Choice == "4")
 
                {
 
                    TileQueue.Show();
 
                }
 
                else if (Choice == "7")
 
                {
 
                    DisplayTilesInHand(PlayerTiles);
 
                }
 
                else if (Choice == "0")
 
                {
 
                    ValidChoice = true;
 
                    FillHandWithTiles(ref TileQueue, ref PlayerTiles, MaxHandSize);
 
                }
 
                else
 
                {
 
                    ValidChoice = true;
 
                    if (Choice.Length == 0)
 
                    {
 
                        ValidWord = false;
 
                    }
 
                    else
 
                    {
 
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
 
                    }
 
                    if (ValidWord)
 
                    {
 
                        ValidWord = CheckWordIsValid(Choice, AllowedWords);
 
                        if (ValidWord)
 
                        {
 
                            Console.WriteLine();
 
                            Console.WriteLine("Valid word");
 
                            Console.WriteLine();
 
                            UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
 
                            Console.WriteLine("Your current score is: " + PlayerScore);
 
                            NewTileChoice = GetNewTileChoice();
 
                        }
 
                    }
 
                    if (!ValidWord)
 
                    {
 
                        Console.WriteLine();
 
                        Console.WriteLine("Not a valid attempt, you lose your turn.");
 
                        Console.WriteLine();
 
                    }
 
                    if (NewTileChoice != "4")
 
                    {
 
                        AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, ref NewTileChoice, Choice, ref PlayerScore);
 
                    }
 
                    Console.WriteLine();
 
                    Console.WriteLine("Your word was:" + Choice);
 
                    Console.WriteLine("Your new score is:" + PlayerScore);
 
                    Console.WriteLine("You have played " + PlayerTilesPlayed + " tiles so far in this game.");
 
                }
 
            }
 
        }
 
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 10:48, 16 November 2017

In all honesty, I'm unsure as to what this is asking. In perpetuity I feel there are three potential answers to what is being asked:

  • 1) It wants me to select a random tile in the players hand and replace it with a vowel (penalising the player should the removed tile also be a vowel).
  • 2) It wants me to select a random tile which isn't a vowel and replace it with a vowel (which poses a risk for error if the players hand is filled with vowels).
  • 3) Allow the player to decide which tile in his hand he/she wishes to replace with a vowel.

In all honesty, only the last solution appears to complete the required task and leave as little room for error as possible.

Well then, without hesitation, let's begin:

private static string GetChoice() {
    string Choice;
    Console.WriteLine();
    Console.WriteLine("Either:");
    Console.WriteLine("     enter the word you would like to play OR");
    Console.WriteLine("     press 1 to display the letter values OR");
    Console.WriteLine("     press 4 to view the tile queue OR");
    Console.WriteLine("     press 7 to view your tiles again OR");
    Console.WriteLine("     press 6 to sell 10 points for a vowel");
    Console.WriteLine("     press 0 to fill hand and stop the game.");
    Console.Write("> ");
    Choice = Console.ReadLine();
    Console.WriteLine();
    Choice = Choice.ToUpper();
    return Choice;
}

Firstly we must add our new command to our get choice method.

// ... Some stuff was here

else if (Choice == "6") {

}

// ... Some stuff is here

Once we've done that, we need to create a new conditional check in our HaveTurn method.

It's time to add the desired logic. Firstly, we must check if the player has more than 10 points.

// ... Some stuff was here

else if (Choice == "6") {
    if (PlayerScore < 10) Console.WriteLine("Not enough points available"); else {

    }
}

// ... Some stuff is here

Now the contents of the else statement is only run once the player has enough points.

// ... Some stuff was here

else if (Choice == "6") {
    if (PlayerScore < 10) Console.WriteLine("Not enough points available"); else {
        Random random = new Random(); // Random instance to get vowel

        String tileToRemove = "", vowel = new string[] { "A", "E", "I", "O", "U" }[random.Next(0, 4)];

        Console.WriteLine("Your Current Hand: {0}\n", PlayerTiles);

        do {
            Console.Write("\nPlease Input Which Tile You'd Like To Remove :> ");
            tileToRemove = Console.ReadLine().Trim().ToUpper(); // Read new tile from console stream
        } while (tileToRemove.Length != 1 && !PlayerTiles.Contains(tileToRemove));

        int removalTileIndex = PlayerTiles.IndexOf(tileToRemove); // Index of tile to replace

        PlayerTiles = PlayerTiles.Remove(removalTileIndex, 1).Insert(removalTileIndex, vowel);
    }
}

// ... Some stuff is here

Alternative Method

This is the existing method for a player's turn. You only need to add/edit one line of this method.

        private static void HaveTurn(string PlayerName, ref string PlayerTiles, ref int PlayerTilesPlayed, ref int PlayerScore, Dictionary<char, int> TileDictionary, ref QueueOfTiles TileQueue, List<string> AllowedWords, int MaxHandSize, int NoOfEndOfTurnTiles)
        {
            Console.WriteLine();
            Console.WriteLine(PlayerName + " it is your turn.");
            DisplayTilesInHand(PlayerTiles);
            string NewTileChoice = "2";
            bool ValidChoice = false;
            bool ValidWord = false;
            string Choice = "";
            while (!ValidChoice)
            {
                Choice = GetChoice();
                if (Choice == "1")
                {
                    DisplayTileValues(TileDictionary, AllowedWords);
                }
                else if (Choice == "4")
                {
                    TileQueue.Show();
                }
                else if (Choice == "7")
                {
                    DisplayTilesInHand(PlayerTiles);
                }
                else if (Choice == "0")
                {
                    ValidChoice = true;
                    FillHandWithTiles(ref TileQueue, ref PlayerTiles, MaxHandSize);
                }
                else
                {
                    ValidChoice = true;
                    if (Choice.Length == 0)
                    {
                        ValidWord = false;
                    }
                    else
                    {
                        ValidWord = CheckWordIsInTiles(Choice, PlayerTiles);
                    }
                    if (ValidWord)
                    {
                        ValidWord = CheckWordIsValid(Choice, AllowedWords);
                        if (ValidWord)
                        {
                            Console.WriteLine();
                            Console.WriteLine("Valid word");
                            Console.WriteLine();
                            UpdateAfterAllowedWord(Choice, ref PlayerTiles, ref PlayerScore, ref PlayerTilesPlayed, TileDictionary, AllowedWords);
                            Console.WriteLine("Your current score is: " + PlayerScore);
                            NewTileChoice = GetNewTileChoice();
                        }
                    }
                    if (!ValidWord)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Not a valid attempt, you lose your turn.");
                        Console.WriteLine();
                    }
                    if (NewTileChoice != "4")
                    {
                        AddEndOfTurnTiles(ref TileQueue, ref PlayerTiles, ref NewTileChoice, Choice, ref PlayerScore);
                    }
                    Console.WriteLine();
                    Console.WriteLine("Your word was:" + Choice);
                    Console.WriteLine("Your new score is:" + PlayerScore);
                    Console.WriteLine("You have played " + PlayerTilesPlayed + " tiles so far in this game.");
                }
            }
        }

This is a method to generate a random number, which is then used to select a random vowel:

private static string RandomVowel()
        { 
            string Vowel ="";
            Random RandomNumber = new Random();
            int Value = RandomNumber.Next(0, 4);
            switch (Value) 
            {
                case 0:
                    Vowel = "A";
                    break;
                case 1:
                    Vowel = "E";
                    break;
                case 2:
                    Vowel = "I";
                    break;
                case 3:
                    Vowel = "O";
                    break;
                case 4:
                    Vowel = "U";
                    break;
                default:
                    Vowel = "A";
                    break;
            }
            return Vowel;
        }
        private static void AddEndOfTurnTiles(ref QueueOfTiles TileQueue, ref string PlayerTiles, ref string NewTileChoice, string Choice, ref int PlayerScore)
        {
            int NoOfEndOfTurnTiles = 0;
            
            if (NewTileChoice == "1")
            {
                NoOfEndOfTurnTiles = Choice.Length;
            }
            else if (NewTileChoice == "2")
            {
                NoOfEndOfTurnTiles = 3;
            }
            else if (NewTileChoice == "3")
            {
                NoOfEndOfTurnTiles = Choice.Length + 3;
            }
            else if (NewTileChoice == "5")
            {

                if (PlayerScore >= 10)
                {
                    PlayerTiles = PlayerTiles + RandomVowel();
                    PlayerScore = PlayerScore - 10;
                }
                else
                {
                    Console.WriteLine("You do not have enough points to make this choice.");
                    Console.WriteLine("");
                    NewTileChoice = GetNewTileChoice();
                }
   
            }
      
            for (int Count = 0; Count < NoOfEndOfTurnTiles; Count++)
            {
                PlayerTiles = PlayerTiles + TileQueue.Remove();
                TileQueue.Add();
            }
        }
        private static string GetNewTileChoice()
        {
            string NewTileChoice = "";
            string[] TileChoice = { "1", "2", "3", "4", "5" };
            while (Array.IndexOf(TileChoice, NewTileChoice) == -1)
            {
                Console.WriteLine("Do you want to:");
                Console.WriteLine("     replace the tiles you used (1) OR");
                Console.WriteLine("     get three extra tiles (2) OR");
                Console.WriteLine("     replace the tiles you used and get three extra tiles (3) OR");
                Console.WriteLine("     get no new tiles (4) OR");
                Console.WriteLine("     get a random vowel for 10 points (5)?");
                Console.Write("> ");
                NewTileChoice = Console.ReadLine();
            }
            return NewTileChoice; ;
        }