Tic Tac Toe转弯问题C#

本文关键字:问题 转弯 Toe Tac Tic | 更新日期: 2023-09-27 18:19:29

我在一个基本的编程类中,现在我们假设在一个模拟玩井字游戏的程序中进行编码。在我的代码中,我计划让人们选择是想看模拟游戏还是想玩,然后这将是一个两人游戏,我还没有准备好制作一个与电脑对抗的游戏。

我被模拟部分卡住了,所以转到了游戏部分。玩家一去,但按照我的if语句的工作方式,它为玩家一做动作,但也为玩家2做动作。代码很长,但每个按钮都是由相同的基本代码组成的,所以我只发布了一个按钮。

添加了"lblTest.Text = OneDown.ToString();"代码,这样我就可以看到它所做的计算,每次点击都对玩家1和2计数,显示结束数字为8。我该如何让它只为玩家一或玩家二运行?

private void btn1_Click(object sender, EventArgs e)
{
    if (PlayerTurn == 1)
    {
        OneDown = OneDown + 5;
        OneAcross = OneAcross + 5;
        SlashOne = SlashOne + 5;
        btn1.Text = "X";
        btn1.Enabled = false;
        PlayerTurn = PlayerTurn - 1;
        lblTurn.Text = "O";
    }
    if (SlashOne == 15 || OneAcross == 15 || OneDown == 15)
    {
        MessageBox.Show("Player 1 Wins!");
    }
    else if (PlayerTurn == 0)
    {
        OneDown = OneDown + 3;
        OneAcross = OneAcross + 3;
        SlashOne = SlashOne + 3;
        btn1.Text = "O";
        btn1.Enabled = false;
        PlayerTurn = PlayerTurn + 1;
        lblTurn.Text = "X";
        lblTest.Text = OneDown.ToString();
    }
    if (SlashOne == 9 || OneAcross == 9 || OneDown == 9)
    {
        MessageBox.Show("Player 2 Wins!");
    }

Tic Tac Toe转弯问题C#

基本上你的else if没有连接到正确的if.

private void btn1_Click(object sender, EventArgs e)
{
    if (PlayerTurn == 1)
    {
        OneDown = OneDown + 5;
        OneAcross = OneAcross + 5;
        SlashOne = SlashOne + 5;
        btn1.Text = "X";
        btn1.Enabled = false;
        PlayerTurn = PlayerTurn - 1;
        lblTurn.Text = "O";
        if (SlashOne == 15 || OneAcross == 15 || OneDown == 15)
        {
            MessageBox.Show("Player 1 Wins!");
        }
    }
    else if (PlayerTurn == 0)
    {
        OneDown = OneDown + 3;
        OneAcross = OneAcross + 3;
        SlashOne = SlashOne + 3;
        btn1.Text = "O";
        btn1.Enabled = false;
        PlayerTurn = PlayerTurn + 1;
        lblTurn.Text = "X";
        lblTest.Text = OneDown.ToString();
        if (SlashOne == 9 || OneAcross == 9 || OneDown == 9)
        {
            MessageBox.Show("Player 2 Wins!");
        }
    }