【问题标题】:Tic Tac Toe - Letter 'X' AI doesn't work (C#)井字游戏 - 字母 'X' AI 不起作用 (C#)
【发布时间】:2017-09-13 01:15:29
【问题描述】:

我正在为我在 C# 课程中的作业开发井字游戏 AI。在游戏开始时,游戏会让玩家在字母“X”和字母“O”之间选择一个字母。

如果玩家选择字母“X”,那么字母“O”将自动成为 AI。 如果玩家选择字母“O”,那么字母“X”将自动成为 AI。

问题:当我选择字母“O”时,游戏让我做第二个回合(这是字母“X”,AI 会做那个移动/转身,而不是我)。

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

附:我使用 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

【问题讨论】:

  • 代码太多了。请参阅How to create a Minimal, Complete and Verifiable ExampleHow to Ask,然后相应地返回并edit 您的帖子。或者,更好的是,使用调试器单步调试您的代码并找出问题所在(在这种情况下您根本不需要询问)或将问题缩小到代码的一小部分然后发布代码在这里。祝你好运。 (您可能需要重新考虑切换firstturn 值的位置,这可能会有所帮助。)
  • 您可以尝试将first_turn = !first_turn; 移动到if (!first_turn) { PuhTakTehMind(); } 之后。
  • @KeyurPATEL 它使字母'O' 使一举一动.. 仍然没有解决问题。但是 tnx 试图提供帮助:)
  • 这并不能真正解决您的问题,但它可能会在未来有所帮助:我强烈建议根据变量的实际代表来命名变量......对我来说,一个名为“first_turn”的布尔值意味着我说它只在第一轮是真的,然后每轮都是假的,与他们分配的字母无关。我宁愿称它为“playerWentFirst”之类的东西。此外,您的逻辑没有考虑到玩家在第一步中将 X 放在中心方块中......

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


【解决方案1】:

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

因此,AI 例程将被跳过。又轮到玩家了,但这次first_turn是真的,所以玩家会在棋盘上设置一个X。

这就是程序实际编写的目的。编写符合要求的程序是您的任务的一部分,并且有无数种方法可以做到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多