高效掌握C#第五回---猜单词游戏
前四回介绍了c#常用的数据类型,数组,字符串的常用操作,本回书进入实践环节,综合运用前面讲到的这些知识,实践一个控制台小游戏。
以下是需求说明:
/*
Playing a console-based game
To solve the problem, your program must be able to:
Choose a random word to use as the secret word. That word is chosen from a word list, as described in the following paragraph.
Keep track of the user’s partially guessed word, which begins as a series of dashes and then updated as correct letters are guessed.
Implement the basic control structure and manage the details (ask the user to guess a letter, keep track of the number of guesses remaining, print out the various messages, detect the end of the game, and so forth)
*/
简单说需求就是从一个单词集合中随机选取一个单词, 假设猜想单词school, 就在控制台上输出 _ _ _ _ _ _,根据英语普遍规律,绝大部分的单词都有元音字母,所以从元音字母开始猜比较容易。假设用户猜字母O,单词school中出现了,就在单词上预留的位置中填写此字母,_ _ _ o o _ 当用户们猜测的单词为school中并未出现的字母,则将在控制台中输出最近一次单词的状态。若给定猜测次数内,单词仍未猜出,则系统获胜,若猜出此单词,则为用户获胜。
java版本实现
public class Main { //1一个单词数组(字符串数组) //2随机获取一个单词,随机一个下标(下标值在0到单词数组长度之间) //3得到单词长度len,创建一个长度为len的字符数组word,数组元素初值均为- //4循环以下步骤(退出循环的条件为,用户允许猜错的次数<=0或word字符中数组中不再包含-字符) //5输出word到屏幕 //6获取客户输入,判断随机选出的单词中是否包含用户输入的字符 //7若包含,得到字符在单词中出现的位置,并将该位置的字符替换成用户输入的字符,返回到4 //8若不包含,用户可以猜错的次数减一,返回到4 //9.得出最后结论:判断用户允许猜错的次数是否大于0,如果大于0,输出用户胜利,否则输出用户失败 public static boolean isContaisChar(char[] array, char c) { boolean bFind = false; for(int i = 0; i<array.length; i++) { if(array[i] == c) { bFind = true; break; } } return bFind; } public static void main(String[] args) { // write your code here //1定义一个单词数组(字符串数组) String[] words = {"picture", "chinese", "school", "question", "include", "simple", "difficult", "understand", "necessary", "support" }; //2随机获取一个单词,随机一个下标(下标值在0到单词数组长度之间) Random random = new Random(); int nRandomIndex = random.nextInt(words.length); String strWordChoose = words[nRandomIndex]; //3得到单词长度len,创建一个长度为len的字符数组word,数组元素初值均为- int nLen = strWordChoose.length(); char[] wordShow = new char[nLen]; for(int i = 0 ; i < nLen; i++) { wordShow[i] = '-'; } //4循环以下步骤(退出循环的条件为,用户允许猜错的次数<=0或word字符中数组中不再包含-字符) int nGuessTimes = 3; while(nGuessTimes > 0 && isContaisChar(wordShow,'-')) { //5输出word到屏幕 System.out.println(wordShow); //6获取客户输入,判断随机选出的单词中是否包含用户输入的字符 Scanner sc = new Scanner(System.in); String c = sc.nextLine(); //7若包含,得到字符在单词中出现的位置,并将该位置的字符替换成用户输入的字符,返回到4 boolean bFind = false; for(int i = 0; i < wordShow.length; i++) { if(strWordChoose.charAt(i) == c.toCharArray()[0]) { wordShow[i] = c.toCharArray()[0]; bFind = true; } } //8若不包含,用户可以猜错的次数减一,返回到4 if(!bFind) { nGuessTimes--; System.out.println("您还有"+nGuessTimes+"次机会"); } } //9.得出最后结论:判断用户允许猜错的次数是否大于0,如果大于0,输出用户胜利,否则输出用户失败 if(nGuessTimes > 0) { System.out.println("you win"); } else { System.out.println("you lose"); } } }
以下给出代码实现(C#):
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Diagnostics; 7 8 namespace GuessWord 9 { 10 class Program 11 { 12 static int wrongGuess, lettersLeft; 13 14 static void Main(string[] args) 15 { 16 string wordToGuess = GetWordToGuess(); 17 18 char[] maskedWord = GetHiddenLetters(wordToGuess, '-'); 19 20 lettersLeft = wordToGuess.Length; 21 char userGuess; 22 23 wrongGuess = 3; 24 25 while (wrongGuess > 0 && lettersLeft > 0) 26 { 27 DisplayCharacters(maskedWord); 28 29 Console.WriteLine("Enter a letter?"); 30 userGuess = char.Parse(Console.ReadLine()); 31 32 maskedWord = CheckGuess(userGuess, wordToGuess, maskedWord); 33 } 34 35 Console.WriteLine("Well done! Thanks for playing."); 36 Console.ReadLine(); 37 } 38 39 static string GetWordToGuess() 40 { 41 Random number = new Random(); 42 int wordNumber = number.Next(0, 9); 43 44 string[] words = { "picture", "chinese", "school", "question", "include", "simple", "difficult", "understand", "necessary", "support" }; 45 46 string selectWord = words[wordNumber]; 47 return selectWord; 48 } 49 50 static char[] GetHiddenLetters(string word, char mask) 51 { 52 char[] hidden = new char[word.Length]; 53 54 for (int i = 0; i < word.Length; i++) 55 { 56 hidden[i] = mask; 57 } 58 59 return hidden; 60 } 61 62 static void DisplayCharacters(char[] characters) 63 { 64 foreach (char letter in characters) 65 { 66 Console.Write(letter); 67 } 68 Console.WriteLine(); 69 } 70 71 static char[] CheckGuess(char letterToCheck, string word, char[] characters) 72 { 73 if (word.Contains(letterToCheck)) 74 { 75 for (int i = 0; i < word.Length; i++) 76 { 77 if (word[i] == letterToCheck) 78 { 79 characters[i] = word[i]; 80 lettersLeft--; 81 } 82 } 83 } 84 else 85 { 86 wrongGuess--; 87 } 88 89 return characters; 90 } 91 } 92 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?