我正在为学校的作业做一个石头剪刀布的游戏,但是当我试着运行这个脚本时,游戏不能正常工作。例如,当我单击Rock时,计算机只选择相同的或选择纸张。但玩家永远不会赢。我不知道怎么修这个,我已经试了好几个小时了。下面是我的代码:
public partial class MainWindow : Window
{
string Computer;
string[] computer = { "Rock", "Paper", "Scissors" };
Random random = new Random();
int RandomType;
string PlayerPicks;
public MainWindow()
{
InitializeComponent();
}
private void StoneButton_Click(object sender, RoutedEventArgs e)
{
PlayerPicks = "Rock";
RandomType = random.Next(0, 2);
Computer = computer[RandomType];
Game();
}
private void PaperButton_Click(object sender, RoutedEventArgs e)
{
PlayerPicks = "Paper";
RandomType = random.Next(0, 2);
Computer = computer[RandomType];
Game();
}
private void ScissorsButton_Click(object sender, RoutedEventArgs e)
{
PlayerPicks = "Scissors";
RandomType = random.Next(0, 2);
Computer = computer[RandomType];
Game();
}
void Game()
{
string message = "The winner is: ";
string computerWins = "Computer!";
string playerWins = "Player!";
string draw = "N-Nobody?";
if (PlayerPicks == "Rock" && Computer == "Paper") // Player: Rock, Computer: paper = computer wins
{
MessageBox.Show(message + computerWins);
}
else if (PlayerPicks == "Rock" && Computer == "Scissors") // Player: Rock, Computer: Scissors = Player wins
{
MessageBox.Show(message + playerWins);
}
else if (PlayerPicks == "Paper" && Computer == "Scissors") // Player: Paper, Computer: Scissors = Computer wins
{
MessageBox.Show(message + computerWins);
}
else if (PlayerPicks == "Paper" && Computer == "Rock") // Player: Paper, Computer: Rock = Player wins
{
MessageBox.Show(message + playerWins);
}
else if (PlayerPicks == "Scissors" && Computer == "Rock") // Player: Scissors, Computer: Rock = Computer wins
{
MessageBox.Show(message + computerWins);
}
else if (PlayerPicks == "Scissors" && Computer == "Paper") // Player: Scissors, Computer: Paper = Player wins
{
MessageBox.Show(message + playerWins);
}
if (PlayerPicks == "Scissors" && Computer == "Scissor")
{
MessageBox.Show(message + draw);
}
if (PlayerPicks == "Paper" && Computer == "Paper")
{
MessageBox.Show(message + draw);
}
if (PlayerPicks == "Rock" && Computer == "Rock")
{
MessageBox.Show(message + draw);
}
字符串
4条答案
按热度按时间eiee3dmh1#
问题一
RandomType = random.Next(0, 2)
仅生成0到1之间的数字,您需要执行RandomType = random.Next(0, 3)
这将生成0到2之间的数字问题二
if (PlayerPicks == "Scissors" && Computer == "Scissor")
检查计算机是否选择了Scissor
而不是Scissors
,只需将其更改为if (PlayerPicks == "Scissors" && Computer == "Scissors")
fcwjkofz2#
这里有另一种方法。它已经从您的代码中进行了很大程度的重构。首先,我在form类中添加了一个私有的
System.Random
成员字段:字符串
然后,我设置了两个
enum
,一个用于“手势”(石头、剪刀、布),另一个用于结果:型
然后我创建一些辅助函数,首先确定计算机的下一步行动:
型
下一个是规则引擎来决定赢家:
型
最后,一个“玩游戏”,显示的结果,发挥在少数标签的形式:
型
在这一点上,我的三个按钮点击处理程序简单地看起来像这样(这是Rock处理程序):
型
以这种方式构造事物的一个结果是,要将其扩展为“Rocks,Paper,Scissors,Lizard,Spock”,您所需要做的就是向
HandSign
枚举添加两个条目,向DetermineWinner
添加更多的逻辑和两个按钮/按钮处理程序。还要注意,没有办法拼写“剪刀”错误,仍然有这个编译!
备注:
在我的回答中可以找到一个控制台应用程序版本:剪刀石头布出了问题
goqiplq23#
接下来,上限是独占的,所以你应该执行
RandomType = random.Next(0, 3);
此外,在:
if (PlayerPicks == "Scissors" && Computer == "Scissor")
你有“剪刀”而不是“剪刀s”ncgqoxb04#
首先,你需要使用random.Next(0,3)(正如Anu Viswan在评论中提到的)此外,你在下面的第三个“if”处有一个错别字,你输入了 Scissor 而不是 Scissors。我建议对这些字符串使用变量--这样你就不会有打字错误的问题了。