井字游戏 - 字母"X"AI 不起作用 (C#)



我正在为我的C#课程做分配的TIC TAC TOE AI。在游戏开始时,游戏将让玩家在字母'x'和字母'o'之间选择一个字母。

如果玩家选择字母'x',则自动将字母" O"为AI。如果玩家选择字母'o',则自动将字母" x"为ai。

问题:当我选择字母'o'时,游戏会让我进行第二回合(这是字母'x',而AI会这样做/转弯,而不是我)。

问题:我对代码的逻辑真的错了吗?如果是,我需要更改代码的逻辑上的更改?

P.S。我使用Visual Studio 2015

这是我知道有问题的一部分的代码:

namespace Puh_Tak_Teh{
public partial class Form3 : Form{
    private bool first_turn; //X - true , Y - false
    public int turn_count = 0;
    Image X = Image.FromFile("C:\Users\Denzell\Documents\Visual Studio 2015\Projects\Puh Tak Teh\x.png");
    Image O = Image.FromFile("C:\Users\Denzell\Documents\Visual Studio 2015\Projects\Puh Tak Teh\o.png");
    public Form3(Form2 form2){
        InitializeComponent();
        if (form2.player_turn == true){
            first_turn = true; //from form 2, if the player chose 'X'
        } //then the form here bool first_turn will be set to true
        else if (form2.player_turn == false){
            first_turn = false; //from form 2, if the player chose O
        }//then the form here bool first_turn will be set to true false
    }
    private void Show(Object sender, EventArgs e){
        InitializeComponent();
    }
    private void button_click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        if (first_turn) //if the first_turn is true (which is X)
        {
            b.Image = X;
            b.ImageAlign = ContentAlignment.MiddleCenter;
        }
        else //if the first_turn is false (which is O)
        {
            b.Image = O;
            b.ImageAlign = ContentAlignment.MiddleCenter;
        }
        first_turn = !first_turn; //
        b.Enabled = false; //disables the button if it is already clicked
        turn_count++; //counts 
        CheckWinner();
        if (!first_turn) //I think the logical error / my problem starts  from here
        {
            PuhTakTehMind();
        }
    }
    private void PuhTakTehMind() //private method in which the game would decide which what move the AI would make
    {
        Button AddMove = null; //makes/performs the move
        AddMove = look_for_win_or_block(X); //loof for win ( letter X)
        if (AddMove == null)
        {
            AddMove = look_for_win_or_block(O); //look for block (letter O)
            if (AddMove == null)
            {
                AddMove = look_for_corner();
                if (AddMove == null)
                {
                    AddMove = look_for_open_space();
                }
            }
        }
        if (turn_count != 9)
            AddMove.PerformClick();
    }

整个代码:https://pastebin.com/wjzxjupc

如果选择了字母O,则first_turn开始为false。单击第一个按钮,将按钮设置为字母O并禁用。然后first_turn在此行上设置为true: first_turn = !first_turn;

因此,然后跳过了AI例程。这是播放器再次转弯,但是这次first_turn是正确的,因此玩家将在板上设置X。

这是该程序实际上要做的。作为作业的一部分,您的工作是编写适合要求的程序,并且有很多方法可以做到。

最新更新