带窗的剪刀石头布游戏

时间:2018-12-12 15:53:40

标签: c# wpf

我正在为学校布置的石头,剪刀,剪刀游戏,但是当我尝试运行此脚本时,该游戏无法正常运行。例如,当我单击“摇滚”时,计算机仅会拾取相同的东西或会拾取纸张。但是玩家永远不会赢。我已经尝试了几个小时,我不知道该如何解决。这是我的代码:

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 个答案:

答案 0 :(得分:4)

问题1

RandomType = random.Next(0, 2)仅生成0到1之间的数字,您需要执行RandomType = random.Next(0, 3)这将生成0到2之间的数字

问题2

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

答案 1 :(得分:2)

在Random上。下一个上限是互斥的,所以你应该做 RandomType = random.Next(0, 3);

此外:

if (PlayerPicks == "Scissors" && Computer == "Scissor")您使用的是“剪刀”而不是“剪刀 s

答案 2 :(得分:2)

这是另一种方法。它已从您的代码中大大重构。首先,我向表单类添加了一个私有System.Random成员字段:

private Random _rand = new Random();

然后,我设置了两个enum,一个用于“手势”(石头,纸,剪刀),另一个用于结果:

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);
 }

以这种方式进行结构化的结果是,将其扩展到“岩石,纸张,剪刀,蜥蜴,史波克”,您要做的就是向HandSign枚举中添加两个条目,向DetermineWinner添加更多的逻辑,并增加两个按钮/按钮处理程序。

还请注意,没有办法将“ Scissors”拼​​写错误,并且仍然可以进行编译!

答案 3 :(得分:1)

首先,您需要使用random.Next(0,3)(如评论中提到的Anu Viswan) 此外,您在下面的第三个“ if”处键入了 Scissor 而不是 Scissors 。我建议对这些字符串使用变量-这样您就不会出现拼写错误。