minimax ti c -ta c -ee алгоритм неправильно всегда ставит «X» или «O» в следующем доступном месте - PullRequest
0 голосов
/ 02 апреля 2020

Я пытаюсь создать игру ti c -ta c, которая отвечает игроку алгоритмом минимакса. Это не работает. Он просто ставит «Х» или «О» в следующем доступном месте. (Если 0,0 не является нулем, то оно идет к 0,1, а затем 0,2.) Я не понимаю, почему. Что я делаю не так?

Код:

public static int minimax( String[][] arr,String AI,String human,boolean turn) {
    if (end(arr))
    {
        String cheak = win_check(arr);
        return win_value(cheak,AI,human);
    }
    if (turn)
    {
        int best_score = -2;
        int score;
        for (int i = 0; i < 3; i++)
        {
            for (int f = 0; f < 3; f++)
            {
                {
                    if (arr[i][f] == "")
                    {
                        arr[i][f] = AI;
                        score = minimax(arr, AI, human, false);
                        arr[i][f] = "";
                        if ((best_score < score))
                        {
                            best_score = score;
                        }
                    }
                }
            }
        }
        return best_score;
    }
    //function that does the best move using the minimax algorithm
    public static void best_move (String arr [][],String AI,String human)
{
 int move_score;
 int best_move_score = -2;
 int moves [] =new int[2];
 for (int i=0;i<arr.length;i++)
 {
     for (int f=0;f<arr.length;f++)
     {
         if(arr[i][f] == "")
         {
             move_score = minimax(arr,AI,human,true);
             if(move_score>best_move_score)
             {
                 best_move_score = move_score;
                 moves[0] = i;
                 moves[1]= f;
             }
         }
     }
 }

1 Ответ

0 голосов
/ 05 апреля 2020

Я создал эту игру в C#, вы можете черпать вдохновение здесь ...

TicToaToeHeart

class TicToaToeHeart
{
    //  A  | B  | C
    // -------------
    //  D  | E  | F
    // -------------
    //  G  | H  |I

    public static string[] Strike = { "ABC", "DEF", "GHI", "ADG", "BEH", "CFI", "CEG", "AEI" };

    public static string[] PossibleMoves = { "AB","AC","AD","AE","AG","AI",
                                              "BC","BE","BH",
                                              "CE","CG","CI","CF",
                                              "DG","DE","DF",
                                              "EF","EG","EH","EI",
                                              "FI",
                                              "GH","GI","GC",
                                              "HI"
                                            };
    public static string[] RemainingMoves = { "C","B","G","I","D","E",
                                              "A","H","E",
                                              "G","E","F","I",
                                              "A","F","E",
                                              "D","C","B","A",
                                              "C",
                                              "I","H","E",
                                              "G"
                                            };
}

Game

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TIC_TAC_TOE
{
    public partial class Game : Form
    {
        private string userToken = string.Empty;
        private string userClicked = "";
        private string pcClicked = "";
        public Game()
        {
            InitializeComponent2();
            textBox1.Focus();
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.Items.Add("X");
            comboBox1.Items.Add("O");
            comboBox1.SelectedIndex = 0;
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            StartGame("0", "0");
        }

        private void ButtonA_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonA, "A");
        }

        private void ButtonB_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonB, "B");
        }

        private void ButtonC_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonC, "C");
        }

        private void ButtonD_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonD, "D");
        }

        private void ButtonE_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonE, "E");
        }

        private void ButtonF_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonF, "F");
        }

        private void ButtonG_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonG, "G");
        }

        private void ButtonH_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonH, "H");
        }

        private void ButtonI_Click(object sender, EventArgs e)
        {
            UpdateUserButtonDetails(buttonI, "I");
        }

        private void UpdateUserButtonDetails(Button button, string selectedButton)
        {
            button.BackColor = Color.GreenYellow;
            button.FlatStyle = FlatStyle.Flat;
            button.Text = userToken;
            button.Enabled = false;
            userClicked += selectedButton;
            ComputerTurn();
        }

        private string getPCToken()
        {
            return ("XO").Replace(userToken, "");
        }

        private void ComputerTurn()
        {
            CheckScore(userClicked, true);
            {
                if (CheckForPcWinChance(pcClicked, false) == false)
                    CheckForPcWinChance(userClicked, true);
            }
        }

        public bool SelectToken(string token)
        {
            if (SelectableTokenForPc(token) == false)
            {
                return false;
            }
            else
            {
                if (CheckForPcWinChance(pcClicked, false) == false)
                    SelectButton(token);
                return true;
            }
        }

        private bool CheckForPcWinChance(string pcClick, bool check)
        {
            bool flag = true;
            for (int i = 0; i < Token.PossibleMoves.Length; i++)
            {
                string key = Token.PossibleMoves[i];
                flag = true;
                for (int j = 0; j < key.Length; j++)
                {
                    if (!(pcClick.IndexOf(key[j]) >= 0))
                    {
                        flag = false;
                    }
                }
                if (flag)
                {
                    if (SelectableTokenForPc(Token.RemainingMoves[i]) == false)
                        continue;
                    else
                    {
                        SelectButton(Token.RemainingMoves[i]);
                        return true;
                    }
                }
            }
            if (!flag && check)
            {
                string allToken = "ABCDEFGHI";
                Random r = new Random();
                while (true)
                {
                    int index = r.Next(0, 9);
                    if (SelectToken(allToken[index].ToString()) == false)
                        continue;
                    else
                        break;
                }
            }
            return false;
        }

        private void SelectButton(string selectedButton)
        {
            switch (selectedButton)
            {
                case "A":
                    {
                        UpdatePcButtonDetails(buttonA, selectedButton);
                    }
                    break;
                case "B":
                    {
                        UpdatePcButtonDetails(buttonB, selectedButton);
                    }
                    break;
                case "C":
                    {
                        UpdatePcButtonDetails(buttonC, selectedButton);
                    }
                    break;
                case "D":
                    {
                        UpdatePcButtonDetails(buttonD, selectedButton);
                    }
                    break;
                case "E":
                    {
                        UpdatePcButtonDetails(buttonE, selectedButton);
                    }
                    break;
                case "F":
                    {
                        UpdatePcButtonDetails(buttonF, selectedButton);
                    }
                    break;
                case "G":
                    {
                        UpdatePcButtonDetails(buttonG, selectedButton);
                    }
                    break;
                case "H":
                    {
                        UpdatePcButtonDetails(buttonH, selectedButton);
                    }
                    break;
                case "I":
                    {
                        UpdatePcButtonDetails(buttonI, selectedButton);
                    }
                    break;

            }
            CheckScore(pcClicked, false);
        }

        private void UpdatePcButtonDetails(Button button, string selectedButton)
        {
            button.BackColor = Color.Red;
            button.FlatStyle = FlatStyle.Flat;
            button.Text = getPCToken();
            pcClicked += selectedButton;
            button.Enabled = false;
        }

        private void CheckScore(string token, bool user)
        {
            foreach (string s in Token.Strike)
            {
                bool flag = true;
                for (int i = 0; i < s.Length; i++)
                {
                    if (!(token.IndexOf(s[i]) >= 0))
                    {
                        flag = false;
                    }
                }
                if (flag)
                {
                    if (user)
                    {
                        label5.Text = ((Convert.ToInt32(label5.Text)) + 10) + "";
                        MessageBox.Show(label1.Text + " WON", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        label8.Text = ((Convert.ToInt32(label8.Text)) + 10) + "";
                        MessageBox.Show("PC WON", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                    StartGame(label8.Text, label5.Text);
                    break;
                }
                if (userClicked.Length + pcClicked.Length == 9)
                {
                    MessageBox.Show("Game Draw", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    StartGame(label8.Text, label5.Text);
                    break;
                }
            }
        }

        private bool SelectableTokenForPc(string newToken)
        {
            if (userClicked.IndexOf(newToken) >= 0)
            {
                return false;
            }
            if (pcClicked.IndexOf(newToken) >= 0)
            {
                return false;
            }
            return true;
        }

        private void StartGame(string pcScore, string userScore)
        {
            string name = textBox1.Text;
            name = name.Trim();
            if (name.Equals(""))
            {
                return;
            }
            userClicked = "";
            pcClicked = "";
            userToken = comboBox1.SelectedItem.ToString();
            this.Controls.Clear();
            InitializeComponent();
            label1.Text = name;
            label4.Text = "Pc : ";
            label5.Text = userScore;
            label8.Text = pcScore;
        }

        private void ReStartGameButton_Click(object sender, EventArgs e)
        {
            StartGame("0", "0");
        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            StartGame(label8.Text, label5.Text);
        }
    }
}

enter image description here

кодовая ссылка github

...