We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Problems with a player for Checkers

keksy
keksy asked
on
Medium Priority
507 Views
Last Modified: 2013-12-26
Hi every1,

I have a problem writting my player for tha game Checkers (i guess all of you know this game).  
So - I'm talking about the International version of the game  10x10. I have the board (it is in german, but it is not so important). My task is to write a player for it. Everything is in Java 2. I have the last version 1.5_06. As a development program - Eclipse 3.1.
I'm ready with the algorithms and so on and I am able to load the player, but when the game starts - a become the NullPointerException. Inside I work with a couple of lists and 2 arries. I have already checked everything more than 3 times... but I cannot see my error.

If someone wants to help, I am ready to send my source, the board and its source too (of course). I guess it is something little, but I can't find it :(
You can reach me under:  keksy@yahoo.com at any time

Thanks in advance
Comment
Watch Question

CERTIFIED EXPERT

Commented:
hi keksy,

its not in the terms of ee to resolve the questions apart from here via email!

maybe you should post a pointer in the java-area to this question, since this is more a java-related question, than a ai/physics - one.
if you are unsure, how to create a pointer, post a request here.

i'm not a java-programmer, but c++, but debugging is quite general, so lets go ..

did you find the code, where this exception is thrown? if not use a debugger and find it.. then post it here, so we can try to help you.

good luck :)
ike

Author

Commented:
Yes, please, tell me how to post a request to the Java-area...
Debugger - I cannot run it, because I have problems with references to the functions... :(
CERTIFIED EXPERT
Commented:
you should fix that... programming without debugger gets harder and harder, the grater the project grows...
download another ide, if you cant fix it. if you dont get the debugger running insert debug-output to your
programm at significant places and where you suspect the error to be.. try to find out, where the app crashes.

go to the java area and add a question called "pointer to ... blablabla". put the url to this question into the body, like this:
http://www.experts-exchange.com/Programming/Game_Development/AI_Physics/Q_21799592.html
give it 20 points, dont worry you get them back as long as nobody posts to this thread, but even if someone posts there
you can ask in community-support for points refund, thats ok...

ike

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
Please post the stack trace of the exception
>>> Debugger - I cannot run it, because I have problems with references to the functions... :(

Impossible that's C++ : in JAVA there are no references to functions!

;JOOP!

Author

Commented:
No, this is Java. The problem is that I have given the board and I have also a Player.class file and I should use this...  and when try to add a main-method in my classes, is says: "Cannot make static reference to non-static methods". If I create an object in the main-class, it is not the same, as it should be in original :(
I'll do my best to debug it, but... I am not sure I can handle it out.
The code is long and I do not know if it'll be a good idea to post it here...  I can upload it in my website and post here the links
>>> "Cannot make static reference to non-static methods".

Do you understand this? If not, drop the task and start a tutorial, because this is essential.

;JOOP!

Author

Commented:
Thanks for the tip, sciuriware :)

So, here is the link I have promissed. It is too long to place it here...
http://kater.duo-vision.com/

In regard to points - I give all my points (at about 500)...

Thanks in advance
I've looked at your 'whole' package.
The error is a null reference encountered when an action is fired: probably a false argument to "addActionListener()"
when setting up the GUI.
Now, I can't find any kind of GUI build-up in your sources ..................

;JOOP!

Author

Commented:
I do not get your point... :(
And what kind of GUI are you looking for?  Under GUI I mean the board...

Author

Commented:
I am trying to debug it, while adding to the player/kater/Kater.java file this:

public static void main (String args[]){
      Moves board = new Moves();
      Kater cat = new Kater(board);
      cat.play();
}


but I'm getting:
java.lang.NoSuchMethodError: main
Exception in thread "main"

I really don't get it anymore - totally confused! Just sitting in front of the screen and... out of ideas :(((
In ECLIPSE you must create a project; I think you did it.
Then, for the first time, select the main module (Kater.java) and select Run As.
That will work; from then it runs after Ctrl/F11

Now that GUI: I just read the error messages in your package. Are those not actual?

;JOOP!

Author

Commented:
Yes, I did a priject, imported the .jar file and so on... I also know how to run and re-run a file... but exactly the Eclipse gives me this error... :(

The error messages are actual, but this what I get here is nothing to do with those errors (or I am wrong) ??
Am I writing something, that doesn't match... I think the code, with which I am trying to run and debug the Kater.java is correct... and it gives me this funny erros.. damn :(
I found something: Kater.java ====    public class Kater extends kater.Kater{}

That's a weird class: ECLIPSE doesn't understand that it inherits a 'main()'.
You should change that by giving it a main that calls the super class's main.

;JOOP!

Author

Commented:
Actually I do not touch this "start-class" (if I can say like this) right now.  I am trying to debug tha player and now I work within the package Kater. I am trying to add a main-function to the kater.java, which is inside tha package and I see this error... :(
The file kater.java, in which is this: "public class Kater extends kater.Kater{}" is used from the game to load the player from the verry beginning. It just shows to the game where are the player's .class files...
Should I chage anything there? I don't think so, but if you say it is necessary - I will!

I just don't get it - I am adding a main-method in the kater.java (in the file in tha package), I am 101% sure there are no miss-spelling or something else, I declare an object from type Kater and I apply the function play() to this newly created object...  and I get:

java.lang.NoSuchMethodError: main
Exception in thread "main"

What the f.... is going on :(((((((((((((((((((((((((
Do you want to change Kater.java?

Then it is simple:

Kater.java :

public class Kater
{
      public static void main(String args[])
      {
            kater.Kater.main(args);
      }

}

This class will simply call the other one.

;JOOP!
And don't curse the computer: the poor thing is always right.

;JOOP!

Author

Commented:
This doesn't help, sciuriwave. It doesn't change anything. This just adds a link to the main, that I have already written. And I still get the same error... :(((
You start the application by Kater.java, containing the above and it says "java.lang.NoSuchMethodError: main"?

Impossible, can you post the entire Kater.java as it is now?

;JOOP!

Author

Commented:
As you want...


package kater;
import eMad.Player;
import eMad.Board;

/* Kater.java : My own player
 * Copyright (C) 2006  Vasil Vasilev
 */

public class Kater extends Player{
 
  private Moves currentBoard;
  private int color;


  /**
   * The maximum depth for the minimax-method
   */
  private static final int maxDepth = 3;

  /**
   * Weight of the fields of the board
   */

  private static final int tableWeight[] = { 5, 5, 5, 5, 5,
                                             5, 4, 4, 4, 4,
                                             4, 3, 3, 3, 5,
                                             5, 3, 2, 2, 4,
                                             4, 2, 1, 3, 5,
                                             5, 3, 1, 2, 4,
                                             4, 2, 2, 3, 5,
                                             5, 3, 3, 3, 4,
                                             4, 4, 4, 4, 5,
                                             5, 5, 5, 5, 5};
 
 
 
  /**
   * Constructor.
   * @param gameBoard The board, that the player uses for his moves
   */
  public Kater (Moves gameBoard) {
      currentBoard = gameBoard;
    //color = BLACK;
  }
  public Kater (){};

  /**
   * This method is called always, when it is the player's turn to move
   */
  public void play () {
    try {
      List moves = minimax (currentBoard);
      //int status = getManToJump();    
     
      if (!moves.isEmpty ())
        currentBoard.move (moves);
    }
    catch (BadMoveException bad) {
      bad.printStackTrace ();
      System.exit (-1);
    }
  }


  //public void setBoard (Moves board) {
  //  currentBoard = board;
  //}

  /**
   * Indicates if there is an available move to play
   */
  private boolean mayPlay (List moves) {
    return !moves.isEmpty () && !((List) moves.peek_head ()).isEmpty ();
  }
 

  /**
   * The minimax-strategy algorithm
   */
  private List minimax (Moves board) throws BadMoveException {
    List sucessors;
    List move, bestMove = null;
    Moves nextBoard;
    int value, maxValue = Integer.MIN_VALUE;

    sucessors = board.legalMoves ();
    while (mayPlay (sucessors)) {
      move =  (List) sucessors.pop_front ();
      nextBoard = (Moves) board.clone ();

      //Debug.println ("******************************************************************");
      nextBoard.move (move);
      value = minMove (nextBoard, 1, maxValue, Integer.MAX_VALUE);

      if (value > maxValue) {
        //Debug.println ("Max value : " + value + " at depth : 0");
        maxValue = value;
        bestMove = move;
      }
    }

    //Debug.println ("Move value selected : " + maxValue + " at depth : 0");

    return bestMove;
  }
 
  /**
   * Returns the most valuable move a player can make
   */
  private int maxMove (Moves board, int depth, int alpha, int beta)
   throws BadMoveException {
    if (cutOffTest (board, depth))
      return eval (board);


    List sucessors;
    List move;
    Moves nextBoard;
    int value;

    sucessors = board.legalMoves ();
    while (mayPlay (sucessors)) {
      move = (List) sucessors.pop_front ();
      nextBoard = (Moves) board.clone ();
      nextBoard.move (move);
      value = minMove (nextBoard, depth + 1, alpha, beta);

      if (value > alpha) {
        alpha = value;
      }

      if (alpha > beta) {
        return beta;
      }
 
    }

    return alpha;
  }

  /**
   * Returns the worst move a player can make
   */
  private int minMove (Moves board, int depth, int alpha, int beta)
   throws BadMoveException {
    if (cutOffTest (board, depth))
      return eval (board);


    List sucessors;
    List move;
    Moves nextBoard;
    int value;

   
    sucessors = (List) board.legalMoves ();
    while (mayPlay (sucessors)) {
      move = (List) sucessors.pop_front ();
      nextBoard = (Moves) board.clone ();
      nextBoard.move (move);
      value = maxMove (nextBoard, depth + 1, alpha, beta);

      if (value < beta) {
        beta = value;
      }

      if (beta < alpha) {
        return alpha;
      }
    }

    return beta;
  }

   /**
    * Evaluates the force of the current player
    */
   private int eval (Moves board) {
      int colorQueen;
      int enemy, enemyQueen;
     
      if (color == Moves.WHITE) {
        colorQueen = Moves.WHITE_QUEEN;
        enemy = Moves.BLACK;
        enemyQueen = Moves.BLACK_QUEEN;
      }
      else {
        colorQueen = Moves.BLACK_QUEEN;
        enemy = Moves.WHITE;
        enemyQueen = Moves.WHITE_QUEEN;
      }
     
      int colorForce = 0;
      int enemyForce = 0;
      int piece;

      try {
        for (int i = 0; i < 50;  i++) {
          piece = board.getPiece (i);
       
        if (piece != Moves.EMPTY)
           if (piece == color || piece == colorQueen)
             colorForce += calculateValue (piece, i);
           else
             enemyForce += calculateValue (piece, i);
        }
      }
      catch (BadCoordException bad) {
        bad.printStackTrace ();
        System.exit (-1);
      }

      return colorForce - enemyForce;
   }

   /**
    * It calculates the force of one stone
    */
   private int calculateValue (int piece, int pos) {
      int value;
     
      if (piece == WHITE ) //a simple stone
      if (pos >= 5 && pos <= 9)
          value = 7;
        else
          value = 5;
      else if (piece != BLACK) //a simple stone
      if (pos >= 40 && pos <= 44)
          value = 7;
        else
          value = 5;
      else // queen
      value = 10;

      return value * tableWeight[pos];
   }


  /**
   * Indicates if it can stop the process of evaluation of the best move
   */
  private boolean cutOffTest (Moves board, int depth) {
    return depth > maxDepth || board.hasEnded ();
  }


public String getAuthor() {
      // TODO Auto-generated method stub
      return "Vasil Vasilev & Oleksiy Slonimskyy";
}
public String getPlayerName(){
    return "Der gestiefelte Kater";
  }

public static void main (String[] args){
      Board board = new Board();
      Kater cat = new Kater();
      cat.play();
}

}
Yes that's the file   Kater.java   in the directory kater.
But you are starting the file Kater.java 1 level higher.

That file should NOT have a package and read:

import kater.Kater;

public class Kater
{
      public static void main(String args[])
      {
            kater.Kater.main(args);
      }

}

// as I said before.

Besides that, I don't know who designed the whole program, but the
naming of some directories and files is confusing.
Now, let's try to start that top-level Kater.java
See what happens.

;JOOP!

Author

Commented:
java.lang.NoSuchMethodError: main
Exception in thread "main"

Author

Commented:
I am sure that I compile all the changes I make. I use the command  "javac.exe -classpath ..\chequers.jar *.java kater\*.java" to compile any changes,recently made, but I still get the same error...
I tried to make a new project in Eclipse - there I have a directory player, where all my source is. I am trying to avoid this stupid inheritance. I put the checkers.jar in the root, then my source in a folder player and compile everything with the command "javac.exe -classpath ..\chequers.jar *.java" from this folder. The compilation goes without any mistake and when I run Kater.java... AGAIN the f... mistake :(

I am knocked out! Really!

Can anybody help me, please... please :((
What's the stacktrace = where does the Exception happen?

e.printStackTrace();

;JOOP!

Author

Commented:
I am leaving it like this! I just cannot take the project to the end :(
It's a pitty for whole this time - I was sitting almost day and night and spent my free time to write this and at the end - a lot of mistakes. This will teach me...

Anyway, you will alll the points from this! Please, post something in the "pointer" to give you the points from there too...

Thanks for you efforts!

Best!
It's a pity that we didn't solve this problem.
Please, in the future, provide us with all error messages so that at least we can do more.

;JOOP!

Author

Commented:
I think I made my best to support solving the problem. I published my code, provided with erros and everything I made to troubleshoot and debug it. The StackTrace - actually I don't know how to implement it in my code, in order to provide the results from the trace :(
catch(Exception e)
{
     e.printStackTrace();
}


;JOOP!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.