Reputation: 76
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
Upvotes: 0
Views: 412
Reputation: 1468
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.
Upvotes: 2
Reputation: 925
Welcome to Stack Overflow!
It looks like you want to be able to dynamically set the array size when testing. You can create two constructors to instantiate your array:
boolean s [][] = new boolean[8][8];
Knight kn;
Random r = new Random();
public Chess() {
s = new boolean[8][8];
}
public Chess(boolean s[][]) {
this.s = s;
}
This way your test can pass in the array in the constructor. And just a couple of nits, use a better variable name than 's' and tests do not usually use main. Check out Junit https://github.com/junit-team/junit/wiki/Getting-started
Upvotes: 1
Reputation: 1926
I see that you are trying to create a new Chess object, but you are far from doing so because m = new Chess[5][5];
is the syntax for creating a multidimensional array and not an object.
however here is the syntax for creating new Object of the class chess.
Chess m = new Chess();
Upvotes: 0