剪刀石头布求代码优化
如题,以下是代码,感觉特别不简洁,虽然能运行. 本人新入门C#求指点.谢谢
private void Winlost(int Cpuhand,string Userhand)//形参,电脑,人出手情况
{//电脑 :0剪刀1石头2布 人:j剪刀s石头b布
if(this.cpuhand==0)//如果电脑出剪刀,对比人的三种情况
{
if (this.userhand == "j")
{
Console.WriteLine("平");
}
if (this.userhand == "s")
{
this.userwin++;
Console.WriteLine("你赢了);
}
if (this.userhand == "b")
{
this.cpuwin++;
Console.WriteLine("电脑赢);
}
}
if (this.cpuhand == 1)//如果出布,对比
{
if (this.userhand == "s")
{
Console.WriteLine("平");
}
if (this.userhand == "b")
{
this.userwin++;
Console.WriteLine("你赢了);
}
if (this.userhand == "j")
{
this.cpuwin++;
Console.WriteLine("电脑赢);
}
}
if (this.cpuhand == 2)//如果出石头,对比
{
if (this.userhand == "b")
{
Console.WriteLine("平");
}
if (this.userhand == "j")
{
this.userwin++;
Console.WriteLine("你赢了);
}
if (this.userhand == "s")
{
this.cpuwin++;
Console.WriteLine("电脑赢);
}
}
}
[解决办法]
人手电脑都统一编成数字,python代码
a = ((0,1,2),
(2,0,1),
(1,2,0))
b = ("平","你赢了","电脑赢")
print(b[a[cpuhand][playerhand]])