Beginner - Help with my code.
Basically, I have a program that simulates a game of Kalah, I have an AI player that evaluates each mvoe possible to see if it is a good move.
Here is the code for this:
Code :
package kalah;
public class IntelligentPlayer extends KalaPlayer {
public IntelligentPlayer() {
}
public int chooseMove(KalaGameState gs) throws NoMoveAvailableException {
for (int move = 1; move < 7; move++) {
int evalValue[] = new int[7];
int maxEval = 0;
int maxEvalMove = 0;
KalaGameState clone = (KalaGameState) gs.clone();
if (clone.getNumStones(move - 1) > 0) {
clone.getTurn();
try {
clone.makeMove(move);
} catch (IllegalMoveException ex) {
}
int firstScore = clone.getScore(0);
int secondScore = clone.getScore(1);
if (clone.playerTurn == 0) {
evalValue[move] = firstScore - secondScore;
} else {
evalValue[move] = secondScore - firstScore;
}
if (evalValue[move] > evalValue[move - 1]) {
maxEval = evalValue[move];
maxEvalMove = move;
}
}}
return maxEvalMove;}
This is probably a retarded question, but like I said I am a beginner.
It doesn't seem to be able to find the maxEvalMove variable even though it is defined in the for loop. Any quick solutions, I tried to move the declaration of the variables outside the for loop, but the computer just plays move 0 every time.
Thanks for your help!
Re: Beginner - Help with my code.
Local variables only exist in the block they are declared in (including any sub-blocks of that block). So declaring the variable in the for-loop block means that once you exit the for-loop, that variable will cease to exist. Note: Declaring != initializing.
Try moving the declaration of maxEvalMove outside the for loop into the main method block.
Re: Beginner - Help with my code.
Hi, thanks for your reply.
I have done that. It seems to remove the error but it returns an incorrect value, I belive it is 0 from when it is initialised.
I think my code is wrong to calculate the correct move. Would you suggest any changes to my code?