2

So essentially I want the displayToPlayer variable have the spaces automatically added but I'm not quite sure how to achieve this.

How can I have the spaces automatically added to the hidden message without using a turn or it been added to the guessed letter tally.

Current Result:

Current Result

Desired Result:

Desired Result

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace guessingGame
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] quoteList = {
                "NEVER GUNNA LET YOU DOWN",
                /*"NEVER GUNNA RUN AROUND",
                "THIS IS A TEST"*/
            };   // the list of quotes that need to be guessed by the player

            Random random = new Random((int)DateTime.Now.Ticks);
            string quoteToGuess = quoteList[random.Next(0, quoteList.Length)];
            string quoteToGuessUppercase = quoteToGuess.ToUpper();

            StringBuilder displayToPlayer = new StringBuilder(quoteToGuess.Length);
            for (int i = 0; i < quoteToGuess.Length; i++)
            {
                displayToPlayer.Append('*');
            }

            List<char> correctGuesses = new List<char>();
            List<char> incorrectGuesses = new List<char>();

            int quoteToGuessLength = quoteToGuess.Count(characters => !Char.IsWhiteSpace(characters));
            int turnsLeft = quoteToGuess.Distinct().Count(characters => !Char.IsWhiteSpace(characters)) + 3;
            bool quoteGuessed = false;
            int lettersRevealed = 0;

            string userInput;
            char userGuess;
            Console.WriteLine("Can you work out the quote? (you have {0} chances)", turnsLeft);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(displayToPlayer.ToString());
            Console.WriteLine();

            while (!quoteGuessed && turnsLeft > 0)
            {
                Console.Write("Enter your letter ==>");
                try
                {
                    userInput = Console.ReadLine().ToUpper();
                    userGuess = userInput[0];

                    if (correctGuesses.Contains(userGuess))
                    {
                        Console.WriteLine("You've already tried '{0}', and it was correct!", userGuess);
                        continue;
                    }
                    else if (incorrectGuesses.Contains(userGuess))
                    {
                        Console.WriteLine("You've already tried '{0}', and it was wrong!", userGuess);
                        continue;
                    }

                    if (quoteToGuessUppercase.Contains(userGuess))
                    {
                        correctGuesses.Add(userGuess);

                        for (int i = 0; i < quoteToGuess.Length; i++)
                        {
                            if (quoteToGuessUppercase[i] == userGuess)
                            {
                                displayToPlayer[i] = quoteToGuess[i];
                                lettersRevealed++;
                            }
                        }

                        if (lettersRevealed == quoteToGuess.Length)
                        {
                            quoteGuessed = true;
                        }

                        turnsLeft--;
                        Console.Clear();
                        Console.WriteLine("You have guessed {0} letter(s) out of a total of {1}", lettersRevealed, quoteToGuessLength);
                        Console.WriteLine("You have {0} attempts remaining!", turnsLeft);
                        Console.WriteLine();
                        Console.WriteLine(displayToPlayer.ToString());
                        Console.WriteLine();
                        Console.WriteLine("Correct guess, there's a '{0}' in it!", userGuess);
                        Console.WriteLine();

                    }
                    else
                    {
                        incorrectGuesses.Add(userGuess);
                        turnsLeft--;
                        Console.Clear();
                        Console.WriteLine("You have guessed {0} letter(s) out of a total of {1}", lettersRevealed, quoteToGuessLength);
                        Console.WriteLine("You have {0} attempts remaining!", turnsLeft);
                        Console.WriteLine();
                        Console.WriteLine(displayToPlayer.ToString());
                        Console.WriteLine();
                        Console.WriteLine("Incorrect guess, there's no '{0}' in it!", userGuess);
                        Console.WriteLine();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Enter A Valid Character");
                }
            }

            if (quoteGuessed)
            {
                Console.WriteLine(quoteToGuess);
                Console.WriteLine("You have guessed all {0} letters out of a total of {0} Well done!!!", quoteToGuessLength);
            }
            else
            {
                Console.WriteLine("You lost! It was '{0}'", quoteToGuess);
            }
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
George
  • 31
  • 2

2 Answers2

3

If i understand you correctly, this will probably work

for (int i = 0; i < quoteToGuess.Length; i++)
{
    displayToPlayer.Append(quoteToGuess[i] == ' ' ? ' ' : '*');
}

Its basically saying, if the character at the index of i in quoteToGuess is ' ', then add ' ', if not add '*'


?: Operator (C# Reference)

The conditional operator (?:), commonly known as the ternary conditional operator, returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

Strings and indexes

An index is the position of a Char object (not a Unicode character) in a String. An index is a zero-based, nonnegative number that starts from the first position in the string, which is index position zero

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • BTW, using `StringBuilder` is not needed. Regex.Replace should work fine like this: `Regex.Replace(input, "[^ ]")` – Furkan Kambay Sep 26 '18 at 09:18
  • 1 Small problem I noticed with this solution was it still requires the user to guess the space – George Sep 26 '18 at 09:19
  • @BradleyWilson - If you use something like `nonSpaceChars = quoteToGuess.Replace(" ", "").Length;` you can get the length of the string without chars. – Paul Sep 26 '18 at 09:24
1

You can get rid of the StringBuilder and your for loop completely using System.Linq:

var displayToPlayer = new string(quoteToGuess.Select(c => c == ' ' ? ' ' : '*').ToArray());

If you get an error because Select() doesn't seem to be defined, add this on top of your file:

using System.Linq;
Emaro
  • 1,397
  • 1
  • 12
  • 21