C# 有没有不带if/else/switch/的公式?等,可以替代溶液[,]矩阵
有没有不带if/else/switch/的公式?可能替代解决方案[,]矩阵的etc,以及这对性能/效率的影响或差异是什么C# 有没有不带if/else/switch/的公式?等,可以替代溶液[,]矩阵,c#,math,C#,Math,有没有不带if/else/switch/的公式?可能替代解决方案[,]矩阵的etc,以及这对性能/效率的影响或差异是什么 class Program { private static Random r = new Random(); // names of the "moves" private static string[] rps = { "PAPER", "ROCK", "SCISSORS"}; // result feedback string
class Program
{
private static Random r = new Random();
// names of the "moves"
private static string[] rps = { "PAPER", "ROCK", "SCISSORS"};
// result feedback string to be formatted
private static string[] feedback = { "{1} Beats {0}, You Loose!","{0} Equals {1}, Draw!", "{0} Beats {1}, You Win!" };
// solution matrix ( 0 = loose ; 1 = draw ; 2 = win // array1: for paper; array2: for rock; array3: for scissors; )
private static int[,] solution = {{1, 2, 0},{0, 1, 2},{2, 0, 1}};
/// <summary>
/// Rock Paper scissors solution w/o calculation or if/case/else/
/// </summary>
/// <param name="args">dummy.</param>
static void Main(string[] args)
{
// simulate the players move
int player = r.Next(3);
// simulate the computers move
int computer = r.Next(3);
// retrieve the result from the matrix
int result = solution[player, computer];
//write the result of the match
Console.WriteLine(String.Format("you : {0} vs {1} : computer", rps[player], rps[computer]));
Console.WriteLine(String.Format(feedback[result], rps[player], rps[computer]));
}
}
类程序
{
私有静态随机r=新随机();
//“动作”的名称
私有静态字符串[]rps={“纸”、“石头”、“剪刀”};
//要格式化的结果反馈字符串
私有静态字符串[]反馈={{1}拍{0},你松了!”,“{0}等于{1},画!”,“{0}拍{1},你赢了!”;
//解矩阵(0=松散;1=绘制;2=win//array1:用于纸张;array2:用于岩石;array3:用于剪刀;)
私有静态int[,]解决方案={{1,2,0},{0,1,2},{2,0,1};
///
///不带计算或if/case/else的石头剪纸解决方案/
///
///笨蛋。
静态void Main(字符串[]参数)
{
//模拟玩家的移动
int player=r.Next(3);
//模拟计算机移动
int computer=r.Next(3);
//从矩阵中检索结果
int result=解决方案[玩家,计算机];
//写下比赛结果
WriteLine(String.Format(“you:{0}vs{1}:computer”,rps[player],rps[computer]);
Console.WriteLine(String.Format(反馈[结果]、rps[播放器]、rps[计算机]);
}
}
是的,有一个非常简单的公式:
player computer solution (c+4-p)%3
0 0 1 1
0 1 2 2
0 2 0 0
1 0 0 0
1 1 1 1
1 2 2 2
2 0 2 2
2 1 0 0
2 2 1 1
因此,您可以使用:
int result = (computer + 4 - player) % 3;
访问数组和计算值将花费几乎相同的时间。但是,此应用程序中的性能差异可以忽略不计。仅将结果写入控制台比使用数组或计算值要花费更长的时间。当然,通过计算值,您不需要数组,但因为它太小,所以没有多大区别
还要考虑解决方案的可读性。这个公式与你使用它的目的没有逻辑上的联系,它只是一种获得特定结果的方法,所以你需要一个大的注释来解释它实现了什么
编辑:
如果您想关注可读性,可以将逻辑放在单独的类中:
public class Play {
public enum Value { Paper = 0, Rock = 1, Scissors = 2 }
private Value _value;
public Play(Random rnd) {
_value = (Value)rnd.Next(3);
}
public bool SameAs(Play other) {
return _value == other._value;
}
public bool Beats(Play other) {
return
(_value == Value.Paper && other._value == Value.Rock) ||
(_value == Value.Rock && other._value == Value.Scissors) ||
(_value == Value.Scissors && other._value == Value.Paper);
}
public override string ToString() {
switch (_value) {
case Value.Paper: return "PAPER";
case Value.Rock: return "ROCK";
default: return "SCISSORS";
}
}
}
现在逻辑变得更清晰了:
Random r = new Random();
Play player = new Play(r);
Play computer = new Play(r);
Console.WriteLine("you : {0} vs {1} : computer", player, computer);
string feedback;
if (player.SameAs(computer)) {
feedback = "{0} Equals {1}, Draw!";
} else if (player.Beats(computer)) {
feedback = "{0} Beats {1}, You Win!";
} else {
feedback = "{1} Beats {0}, You Loose!";
}
Console.WriteLine(feedback, player, computer);
我几乎要为随机
的静态
实例的使用喝彩了,但是构造函数在那里的使用很奇怪。人们对无参数构造器有什么看法?你是说这样?(将随机构造函数更改为无参数)是的,无论如何都会发生这种情况,至少大致如此。它使用Environment.TickCount
。如果你有一个真正的理由只使用毫秒精度的话,那可能没问题,但我猜这不适用于很多情况。我知道你是对的。谢谢这是一个典型的东西,我很少使用,而且当我使用它时,确实过度使用它,效率低下;)谢谢你的快速回复。一项测试确实表明这个公式是有效的!也许你能解释一下4的作用?我在1、2和3中也尝试过同样的方法,但没有达到4。很有趣,但非常感谢@Caspar Kleiyne:4的作用与1类似,因为随后使用了模3,但它使(player+4)
始终大于计算机
,因此没有负数可担心。伙计……感谢您的宝贵经验!我喜欢这种东西!!可读性的问题确实是我一直在努力解决的问题。一件事是rps[]数组的行为是“线性”的,而结果以更三角形或“环形”的角度接近rps[]。在代码中澄清这一点是相当困难的。我想,简短的符号让它变得更难。可以说,未来的挑战。@Caspar Kleiyne:我认为,if
语句是最好的选择。我添加了一个示例,其中包含一个封装逻辑的类,并对值使用enum
,这使得它非常可读。