Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 极小极大tic tac toe算法总是错误地将;X";或;O";在下一个可用的地方_Java_Algorithm_Tic Tac Toe_Minimax - Fatal编程技术网

Java 极小极大tic tac toe算法总是错误地将;X";或;O";在下一个可用的地方

Java 极小极大tic tac toe算法总是错误地将;X";或;O";在下一个可用的地方,java,algorithm,tic-tac-toe,minimax,Java,Algorithm,Tic Tac Toe,Minimax,我试图创建一个tic-tac-toe游戏,用minimax算法响应玩家。它不起作用。它只是将“X”或“O”放在下一个可用的位置。(如果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

我试图创建一个tic-tac-toe游戏,用minimax算法响应玩家。它不起作用。它只是将“X”或“O”放在下一个可用的位置。(如果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;
             }
         }
     }
 }
public static int minimax(String[][]arr,String AI,String human,boolean-turn){
如果(结束(arr))
{
字符串cheak=赢得检查(arr);
返回win_值(cheak、AI、human);
}
如果(转身)
{
int最佳分数=-2;
智力得分;
对于(int i=0;i<3;i++)
{
对于(int f=0;f<3;f++)
{
{
如果(arr[i][f]=“”)
{
arr[i][f]=AI;
分数=最小最大值(arr、AI、人类、错误);
arr[i][f]=“”;
如果((最佳分数<分数))
{
最佳分数=分数;
}
}
}
}
}
返回最佳_分数;
}
//使用minimax算法进行最佳移动的函数
公共静态无效最佳移动(字符串arr[][],字符串AI,字符串human)
{
int move_分数;
整数最佳移动分数=-2;
整数移动[]=新整数[2];

因为(inti=0;i我用C#创建了这个游戏,你可以从这里得到灵感

tictoeartheart

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"
                                            };
}
游戏

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);
        }
    }
}
使用系统;
使用系统图;
使用System.Windows.Forms;
名称空间TIC_TAC_TOE
{
公共部分类游戏:形式
{
私有字符串userToken=string.Empty;
私有字符串userClicked=“”;
私有字符串pcClicked=“”;
公共游戏()
{
初始化组件2();
textBox1.Focus();
comboBox1.DropDownStyle=ComboBoxStyle.DropDownList;
组合框1.项目。添加(“X”);
组合框1.项目。添加(“O”);
comboBox1.SelectedIndex=0;
}
私有无效按钮单击(对象发送者,事件参数e)
{
起始名称(“0”、“0”);
}
私有无效按钮单击(对象发送者,事件参数e)
{
UpdateUserButtonDetails(按钮“A”);
}
私有无效按钮单击(对象发送者,事件参数)
{
UpdateUserButtonDetails(按钮B,“B”);
}
私有无效按钮单击(对象发送者,事件参数)
{
UpdateUserButtonDetails(按钮“C”);
}
私有无效按钮单击(对象发送者,事件参数e)
{
UpdateUserButtonDetails(按钮,“D”);
}
私有无效按钮单击(对象发送者,事件参数e)
{
UpdateUserButtonDetails(按钮,“E”);
}
私有无效按钮单击(对象发送者,事件参数e)
{
UpdateUserButtonDetails(按钮F,“F”);
}
私有无效按钮\u单击(对象发送者,事件参数e)
{
UpdateUserButtonDetails(按钮,“G”);
}
私有无效按钮单击(对象发送者,事件参数)
{
UpdateUserButtonDetails(按钮“H”);
}
私有无效按钮单击(对象发送者,事件参数e)
{
UpdateUserButtonDetails(buttonI,“I”);
}
私有void UpdateUserButtonDetails(按钮按钮、字符串选择按钮)
{
button.BackColor=Color.GreenYellow;
button.FlatStyle=FlatStyle.Flat;
Text=userToken;
按钮。已启用=错误;
userClicked+=selectedButton;
ComputerTurn();
}
私有字符串getPCToken()
{
返回(“XO”)。替换(userToken“”);
}
私家车
{
检查分数(userClicked,true);
{
如果(检查PCWINCHANCE(pcClicked,false)=false)
CheckForPcWinChance(用户单击,为真);
}
}
公共布尔选择令牌(字符串令牌)
{
if(SelectableTokenForPc(token)==false)
{
返回false;
}
其他的
{
如果(检查PCWINCHANCE(pcClicked,false)=false)
选择按钮(令牌);
返回true;
}
}
PCWinChance专用布尔检查(字符串pcClick,布尔检查)
{
布尔标志=真;
for(int i=0;i=0))
{
flag=false;
}
}
国际单项体育联合会(旗)
{
if(SelectableTokenForPc(Token.RemainingMoves[i])==false)
继续;
其他的
{
选择按钮(Token.RemainingMoves[i]);
返回true;
}
}
}
如果(!标志&检查)
{
string allToken=“ABCDEFGHI”;
随机r=新随机();
while(true)
{
int-index=r.Next(0,9);
if(SelectToken(allToken[index].ToString())==false)
继续;
其他的
打破
}
}
返回false;
}
私有void SelectButton(字符串selectedButton)
{
开关(选择按钮)