c#飞行棋四人版的代码
时间: 2023-10-28 20:20:54 浏览: 136
Sure! 下面是一个简单的 C# 飞行棋四人版的代码示例:
```csharp
using System;
class Program
{
static int[] players;
static int[] positions;
static int[] goals;
static void Main(string[] args)
{
InitializeGame();
PlayGame();
}
static void InitializeGame()
{
int numPlayers = 4;
int numCells = 30;
players = new int[numPlayers];
positions = new int[numPlayers];
goals = new int[numPlayers];
for (int i = 0; i < numPlayers; i++)
{
players[i] = i + 1;
positions[i] = 0;
goals[i] = numCells;
}
}
static void PlayGame()
{
Random random = new Random();
while (!HasWinner())
{
for (int i = 0; i < players.Length; i++)
{
Console.WriteLine($"Player {players[i]}: Press any key to roll the dice.");
Console.ReadKey();
int diceValue = random.Next(1, 7);
positions[i] += diceValue;
if (positions[i] >= goals[i])
{
positions[i] = goals[i];
Console.WriteLine($"Player {players[i]} has reached the goal!");
}
else
{
Console.WriteLine($"Player {players[i]} rolled a {diceValue}. New position: {positions[i]}");
}
if (HasWinner())
{
Console.WriteLine($"Player {players[i]} wins the game!");
break;
}
}
}
Console.WriteLine("Game Over!");
}
static bool HasWinner()
{
foreach (int position in positions)
{
if (position == goals[0])
return true;
}
return false;
}
}
```
这是一个简单的飞行棋的代码示例,基于控制台应用程序。游戏开始时会要求每位玩家按任意键掷骰子,然后根据骰子的点数移动对应的步数。第一个达到终点的玩家将被宣布为获胜者。请注意,此示例仅用于简单的演示目的,你可以根据自己的需求进行扩展和优化。
阅读全文