Search code examples
javamultidimensional-arraychess

Make new Multidimensional Array in Test class


I am working on a chess game, but I do not know how can I make a test class and in that test class create a new Array and test methods from ChessClass.

My Chess Table class with a method that makes a random move import java.util.Random;

public class Chess {

    boolean s [][] = new boolean[8][8];
    Knight kn;
    Random r = new Random();

    public void RandStart(){
        kn = new Knight(r.nextInt(), r.nextInt());
        s [kn.getX()][kn.getY()] = true;
    }


    public void print(){
        for(int i = 0;i < s.length;i++){
            for(int j = 0;j < s[i].length;j++){
                System.out.println(s[i][j]);
            }   
        }
    }



}

The only thing I do not know is how to make it work in the test class I get an error and I can not use the methods from Chess class

public class Test {

    public static void main(String[] args){
        Chess m = null;
        m = new Chess[5][5];
        m.RandStart();
    }
}

Thanks in advance


Solution

  • In your main method your are trying to assign a two dimensional Chess array to a normal Chess object.

    public class Test {
    
        public static void main(String[] args){
            Chess m = new Chess();
            m.RandStart();
        }
    }
    

    Should work to fix your problem.

    Also I suggest you change RandStart() to randStart() good programming practice is all.