Created
October 22, 2017 22:53
-
-
Save krisives/fc20af0ffbc73db94e67a1ccd5d62815 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package citydoku; | |
public class BoardFitness { | |
public double evaluate(Board board) { | |
double error = 0; | |
for (int i = 0; i < 9; i++) { | |
error += evaluateCell(board, i); | |
error += evaluateRow(board, i); | |
error += evaluateColumn(board, i); | |
} | |
return error; | |
} | |
public double evaluateRow(Board board, int y) { | |
int duplicates = 0; | |
boolean[] seen = new boolean[9]; | |
for (int x = 0; x < 9; x++) { | |
int z = board.get(x, y); | |
duplicates = seen[z] ? 1 : 0; | |
seen[z] = true; | |
} | |
return duplicates; | |
} | |
public double evaluateColumn(Board board, int x) { | |
int duplicates = 0; | |
boolean[] seen = new boolean[9]; | |
for (int y = 0; y < 9; y++) { | |
int z = board.get(x, y); | |
duplicates = seen[z] ? 1 : 0; | |
seen[z] = true; | |
} | |
return duplicates; | |
} | |
public double evaluateCell(Board board, int c) { | |
int duplicates = 0; | |
boolean[] seen = new boolean[9]; | |
int cellX = c % 3; | |
int cellY = c / 3; | |
for (int iy = 0; iy < 3; iy++) { | |
for (int ix = 0; ix < 3; ix++) { | |
int x = cellX * 3 + ix; | |
int y = cellY * 3 + iy; | |
int z = board.get(x, y); | |
duplicates = seen[z] ? 1 : 0; | |
seen[z] = true; | |
} | |
} | |
return duplicates; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment