博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
猜单词小游戏
阅读量:5238 次
发布时间:2019-06-14

本文共 1637 字,大约阅读时间需要 5 分钟。

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;

}
}
}

转载于:https://www.cnblogs.com/yuqianyi604811/p/6534490.html

你可能感兴趣的文章
110104_LC-Display(液晶显示屏)
查看>>
javascript全局变量
查看>>
全连接神经网络(DNN)
查看>>
httpd_Vhosts文件的配置
查看>>
php学习笔记
查看>>
28 hashlib 模块 logging 模块 和 configparser模块 functools模块的偏函数partial
查看>>
普通求素数和线性筛素数
查看>>
React Router 4.0 基本使用
查看>>
作业完成2
查看>>
PHP截取中英文混合字符
查看>>
HTA - OnKeyDown
查看>>
【洛谷P1816 忠诚】线段树
查看>>
CDN 学习笔记
查看>>
电子眼抓拍大解密
查看>>
Linux系统下 /etc/shadow 档案结构
查看>>
多线程---线程间的通信
查看>>
poj 1331 Multiply
查看>>
严重: 文档无效: 找不到语法。 at (null:2:19)
查看>>
tomcat7的数据库连接池tomcatjdbc的25个优势
查看>>
Html 小插件5 百度搜索代码2
查看>>