C# 剪纸石算法
我正在使用下面的方法,但不知道是否有更好的算法来执行测试。有更好的方法吗?在C语言中这样做,但抛开语法不谈,相信整个OOP语言的算法都是一样的。多谢各位C# 剪纸石算法,c#,java,algorithm,C#,Java,Algorithm,我正在使用下面的方法,但不知道是否有更好的算法来执行测试。有更好的方法吗?在C语言中这样做,但抛开语法不谈,相信整个OOP语言的算法都是一样的。多谢各位 public String play(int userInput) { //ComputerIn is a randomly generated number between 1-3 ComputerIn = computerInput(); if (ComputerIn =
public String play(int userInput)
{ //ComputerIn is a randomly generated number between 1-3
ComputerIn = computerInput();
if (ComputerIn == userInput)
return "Draw";
else if (ComputerIn == 1 && userInput == 2)
return "Win";
else if (ComputerIn == 2 && userInput == 3)
return "Win";
else if (ComputerIn == 3 && userInput == 1)
return "Win";
else if (ComputerIn == 1 && userInput == 3)
return "Lose";
else if (ComputerIn == 2 && userInput == 1)
return "Lose";
else
return "Lose";
}
如果使用%将3与1进行折合,则赢家总是比输家大1
当您使用0-2时,这种方法更为自然,在这种情况下,我们将使用ComputerIn+1%3。我用ComputerIn-1代替ComputerIn,用UserInput-1代替UserInput,并简化了表达式,从而得出了我的答案
编辑,在很长一段时间后看这个问题。如前所述,如果ComputerIn未在其他任何地方使用,且仅用于确定赢/输/抽,则该方法实际上相当于:
if (ComputerIn == 1)
return "Win";
else if (ComputerIn == 2)
return "Lose"
else
return "Draw"
这甚至可以进一步简化为
return new String[]{"Win", "Lose", "Draw"}[ComputerIn-1];
由此产生的结果完全无法区分。除非随机生成的数字暴露在该方法之外。无论你的意见是什么,总有1/3的可能性。也就是说,你所要求的,只是以同样的概率返回赢、输或平局的复杂方式 以下是许多可能的解决方案之一。这将使我们获胜
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Input userInput = Input.Rock;
Result result = Play(userInput);
Console.WriteLine(Enum.GetName(result.GetType(), result));
Console.ReadKey();
}
static Result Play(Input userInput)
{
Input computer = Input.Scissors;
switch (userInput)
{
case Input.Paper:
switch (computer)
{
case Input.Paper: return Result.Draw;
case Input.Rock: return Result.Win;
case Input.Scissors: return Result.Lose;
default: throw new Exception("Logic fail.");
}
case Input.Rock:
switch (computer)
{
case Input.Paper: return Result.Lose;
case Input.Rock: return Result.Draw;
case Input.Scissors: return Result.Win;
default: throw new Exception("Logic fail.");
}
case Input.Scissors:
switch (computer)
{
case Input.Paper: return Result.Win;
case Input.Rock: return Result.Lose;
case Input.Scissors: return Result.Draw;
default: throw new Exception("Logic fail.");
}
default: throw new Exception("Logic fail.");
}
}
}
enum Input
{
Rock,
Paper,
Scissors
}
enum Result
{
Lose,
Draw,
Win
}
}
我会这样做:
public class Program
{
public enum RPSPlay { Rock, Scissors, Paper }
public enum RPSPlayResult { Win, Draw, Loose }
public static readonly int SIZE = Enum.GetValues(typeof(RPSPlay)).Length;
static RPSPlayResult Beats(RPSPlay play, RPSPlay otherPlay)
{
if (play == otherPlay) return RPSPlayResult.Draw;
return ((int)play + 1) % SIZE == (int)otherPlay
? RPSPlayResult.Win
: RPSPlayResult.Loose;
}
static void Main(string[] args)
{
Random rand = new Random();
while (true)
{
Console.Write("Your play ({0}) (q to exit) : ", string.Join(",", Enum.GetNames(typeof(RPSPlay))));
var line = Console.ReadLine();
if (line.Equals("q", StringComparison.OrdinalIgnoreCase))
return;
RPSPlay play;
if (!Enum.TryParse(line, true, out play))
{
Console.WriteLine("Invalid Input");
continue;
}
RPSPlay computerPlay = (RPSPlay)rand.Next(SIZE);
Console.WriteLine("Computer Played {0}", computerPlay);
Console.WriteLine(Beats(play, computerPlay));
Console.WriteLine();
}
}
}
我更喜欢使用静态3x3矩阵来存储可能的结果。但这是一个品味的问题,而我是一名数学家。这是我们在午餐时间制作的一行
using System;
public class Rps {
public enum PlayerChoice { Rock, Paper, Scissors };
public enum Result { Draw, FirstWin, FirstLose};
public static Result Match(PlayerChoice player1, PlayerChoice player2) {
return (Result)((player1 - player2 + 3) % 3);
}
public static void Main() {
Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Rock), Result.Draw);
Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Paper), Result.Draw);
Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Scissors), Result.Draw);
Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Scissors), Result.FirstWin);
Rps.Test(Match(PlayerChoice.Rock, PlayerChoice.Paper), Result.FirstLose);
Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Rock), Result.FirstWin);
Rps.Test(Match(PlayerChoice.Paper, PlayerChoice.Scissors), Result.FirstLose);
Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Paper), Result.FirstWin);
Rps.Test(Match(PlayerChoice.Scissors, PlayerChoice.Rock), Result.FirstLose);
}
public static void Test(Result sample, Result origin) {
Console.WriteLine(sample == origin);
}
}
\从java初学者的角度来看。用户可以无限地玩电脑
import java.util.Scanner;
public class AlgorithmDevelopmentRockPaperScissors{
public static void main(String[] args){
System.out.println("\n\nHello Eric today we are going to play a game.");
System.out.println("Its called Rock Paper Scissors.");
System.out.println("All you have to do is input the following");
System.out.println("\n 1 For Rock");
System.out.println("\n 2 For Paper");
System.out.println("\n 3 For Scissors");
int loop;
loop = 0;
while (loop == 0){
System.out.println("\n\nWhat do you choose ?");
int userInput;
Scanner input = new Scanner(System.in);
userInput = input.nextInt();
while (userInput > 3 || userInput <= 0 ){ //ensure that the number input by the sure is within range 1-3. if else the loop trap.
System.out.println("Your choice "+userInput+" is not among the choices that are given. Please enter again.");
userInput = input.nextInt();
}
switch (userInput){
case 1:
System.out.println("You Chose Rock.");
break;
case 2:
System.out.println("You Chose Paper.");
break;
case 3:
System.out.println("You Chose Scissors");
break;
default:
System.out.println("Please Choose either of the choices given");
break;
}
int compInput;
compInput = (int)(3*Math.random()+1);
switch (compInput){
case 1:
System.out.println("\nComputer Chooses Rock.");
break;
case 2:
System.out.println("\nComputer Chooses Paper.");
break;
case 3:
System.out.println("\nComputer Chooses Scissors");
break;
}
if (userInput == compInput){
System.out.println(".........................................");
System.out.println("\nYou Both chose the same thing, the game ends DRAW.");
System.out.println(".........................................");
}
if (userInput == 1 && compInput == 2){
System.out.println(".........................................");
System.out.println("\nComputer wins because Paper wraps rock.");
System.out.println(".........................................");
}
if (userInput == 1 && compInput == 3){
System.out.println(".........................................");
System.out.println("\nYou win because Rock breaks Scissors.");
System.out.println(".........................................");
}
if (userInput == 2 && compInput == 1){
System.out.println(".........................................");
System.out.println("\nYou win Because Paper wraps Rock");
System.out.println(".........................................");
}
if (userInput == 2 && compInput == 3){
System.out.println(".........................................");
System.out.println("\nComputer wins because Scissors cut the paper");
System.out.println(".........................................");
}
if (userInput == 3 && compInput == 1){
System.out.println(".........................................");
System.out.println("\nComputer Wins because Rock Breaks Scissors.");
System.out.println(".........................................");
}
if (userInput == 3 && compInput == 2){
System.out.println(".........................................");
System.out.println("\nYou win because scissors cut the paper");
System.out.println(".........................................");
}
}
}
}
使用正弦波函数计算结果的简单JavaScript实现:
<script>
var tab = ["Lose","Draw","Win"];
var dict = ["Paper","Stone","Scissors"];
var i,text = '';
for (i = 0; i < dict.length; i++) {
text += i + '-' + dict[i] + ' ';
}
var val1 = parseInt(prompt(text));
var val2 = Math.floor(Math.random() * 3);
alert('Computer chose: ' + dict[val2]);
result = Math.sin((val1*180+1) - (val2*180+1));
alert(tab[Math.round(result)+1]);
</script>
不,如果需要,只是为了好玩
观察结果:如果userInput只比computerInput的1,2,2,3领先一位,或者比computerInput的3,1落后两位,则用户获胜 相反,如果userInput落后于computerInput一倍或领先于computerInput两倍,则用户将失败 在模3中,滞后1与超前2相同。 -1模3==2模3==2 int decision=userInput-computerInput+3%3; String[]answer={平局,赢,输}; 返回答案[决定];
不同的语言对此可能有不同的惯用方法。你真的对C、Java或C感兴趣吗?嗨,我想知道C语言,如果Java语言对算法有不同,我也会感兴趣。谢谢。在给定的语法中,我没有看到太多的OOPs概念。这更多的是分支语句。由于Java枚举与C枚举的性质不同,我对Java和C的解决方案也会有所不同。如果ComputerIn%3+1==userInput return Draw;如果ComputerIn==userInput返回Draw;,是否满足@MillerKoijam不,事实上这两个条件是相互排斥的。如果我的两个条件都是假的,那么它们必须相等。当只有3个可能的值时,A>B、B>A或A=B。枚举的东西都是一个好主意,尽管它是从原始问题重构而来的。即使有了它,我仍然宁愿在多行中编写匹配。这可能很难理解,我想我们谈论的是更好的算法。当我看罗伯特·塞吉威克(Robert Sedgewick)的算法课程时,它们也很难阅读。编程不同于算法设计。编程时,可读性和可维护性是您最关心的问题。在编程之前设计算法时,首要考虑的是正确性和效率。。。然而,这并不是一个庞大的企业应用程序,所以谁在乎呢:或者,如果它是来自so的审阅者,那么投反对票就足以转到下一个审阅项目,并增加审阅计数,从而更快地获得徽章。
<script>
var tab = ["Lose","Draw","Win"];
var dict = ["Paper","Stone","Scissors"];
var i,text = '';
for (i = 0; i < dict.length; i++) {
text += i + '-' + dict[i] + ' ';
}
var val1 = parseInt(prompt(text));
var val2 = Math.floor(Math.random() * 3);
alert('Computer chose: ' + dict[val2]);
result = Math.sin((val1*180+1) - (val2*180+1));
alert(tab[Math.round(result)+1]);
</script>