[英]Check ReadKey only for letters (alphabet) in string
i am doing game "Hangman" but i have a problem.我正在做游戏“刽子手”,但我有一个问题。 I am using ReadKey to find the guess of player, but he can use even numbers or buttons like "Enter" etc. I want to stop this, but i don't know how :/ Can you help me please?我正在使用 ReadKey 来找到玩家的猜测,但他可以使用偶数或“Enter”等按钮。我想停止这个,但我不知道如何:/你能帮我吗? I want control, if in result of ReadKey is letter from alphabet or no (but it must be in "string" format, cuz i am working later with string).我想要控制,如果 ReadKey 的结果是字母表中的字母或不是(但它必须是“字符串”格式,因为我稍后会使用字符串)。 Thank you very much guys, here is my code :)非常感谢你们,这是我的代码:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hangman
{
class MainClass
{
/// <summary>
/// Hra Oběšenec
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("Vítejte ve hře Oběšenec!");
//List slov, které bude hráč hádat
string[] words = new string[5];
words[0] = "car";
words[1] = "school";
words[2] = "apple";
words[3] = "orange";
words[4] = "xamarin";
//Získávám délku slova (hádaného slova)
for (int X = 0; X < words.Length; X++)
{
string guessWord = words[X];
short lifes = 5;
short guessedLetters = 0;
char GuessingLetter;
char[] lettersOfWord = new char[guessWord.Length];
//
for (int I = 0; I < guessWord.Length; I++)
{
//Length of word is changing to *
lettersOfWord[I] = '*';
}
bool InGame = true;
//When this while is true, the game is still going, when it will be false, the game will end
while (InGame)
{
//Ochrana proti špatnému inputu
try
{
Console.WriteLine(lettersOfWord);
Console.WriteLine("Stiskněte klávesu. Životy:" + lifes);
ConsoleKeyInfo result_guess = Console.ReadKey(true);
string result = result_guess.KeyChar.ToString();
if (result.Length == 0)
{
continue;
}
//Hráč hádá slovo
GuessingLetter = result[0];
lifes--;
for (int I = 0; I < lettersOfWord.Length; I++)
{
if (lettersOfWord[I] == '*')
{
//Pokud hráč uhádne písmenko ve slově
if (guessWord[I] == GuessingLetter)
{
lettersOfWord[I] = GuessingLetter;
guessedLetters++;
lifes++;
Console.WriteLine("Správně!");
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Nastala neočekávaná chyba!" + e);
}
//Pokud prohraje(promrhá všechny pokusy) hra skončí, avšak pokud slovo uhádne (a zůstane alespoň jeden život) vyhrál hru
if (lifes== 0 || guessedLetters == guessWord.Length)
{
//Konec hry
InGame = false;
if (lifes == 0)
{
Console.WriteLine("Smůla prohrál jsi!");
Console.ReadKey();
break;
}
else
{
//Vítězství nad levlem
Console.WriteLine("Uhodnul jsi celé slovo! Gratuluji!!");
}
}
}
}
}
}
}
Check if the keychar is a letter with the IsLetter method and keep asking for letters if it doesn't.使用 IsLetter 方法检查 keychar 是否为字母,如果不是,则继续询问字母。 Something like this:像这样的东西:
private char GetLetter()
{
while (true)
{
Console.WriteLine("Please input a letter.");
var character = Console.ReadKey().KeyChar;
if (char.IsLetter(character))
return character;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.