Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner - Help with my code.

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default 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:

    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) {
                    try {
                    } 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!

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default 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.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default 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?

Similar Threads

  1. Basic Beginner Help
    By SRD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:27 PM
  2. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM
  3. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM
  4. Good Beginner AWT tutorial
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: August 4th, 2009, 10:31 AM
  5. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 08:52 AM