所以从本质上讲,我希望displayToPlayer
变量具有自动添加的空格,但是我不确定如何实现。
如何在不使用转弯的情况下将空格自动添加到隐藏的消息中,或者将其添加到猜到的字母提示中。
当前结果:
所需结果:
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);
}
}
}
}
答案 0 :(得分:3)
如果我对您的理解正确,这可能会起作用
const food = ratings.reduce((sum, { food }) => sum + food, 0) / ratings.length;
console.log("FOOD", food)
基本上是说,如果for (int i = 0; i < quoteToGuess.Length; i++)
{
displayToPlayer.Append(quoteToGuess[i] == ' ' ? ' ' : '*');
}
中i
的索引处的字符是quoteToGuess
,则添加' '
,如果不添加' '
>
条件运算符(?:),通常称为三元运算符 条件运算符,根据值返回两个值之一 布尔表达式。以下是条件句的语法 运算符。
索引是Char对象(不是Unicode字符)在以下位置的位置 一个字符串。索引是从零开始的非负数 从字符串的第一个位置开始,即索引位置零
答案 1 :(得分:1)
您可以使用StringBuilder
摆脱System.Linq
并完全使用for循环:
var displayToPlayer = new string(quoteToGuess.Select(c => c == ' ' ? ' ' : '*').ToArray());
如果由于似乎未定义Select()
而出现错误,请将其添加到文件顶部:
using System.Linq;