public class LightBoard{
/** The lights on the board, where true represents on and false represents off.
*/
private boolean[][] lights;
/** Constructs a LightBoard object having numRows rows and numCols columns.
* Precondition: numRows > 0, numCols > 0
* Postcondition: each light has a 40% probability of being set to on.
*/
public LightBoard(int numRows, int numCols){
lights = new boolean[numRows][numCols];
for (int i = 0; i < numRows; i++) {//for loops
for (int j = 0; j < numCols; j++) {
lights[i][j] = Math.random() < 0.4;// random chance of being on 40 percent
}
}
}
/** Evaluates a light in row index row and column index col and returns a status
* as described in part (b).
* Precondition: row and col are valid indexes in lights.
*/
public boolean evaluateLight(int row, int col){
//first find out how many are on in column
int columnLights = 0;
for (int i = 0; i < lights.length; i++) {
if (lights[i][col]) {
columnLights++;
}
}
//next check conditions for light
if (lights[row][col] == true && columnLights % 2 == 0) {//first condition
return false;
} else if (lights[row][col] == false && columnLights % 3 == 0) {//second condition
return true;
} else {//last condition
return lights[row][col];
}
}
public static void main(String[] args) {
//tester method
LightBoard tester = new LightBoard(10,10);
System.out.println(tester.evaluateLight(3,6));
System.out.println(tester.evaluateLight(2,5));
System.out.println(tester.evaluateLight(0,8));
System.out.println(tester.evaluateLight(2,9));
}
}
LightBoard.main(null);