/***** ALPHA BETA *****/ // Starts the Alpha Beta Algorithm for every first move (root) by the start parameters // (The Player to start is the ai player) private static MoveString AlphaBetaRoot(int depth, StringState state, Player player) { // Start with the lowest possible value int bestValue = int.MinValue; // The best move result MoveString bestMove = new MoveString(); // Get all first moves List <MoveString> possibleMoves = GetPossibleMoves(state, player); // Iterate through all possible moves foreach (MoveString move in possibleMoves) { // Execute the move on a new reference of the main state StringState moveState = GetStateWithMove(state, move); // Get the value for this route by alpha beta algorithm int value = AlphaBeta(depth - 1, moveState, player, int.MinValue, int.MaxValue); // Is the value for this route better than the previos value? if (value >= bestValue) { // Overwrite the best value bestValue = value; // Save the appropriate move bestMove = move; } } // Return only the best move without the value return(bestMove); }
// Return a new state with the given move on the given state with new reference private static StringState GetStateWithMove(StringState state, MoveString move) { // Deep clone previous state StringState resultState = TypeConverter.DeepCloneState(state); // Remove the token from the previous field (if already placed) resultState = RemoveTokenFromGameState(resultState, move.Token); // If the field already has a token if (resultState.ContainsKey(move.Field)) { Stack <string> tokenStack = new Stack <string>(resultState[move.Field]); tokenStack.Push(move.Token); // Place the allowed Token above the old Token resultState.Remove(move.Field); resultState.Add(move.Field, tokenStack); } else { // Otherwise add the Field with a new Stack with the allowed Token Stack <string> tokenStack = new Stack <string>(); tokenStack.Push(move.Token); resultState.Add(move.Field, tokenStack); } return(resultState); }
// Start is called before the first frame update void Start() { Initiate_params(); for (int i = 0; i < nball; i++) { GameObject newBall = Instantiate(Ballpref); newBall.transform.localScale = new Vector3(diameter, diameter, diameter); MoveController ballscript = newBall.GetComponent <MoveController>(); balls.Add(newBall.gameObject); ballscripts.Add(ballscript); } for (int i = 0; i < 2; i++) { GameObject newString = Instantiate(Stringpref); newString.transform.localScale = new Vector3(10.0f, 10.0f, 10.0f); MoveString stringscript = newString.GetComponent <MoveString>(); strings.Add(newString.gameObject); stringscripts.Add(stringscript); } Initiate_xv(); Get_xv(); UpdatePos(); }
public void ConfirmAIMove() { // Get the best AI Move for the state MoveString moveString = AIController.GetBestMove(TypeConverter.ConvertState(GameFieldControllerPrefab.GameField)); // Convert MoveString to Move with Token GameObject Move move = MoveConverterPrefab.ConvertMove(moveString); // Execute the best ai move directly ExecuteMove(move); // Hide UI AIMoveConfirmationUI.SetActive(false); }
// Converts the given MoveString with String to a Move with GameObjects and returns it public Move ConvertMove(MoveString moveString) { // Iterate through Tokens to get the one with the searched name foreach (GameObject token in Tokens) { // Does the token have the name which is searched for? if (token.name == moveString.Token) { // Return the token combined with the field of the param return(new Move(token, moveString.Field)); } } // Return empty Move if nothing found (shouldn't happen) return(new Move()); }
// Debug function for a move // Debugs the field and the token private static void DebugMove(MoveString move) { Debug.Log("field: " + move.Field); Debug.Log("token: " + move.Token); }