Tic Tac Toe - Letter' X' AI不起作用(C#)

时间:2017-09-13 01:15:30

标签: c# visual-studio-2015 tic-tac-toe

我在我的C#课程中完成了我的任务。在游戏开始时,游戏将让玩家选择字母“X'”之间的字母。和信' O。

如果玩家选择字母' X'然后自动写出字母' O'将是人工智能。 如果玩家选择了字母“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

1 个答案:

答案 0 :(得分:0)

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

因此,跳过AI例程。这是玩家再次转弯,但这次first_turn是真的,所以玩家将在棋盘上设置一个X.

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