不要猜数字和猜单词 请举出别的例子
时间: 2024-04-12 22:30:08 浏览: 83
当然,我可以给你举一个不涉及猜数字和猜单词的小游戏的例子。以下是一个用 C# 编写的石头、剪刀、布游戏的源代码示例:
```csharp
using System;
class Program
{
static void Main()
{
Console.WriteLine("欢迎来到石头、剪刀、布游戏!");
Console.WriteLine("请选择:1. 石头 | 2. 剪刀 | 3. 布");
while (true)
{
string playerChoice = Console.ReadLine();
int playerSelection;
if (int.TryParse(playerChoice, out playerSelection) && playerSelection >= 1 && playerSelection <= 3)
{
string computerChoice = GetComputerChoice();
Console.WriteLine($"你选择了:{GetGameItemName(playerSelection)}");
Console.WriteLine($"电脑选择了:{computerChoice}");
int result = DetermineWinner(playerSelection, GetGameItemNumber(computerChoice));
if (result == 0)
{
Console.WriteLine("平局!");
}
else if (result == 1)
{
Console.WriteLine("恭喜你,你赢了!");
}
else
{
Console.WriteLine("很遗憾,你输了!");
}
break;
}
else
{
Console.WriteLine("无效的选择,请重新选择:1. 石头 | 2. 剪刀 | 3. 布");
}
}
Console.WriteLine("游戏结束!");
}
static string GetComputerChoice()
{
Random random = new Random();
int choice = random.Next(1, 4);
return GetGameItemName(choice);
}
static string GetGameItemName(int choice)
{
switch (choice)
{
case 1:
return "石头";
case 2:
return "剪刀";
case 3:
return "布";
default:
return "";
}
}
static int GetGameItemNumber(string choice)
{
switch (choice)
{
case "石头":
return 1;
case "剪刀":
return 2;
case "布":
return 3;
default:
return -1;
}
}
static int DetermineWinner(int playerSelection, int computerSelection)
{
if (playerSelection == computerSelection)
{
return 0; // 平局
}
else if ((playerSelection == 1 && computerSelection == 2) ||
(playerSelection == 2 && computerSelection == 3) ||
(playerSelection == 3 && computerSelection == 1))
{
return 1; // 玩家赢
}
else
{
return -1; // 玩家输
}
}
}
```
在这个代码示例中,我们实现了一个简单的石头、剪刀、布游戏。玩家需要选择其中一个选项(石头、剪刀或布),然后与电脑进行比较,判断胜负。
游戏开始时,我们打印出欢迎消息和选项提示。然后,使用一个无限循环来接收玩家的选择。玩家输入的选择会被解析为整数,并进行有效性检查。
如果玩家的选择有效,我们会获取电脑的选择,并打印出玩家和电脑的选择结果。接下来,我们使用 `DetermineWinner` 函数来判断胜负,并打印出相应的消息。
最后,游戏结束并打印出结束消息。
这只是一个简单的示例,你可以根据自己的需要进行修改和扩展。希望这个代码对你有帮助!如果你有更多问题,请随时提问。
阅读全文
相关推荐


















