wpf 石头剪刀布游戏有一个窗口

tnkciper  于 2023-08-07  发布在  其他
关注(0)|答案(4)|浏览(106)

我正在为学校的作业做一个石头剪刀布的游戏,但是当我试着运行这个脚本时,游戏不能正常工作。例如,当我单击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);
        }

字符串

eiee3dmh

eiee3dmh1#

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

fcwjkofz

fcwjkofz2#

这里有另一种方法。它已经从您的代码中进行了很大程度的重构。首先,我在form类中添加了一个私有的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);
 }


以这种方式构造事物的一个结果是,要将其扩展为“Rocks,Paper,Scissors,Lizard,Spock”,您所需要做的就是向HandSign枚举添加两个条目,向DetermineWinner添加更多的逻辑和两个按钮/按钮处理程序。
还要注意,没有办法拼写“剪刀”错误,仍然有这个编译!

备注:

在我的回答中可以找到一个控制台应用程序版本:剪刀石头布出了问题

goqiplq2

goqiplq23#

接下来,上限是独占的,所以你应该执行RandomType = random.Next(0, 3);
此外,在:
if (PlayerPicks == "Scissors" && Computer == "Scissor")你有“剪刀”而不是“剪刀s

ncgqoxb0

ncgqoxb04#

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

相关问题