gpt4 book ai didi

c# - 带有窗口的石头剪刀布游戏

转载 作者:太空狗 更新时间:2023-10-30 00:17:51 26 4
gpt4 key购买 nike

我正在为一项学校作业开发剪刀石头布游戏,但是当我尝试运行此脚本时,游戏无法正常运行。例如,当我单击 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);
}

最佳答案

问题 1

RandomType = random.Next(0, 2) 只生成一个介于 0 和 1 之间的数字,您需要执行 RandomType = random.Next(0, 3) 这将生成一个介于 0 和 2 之间的数字

问题2

if (PlayerPicks == "Scissors"&& Computer == "Scissor") 检查计算机是否选择了 Scissors 而不是 Scissors 只需更改这到 if (PlayerPicks == "Scissors"&& Computer == "Scissors")

关于c# - 带有窗口的石头剪刀布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53746727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com