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

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 177: Line 177:
 
                 Console.WriteLine("    replace the tiles you used and get three extra tiles (3) 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 no new tiles (4) OR");
 +
                //This is the start of what you need to add
 
                 Console.WriteLine("    get a random vowel for 10 points (5)?");
 
                 Console.WriteLine("    get a random vowel for 10 points (5)?");
 +
                //This is the end of what you need to add
 
                 Console.Write("> ");
 
                 Console.Write("> ");
 
                 NewTileChoice = Console.ReadLine();
 
                 NewTileChoice = Console.ReadLine();
Line 185: Line 187:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
This is a method to generate a random number, which is then used to select a random vowel:
+
This is a possible method to generate a random number, which is then used in the switch case statement to select a random vowel. You need to create this entire method, or something that serves a similar function.
 
<syntaxhighlight lang="c#">
 
<syntaxhighlight lang="c#">
 
private static string RandomVowel()
 
private static string RandomVowel()
Line 217: Line 219:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
This existing method uses the Player's NewTileChoice
+
This existing method uses the Player's NewTileChoice to add the appropriate amount of tiles. The if statement for options 1, 2 and 3 ("replace tiles used in last turn", "add three tiles regardless of tiles used in last turn", or "replace tiles used in last turn and add three additional tiles") already exists. <br>
 +
You need to add an extra option to the if statement for Players that choose option 5 ("get one vowel tile in exchange for 10 points"). <br>
 +
Within the extra option, you need to add another if statement which checks whether the player has 10 points. If they have 10 points, the RandomVowel method is used to select a random vowel, add it to the string of PlayerTiles and subtract 10 points from the PlayerScore. <br>
 +
If the player has less than 10 points, they are unable to purchase a vowel - a Console.WriteLine() is used to tell them this, and then the method GetNewTileChoice() is called again so that the Player can pick a different option. For GetNewTileChoice() to work within AddEndofTurnTiles, you need to edit the list of variables at the top of the method. Add "ref" in front of "string NewTileChoice"
 
<syntaxhighlight lang="c#">
 
<syntaxhighlight lang="c#">
 +
//Add ref in front of "string NewTileChoice"
 
         private static void AddEndOfTurnTiles(ref QueueOfTiles TileQueue, ref string PlayerTiles, ref string NewTileChoice, string Choice, ref int PlayerScore)
 
         private static void AddEndOfTurnTiles(ref QueueOfTiles TileQueue, ref string PlayerTiles, ref string NewTileChoice, string Choice, ref int PlayerScore)
 
         {
 
         {
Line 238: Line 244:
 
             else if (NewTileChoice == "5")
 
             else if (NewTileChoice == "5")
 
             {
 
             {
 
 
                 if (PlayerScore >= 10)
 
                 if (PlayerScore >= 10)
 
                 {
 
                 {

Revision as of 10:06, 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);
                            //This is the start of what you need to add
                            Console.WriteLine("Your current score is: " + PlayerScore);
                            //This is the end of what you need to add
                            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.");
                }
            }
        }
        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");
                //This is the start of what you need to add
                Console.WriteLine("     get a random vowel for 10 points (5)?");
                //This is the end of what you need to add
                Console.Write("> ");
                NewTileChoice = Console.ReadLine();
            }
            return NewTileChoice; ;
        }

This is a possible method to generate a random number, which is then used in the switch case statement to select a random vowel. You need to create this entire method, or something that serves a similar function.

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;
        }

This existing method uses the Player's NewTileChoice to add the appropriate amount of tiles. The if statement for options 1, 2 and 3 ("replace tiles used in last turn", "add three tiles regardless of tiles used in last turn", or "replace tiles used in last turn and add three additional tiles") already exists.
You need to add an extra option to the if statement for Players that choose option 5 ("get one vowel tile in exchange for 10 points").
Within the extra option, you need to add another if statement which checks whether the player has 10 points. If they have 10 points, the RandomVowel method is used to select a random vowel, add it to the string of PlayerTiles and subtract 10 points from the PlayerScore.
If the player has less than 10 points, they are unable to purchase a vowel - a Console.WriteLine() is used to tell them this, and then the method GetNewTileChoice() is called again so that the Player can pick a different option. For GetNewTileChoice() to work within AddEndofTurnTiles, you need to edit the list of variables at the top of the method. Add "ref" in front of "string NewTileChoice"

//Add ref in front of "string NewTileChoice"
        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;
            }
            //This is the start of what you need to add
            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();
                }
            }
            //This is the end of what you need to add
      
            for (int Count = 0; Count < NoOfEndOfTurnTiles; Count++)
            {
                PlayerTiles = PlayerTiles + TileQueue.Remove();
                TileQueue.Add();
            }
        }