C# 实现控制台猜拳游戏(新手入门程序)
class Program
{
/// <summary>
/// 剪刀 用0表示
/// </summary>
private const int JIANDAO = 0;
/// <summary>
/// 石头用 1表示
/// </summary>
private const int SHITOU = 1;
/// <summary>
/// 布用2表示
/// </summary>
private const int BU = 2;
//电脑的积分,积分是一个全局变量,在任何地方修改,都要改变值,所以用static修饰
private static int pcIntegral;
//玩家的积分
private static int userIntegral;
static void Main(string[] args)
{
int count = 0; // 场次
Random random = new Random(); //实例化对象
while (++count <= 3)
{
Console.WriteLine("第{0}局比赛", count);
Console.WriteLine("请玩家先出拳,剪刀[0],石头[1],布[2] ");
int pc = random.Next(0, 3);
String input = Console.ReadLine();
int user = int.Parse(input);
if (user < 0 || user >= 3)
{
Console.WriteLine("请输入0到2之间的数字,本局无效\n");
count--; //回滚
continue; //跳过当前循环
}
//调用判断输赢的函数
Judge(user, pc);
}
Console.WriteLine("\n--------------最终的得分情况如下---------------");
Console.WriteLine("玩家得分{0}", userIntegral);
Console.WriteLine("PC得分{0}", pcIntegral);
// 防止程序运行完毕以后直接关闭
Console.ReadKey();
}
/// <summary>
/// 返回猜拳结果
/// </summary>
/// <param name="user">用户出的拳</param>
/// <param name="pc">电脑的出的全</param>
/// <returns></returns>
private static void Judge(int user, int pc)
{
#region pc 全部出剪刀的情况
if (pc == JIANDAO && user == JIANDAO)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局为平局", "剪刀", "剪刀");
}
if (pc == JIANDAO && user == SHITOU)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局玩家胜利,胜场积分加一", "剪刀", "石头");
userIntegral++;
}
if (pc == JIANDAO && user == BU)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局PC胜利,胜场积分加一", "剪刀", "布");
pcIntegral++;
}
#endregion
#region PC 全部出石头的情况
if (pc == SHITOU && user == JIANDAO)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局PC胜利,胜场积分加一", "石头", "剪刀");
pcIntegral++;
}
if (pc == SHITOU && user == SHITOU)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局为平局", "石头", "石头");
}
if (pc == SHITOU && user == BU)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局玩家胜利,胜场积分加一", "石头", "布");
userIntegral++;
}
#endregion
#region PC全部出布的情况
if (pc == BU && user == JIANDAO)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局玩家胜利,胜场积分加一", "布", "剪刀");
userIntegral++;
}
if (pc == BU && user == SHITOU)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局PC胜利,胜场积分加一", "布", "石头");
pcIntegral ++;
}
if (pc == BU && user == BU)
{
Console.WriteLine("PC出的{0},玩家出的{1},本局为平局", "布", "布");
}
#endregion
}
}