带窗户的石头剪刀布游戏 [英] Rock paper scissors game with a window

查看:28
本文介绍了带窗户的石头剪刀布游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学校作业制作石头剪刀布游戏,但是当我尝试运行此脚本时,游戏无法正常运行.例如,当我单击 Rock 时,计算机只会选择相同的或选择纸张.但玩家永远不会赢.我不知道如何解决这个问题,我已经尝试了几个小时.这是我的代码:

I'm working on a Rock, paper, scissors game for a school assignment, But when I try to run this script the game doesn't work properly. When I click Rock for example the computer only picks the same or picks paper. But the player never wins. I don't know how to fix this, Ive been trying for hours. This is my code:

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 之间的数字

RandomType = random.Next(0, 2) only generates a number between 0 and 1, you would need to do RandomType = random.Next(0, 3) This will generate a number between 0 and 2

问题 2

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

if (PlayerPicks == "Scissors" && Computer == "Scissor") checks if computer picked Scissor instead of Scissors Just change this to if (PlayerPicks == "Scissors" && Computer == "Scissors")

这篇关于带窗户的石头剪刀布游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆