i am trying to implement a minimax algorithm from scratch in java. General point is known to all, through a tree i try to find the best possible move. I dont have something crucial from code to show now, first i would like someone to give me a general approach so i can start the project and then update the post with my code.
In addition to that i am also going to implement a random heuristic for my game, that will in random choose the next move and pass it to the game, but this will be added later on.
I will be adding bounty on this question.
P.S.
This is not a duplicate, i dont want to copy someone else's code, i have to do the whole code on my own.
So the main thinking to follow is the following:
Calculate the best move with the given game field and the rating mechanism
field the input field where to calculate from ratingMechanisms
the mechanism to rate the move and return the best move.
In a more in depth analysis: The key to the Minimax algorithm is a back and forth between the two players, where the player whose "turn it is" desires to pick the move with the maximum score. In turn, the scores for each of the available moves are determined by the opposing player deciding which of its available moves has the minimum score. And the scores for the opposing players moves are again determined by the turn-taking player trying to maximize its score and so on all the way down the move tree to an end state.
A description for the algorithm, assuming X is the "turn taking player," would look something like:
-If the game is over, return the score from X's perspective.
-Otherwise get a list of new game states for every possible move
-Create a scores list
-For each of these states add the minimax result of that state to the scores list
-If it's X's turn, return the maximum score from the scores list
-If it's O's turn, return the minimum score from the scores list
-You'll notice that this algorithm is recursive, it flips back and forth between the players until a final score is found.
In the main part you should use a for in order to minimize or maximize each level (also you can try adding some debugging like this: //System.out.println("max/min level " + i);)
, then while getting the current level of the tree check if it is null so you get no exceptions, and for the that level add it in the max node, while parsing in the min node.
In order to use the max and min nodes you have create the functions for these.
The maximize part of the algorithms: The node will get the biggest rating of his children. WARNING: if no child exists, the rating will be the min.
For the minimize part of the algorithms. The node will get the lowest rating of his children. WARNING: if no child exists, the rating will be max.