Quantcast
Channel: Questions in topic: "tree"
Viewing all articles
Browse latest Browse all 513

problem in minimax algorithm

$
0
0
Hello developers, i am trying to make a tic tac toe game . However, while creating the IA , using the minimax algorithm, i encountred and i don't really understand why .Any help would be appreciated. Error type : InvalidOperationException: operation is not valid due to the current state of the object. lines : 127,132. where i try to change the index . Appearently , the list is empty for some reason , when i debug its count , i get 0. here's what i'm using : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; public class GameManager : MonoBehaviour { public static GameManager instance; public string[] board = { "0", "1", "2", "3", "4", "5", "6", "7", "8" }; public List emptySpots = new List(); public readonly int[,] winningCombos = { {0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6} }; public bool Player1Turn = true; public int AiDrawIndex; public GameObject matrix; private void Start() { if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } for (int i = 0; i < 9; i++) { board[i] = "" + i; } emptySpots = checkEmptySpots(board); } public bool CheckWin(string[] currentBoard, string s) { bool checkNext = false; bool check = false; for (int i = 0; i < 8; i++) { for (int j = 0; j < 3; j++) { checkNext = false; if (currentBoard[winningCombos[i, j]] != s) { checkNext = true; break; } } if (checkNext) { continue; } else { return true; } } return check; } public List checkEmptySpots(string[] checkBoard) { List emptySlots = new List(); for (int i = 0; i < checkBoard.Length; i++) { if (board[i] != "X" && board[i] != "O") { emptySlots.Add(i); } } return emptySlots; } public bool checkTie(List checkList) { if (checkList.Count == 0) { return true; } return false; } public string[] drawString(int index, string s, string[]drawBoard, List emptylist) { drawBoard[index] = s; if (emptylist.Contains(index)) { emptylist.Remove(index); } return drawBoard; } public int miniMax(string[] board,string player) { string _player = player; List availableSpots = checkEmptySpots(board); if (CheckWin(board,"O")) { return +10; } else if (CheckWin(board,"X")) { return -10; } else if (checkTie(availableSpots)) { return 0; } List scores = new List(); List moves = new List(); for (int i = 0; i <9; i++) { if (board[i] != "X" && board[i] != "O") { moves.Add(i); scores.Add(miniMax(makeGridMove(board, player, i), switchPlayer(player))); } } if (player == "O") { AiDrawIndex = moves[scores.IndexOf(scores.Max())]; return scores.Max(); } else { AiDrawIndex = moves[scores.IndexOf(scores.Min())]; return scores.Min(); } } string[] cloneGrid(string[] Grid) { string[] Clone = new string[9]; for (int i = 0; i < 9; i++) Clone[i] = Grid[i]; return Clone; } string[] makeGridMove(string[] Grid, string Move, int Position) { string[] newGrid = cloneGrid(Grid); newGrid[Position] = Move; return newGrid; } public int returnMin(List list) { int min = 100000; int index = -1; for (int i = 0; i < list.Count; ++i) { if (list[i] < min) { min = list[i]; index = i; } } return list[index]; } public int returnMax(List list) { int max = -100000; int index = -1; for (int i = 0; i < list.Count; ++i) { if (list[i] > max) { max = list[i]; index = i; } } return list[index]; } public string switchPlayer(string Player) { if (Player == "X") return "O"; else return "X"; } private void Update() { //Debug.Log(AiDrawIndex); if (Player1Turn) { return; } else { miniMax(cloneGrid(board), "O"); drawString(AiDrawIndex, "O", board, emptySpots); GameObject CorrectButton =matrix.transform.Find("" + AiDrawIndex).gameObject; CorrectButton.transform.Find("Text").GetComponent().text = "O"; CorrectButton.GetComponent

Viewing all articles
Browse latest Browse all 513

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>