用c#写一个斗地主小游戏
时间: 2024-01-28 20:03:57 浏览: 41
好的,我可以提供一个简单的 C# 控制台斗地主小游戏的示例,让你了解如何使用 C# 编写一个斗地主游戏。注意,这只是一个简化版的斗地主游戏,仅用于演示用途。
下面是完整的代码:
```csharp
using System;
using System.Collections.Generic;
namespace DouDiZhuGame
{
class Program
{
static void Main(string[] args)
{
// 初始化扑克牌
List<string> poker = new List<string>();
string[] colors = { "♠", "♥", "♣", "♦" };
string[] nums = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
foreach (string color in colors)
{
foreach (string num in nums)
{
poker.Add(color + num);
}
}
poker.Add("小王");
poker.Add("大王");
// 洗牌
Random random = new Random();
for (int i = poker.Count - 1; i > 0; i--)
{
int j = random.Next(i + 1);
string temp = poker[i];
poker[i] = poker[j];
poker[j] = temp;
}
// 发牌
List<string> player1 = new List<string>();
List<string> player2 = new List<string>();
List<string> player3 = new List<string>();
List<string> dipai = new List<string>();
for (int i = 0; i < poker.Count; i++)
{
if (i >= poker.Count - 3)
{
dipai.Add(poker[i]);
}
else if (i % 3 == 0)
{
player1.Add(poker[i]);
}
else if (i % 3 == 1)
{
player2.Add(poker[i]);
}
else if (i % 3 == 2)
{
player3.Add(poker[i]);
}
}
// 玩家1出牌
Console.WriteLine("玩家1的手牌:");
foreach (string card in player1)
{
Console.Write(card + " ");
}
Console.WriteLine();
Console.Write("玩家1出牌:");
string input = Console.ReadLine();
List<string> play1Cards = new List<string>(input.Split(" "));
// 玩家2出牌
Console.WriteLine("玩家2的手牌:");
foreach (string card in player2)
{
Console.Write(card + " ");
}
Console.WriteLine();
Console.Write("玩家2出牌:");
input = Console.ReadLine();
List<string> play2Cards = new List<string>(input.Split(" "));
// 玩家3出牌
Console.WriteLine("玩家3的手牌:");
foreach (string card in player3)
{
Console.Write(card + " ");
}
Console.WriteLine();
Console.Write("玩家3出牌:");
input = Console.ReadLine();
List<string> play3Cards = new List<string>(input.Split(" "));
// 比较牌的大小
Console.WriteLine("玩家1的出牌:");
foreach (string card in play1Cards)
{
Console.Write(card + " ");
}
Console.WriteLine();
Console.WriteLine("玩家2的出牌:");
foreach (string card in play2Cards)
{
Console.Write(card + " ");
}
Console.WriteLine();
Console.WriteLine("玩家3的出牌:");
foreach (string card in play3Cards)
{
Console.Write(card + " ");
}
Console.WriteLine();
Console.ReadLine();
}
}
}
```
运行程序后,控制台会输出玩家的手牌,并提示玩家出牌。玩家需要输入要出的牌,以空格分隔每张牌。程序会将玩家出的牌存储在一个列表中,并按顺序比较三个玩家出的牌的大小。最后程序会输出每个玩家出的牌和比较结果。
希望这个示例可以帮助你了解如何使用 C# 编写一个斗地主小游戏!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)