猿问

带窗户的石头剪刀布游戏

我正在为学校作业制作摇滚、纸、剪刀游戏,但是当我尝试运行此脚本时,游戏无法正常运行。例如,当我单击 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);


        }


   

慕勒3428872
浏览 131回答 3
3回答

守着星空守着你

问题 1RandomType = random.Next(0, 2)只生成一个介于 0 和 1 之间的数字,您需要这样做RandomType = random.Next(0, 3)这将生成一个介于 0 和 2 之间的数字问题 2if (PlayerPicks == "Scissors" && Computer == "Scissor")检查计算机是否选择Scissor而不是Scissors只需将其更改为if (PlayerPicks == "Scissors" && Computer == "Scissors")

慕斯王

这是另一种方法。它已从您的代码中进行了极大的重构。首先,我在表单类中添加了一个私有System.Random成员字段:private Random _rand = new Random();然后,我设置了两个enums,一个用于“手势”(石头、纸、剪刀),另一个用于结果:public enum HandSign{    Rock,           // rock beats scissors    Paper,          // paper beats rock    Scissors,       // scissors beats paper}public enum Result{    Draw,    ComputerWins,    PlayerWins}然后我创建了一些辅助函数,首先确定计算机的下一步动作:private HandSign GetRandomPlay(){    var result = _rand.Next(0, Enum.GetValues(typeof(HandSign)).Length);    return (HandSign) result;}接下来是确定获胜者的规则引擎:private Result DetermineWinner(HandSign player, HandSign computer){    if (player == computer)    {        return Result.Draw;    }    if (player == HandSign.Scissors && computer == HandSign.Rock)    {        return Result.ComputerWins;    }    if (player == HandSign.Rock && computer == HandSign.Scissors)    {        return Result.PlayerWins;    }    if (player > computer)    {        return Result.PlayerWins;    }    //finally, otherwise    return Result.ComputerWins;}最后来一张“玩游戏”,在表格上的一把标签上展示了玩的结果: private void PlayGame(HandSign playerPicks) {     PlayerPicksLbl.Text = playerPicks.ToString();     var computerPicks = GetRandomPlay();     ComputerPicksLbl.Text = computerPicks.ToString();     WinnerLbl.Text = DetermineWinner(playerPicks, computerPicks).ToString(); }那时,我的三个按钮单击处理程序看起来像这样(这是 Rock 处理程序): private void RockBtn_Click(object sender, EventArgs e) {     PlayGame(HandSign.Rock); }以这种方式构造事物的一个结果是将其扩展到“Rocks, Paper, Scissors, Lizard, Spock”,您需要做的就是向枚举添加两个条目,向两个条目添加HandSign更多逻辑DetermineWinner更多按钮/按钮处理程序。另请注意,没有办法拼错“剪刀”,仍然可以编译!

慕仙森

在 Random.Next 上,上限是独占的,所以你应该这样做 RandomType = random.Next(0, 3);此外在:if (PlayerPicks == "Scissors" && Computer == "Scissor")你有“Scissor”而不是“Scissor s ”
随时随地看视频慕课网APP
我要回答