using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Rendering; using static UnityEditor.Experimental.GraphView.GraphView; public class Minimax : MonoBehaviour { int[] allowedSteps = {1, 2, 3}; //List positions = new List(); //int currentPosition = 0; MainGame game; float lastTime = 0; char opposite(char p) { return (p == 'N') ? 'X' : 'N'; } List availableMoves(string stage) { List answers = new List(); //char player = stage[stage.Length-1]; char player = stage[^1]; int place=int.Parse(stage.Substring(0, stage.Length - 1)); char newplayer=opposite(player); foreach(int stepLength in allowedSteps){ if(place + stepLength <= game.N) { answers.Add((place + stepLength).ToString() + newplayer); } } return answers; } float score(string stage, int depth=0) { char playerStr = stage[^1]; int place = int.Parse(stage.Substring(0, stage.Length - 1)); if (place == game.N) { if (playerStr == 'N') { return 1; } else { return -1; } } //make game length to 20 //find depth, where in some situations you can calcualate to end //and some situations not //try to find length of game and good depth, //where calculation needs time, but not too mutch if (depth >= 6) { return 0; } //game not finished yet //print out all available moves from this point // Debug.Log(availableMoves(stage).ToSeparatedString(", ")); List moves = availableMoves(stage); var scores = moves.Select(move => score(move, depth+1)); return (playerStr=='N') ? scores.Min() : scores.Max(); } string BestMove(string stage) { List moves = availableMoves(stage); List scores = moves.Select(move => score(move)).ToList(); float bestScore=score(stage); int bestPosition = scores.IndexOf(bestScore); return moves[bestPosition]; } void Start() { //Debug.Log(string.Join(",", availableMoves("2X"))); game = GameObject.Find("GameObject").GetComponent(); // positions = availableMoves("4X"); //Change program, print out score for some starting position //example 2X //Debug.Log(positions.ToString()); //Debug.Log(score("2X")); Debug.Log(BestMove("2X")); } void Update() { if (Time.time >= lastTime + 2) { if (game.sphere.GetComponent().material.color == game.player) { game.DisplayStage(BestMove(game.GetCurrentStage())); } //game.DisplayStage(positions[currentPosition]); //currentPosition++; //if (currentPosition == positions.Count) { currentPosition = 0; } //Debug.Log(score(positions[currentPosition])); lastTime=Time.time; } } }