猜单词小游戏
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猜单词小游戏相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace GuessWord
{
class Program
{
static int wrongGuess, lettersLeft;
static void Main(string[] args)
{
string wordToGuess = GetWordToGuess();
char[] maskedWord = GetHiddenLetters(wordToGuess, ‘-‘);
lettersLeft = wordToGuess.Length;
char userGuess;
wrongGuess = 3;
while (wrongGuess > 0 && lettersLeft > 0)
{
DisplayCharacters(maskedWord);
Console.WriteLine("Enter a letter?");
userGuess = char.Parse(Console.ReadLine());
maskedWord = CheckGuess(userGuess, wordToGuess, maskedWord);
}
Console.WriteLine("Well done! Thanks for playing.");
Console.ReadLine();
}
static string GetWordToGuess()
{
Random number = new Random();
int wordNumber = number.Next(0, 9);
string[] words = { "picture", "chinese", "school", "question", "include", "simple", "difficult", "understand", "necessary", "support" };
string selectWord = words[wordNumber];
return selectWord;
}
static char[] GetHiddenLetters(string word, char mask)
{
char[] hidden = new char[word.Length];
for (int i = 0; i < word.Length; i++)
{
hidden[i] = mask;
}
return hidden;
}
static void DisplayCharacters(char[] characters)
{
foreach (char letter in characters)
{
Console.Write(letter);
}
Console.WriteLine();
}
static char[] CheckGuess(char letterToCheck, string word, char[] characters)
{
if (word.Contains(letterToCheck))
{
for (int i = 0; i < word.Length; i++)
{
if (word[i] == letterToCheck)
{
characters[i] = word[i];
lettersLeft--;
}
}
}
else
{
wrongGuess--;
}
return characters;
}
}
}
以上是关于猜单词小游戏的主要内容,如果未能解决你的问题,请参考以下文章