我正在为学校作业制作摇滚、纸、剪刀游戏,但是当我尝试运行此脚本时,游戏无法正常运行。例如,当我单击 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);
}
守着星空守着你
慕斯王
慕仙森
相关分类