Skip to content
Snippets Groups Projects
Commit 9f460082 authored by Justin's avatar Justin
Browse files

Fixed information hiding

parent 7c5c76b6
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ public class HintGenerator {
int width = ng.getWidth();
int maxHints = height / 2 + 1;
int[][] hints = new int[width][maxHints];
boolean[][] imageData = ng.getImageData();
int hintnum = 0;
int blackCount = 0;
......@@ -14,7 +13,7 @@ public class HintGenerator {
hintnum = 0;
blackCount = 0;
for (int y = 0; y < height; y++) {
if (imageData[x][y]) {
if (ng.getPixel(x, y)) {
blackCount++;
} else {
if (blackCount != 0) {
......@@ -37,7 +36,6 @@ public class HintGenerator {
int width = ng.getWidth();
int maxHints = width / 2 + 1;
int[][] hints = new int[height][maxHints];
boolean[][] imageData = ng.getImageData();
int hintnum = 0;
int blackCount = 0;
......@@ -46,7 +44,7 @@ public class HintGenerator {
hintnum = 0;
blackCount = 0;
for (int x = 0; x < width; x++) {
if (imageData[x][y]) {
if (ng.getPixel(x, y)) {
blackCount++;
} else {
if (blackCount != 0) {
......
public class Hints {
private int hintWidth;
private int maxHints;
private int[][] hints;
public Hints(Nonogram ng, boolean rows) {
if (rows) {
generateRowHints(ng);
} else {
generateColHints(ng);
}
}
private int[][] generateColHints(Nonogram ng) {
int height = ng.getHeight();
int width = ng.getWidth();
hintWidth = width;
maxHints = height / 2 + 1;
hints = new int[width][maxHints];
int hintnum = 0;
int blackCount = 0;
for (int x = 0; x < width; x++) {
hintnum = 0;
blackCount = 0;
for (int y = 0; y < height; y++) {
if (ng.getPixel(x, y)) {
blackCount++;
} else {
if (blackCount != 0) {
hints[x][hintnum] = blackCount;
hintnum++;
}
blackCount = 0;
}
}
if (blackCount != 0) {
hints[x][hintnum] = blackCount;
}
}
return hints;
}
private int[][] generateRowHints(Nonogram ng) {
int height = ng.getHeight();
int width = ng.getWidth();
hintWidth = height;
maxHints = width / 2 + 1;
hints = new int[height][maxHints];
int hintnum = 0;
int blackCount = 0;
for (int y = 0; y < height; y++) {
hintnum = 0;
blackCount = 0;
for (int x = 0; x < width; x++) {
if (ng.getPixel(x, y)) {
blackCount++;
} else {
if (blackCount != 0) {
hints[y][hintnum] = blackCount;
hintnum++;
}
blackCount = 0;
}
}
if (blackCount != 0) {
hints[y][hintnum] = blackCount;
}
}
return hints;
}
public int getHintWidth() {
return hintWidth;
}
public int getHintCount(int position) {
int count = 0;
for (int i = 0; i < maxHints; i++) {
if (hints[position][i] != 0) {
count++;
} else {
break;
}
}
return count;
}
public int getHint(int position, int hintNumber) {
return hints[position][hintNumber];
}
}
......@@ -71,7 +71,7 @@ public class JunitTest {
for (int y = 0; y < test.getHeight(); y++) {
for (int x = 0; x < test.getWidth(); x++) {
assertEquals(test.getImageData()[y][x],testImageData1[y][x]);
assertEquals(test.getPixel(y,x),testImageData1[y][x]);
}
}
......@@ -83,7 +83,7 @@ public class JunitTest {
for (int y = 0; y < test2.getHeight(); y++) {
for (int x = 0; x < test2.getWidth(); x++) {
assertEquals(test2.getImageData()[y][x],testImageData2[y][x]);
assertEquals(test2.getPixel(y,x),testImageData2[y][x]);
}
}
......@@ -95,7 +95,7 @@ public class JunitTest {
for (int y = 0; y < test3.getHeight(); y++) {
for (int x = 0; x < test3.getWidth(); x++) {
assertEquals(test3.getImageData()[y][x],testImageData3[y][x]);
assertEquals(test3.getPixel(y,x),testImageData3[y][x]);
}
}
......@@ -107,7 +107,7 @@ public class JunitTest {
for (int y = 0; y < test4.getHeight(); y++) {
for (int x = 0; x < test4.getWidth(); x++) {
assertEquals(test4.getImageData()[y][x],testImageData1[y][x]);
assertEquals(test4.getPixel(y,x),testImageData1[y][x]);
}
}
......@@ -119,7 +119,7 @@ public class JunitTest {
for (int y = 0; y < test5.getHeight(); y++) {
for (int x = 0; x < test5.getWidth(); x++) {
assertEquals(test5.getImageData()[y][x],testImageData2[y][x]);
assertEquals(test5.getPixel(y,x),testImageData2[y][x]);
}
}
......@@ -131,7 +131,7 @@ public class JunitTest {
for (int y = 0; y < test6.getHeight(); y++) {
for (int x = 0; x < test6.getWidth(); x++) {
assertEquals(test6.getImageData()[y][x],testImageData3[y][x]);
assertEquals(test6.getPixel(y,x),testImageData3[y][x]);
}
}
......
......@@ -4,38 +4,33 @@ import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Nonogram ng = new Nonogram(new File("blue.bmp"));
int[][] colHints = HintGenerator.generateColHints(ng);
int[][] rowHints = HintGenerator.generateRowHints(ng);
boolean[][] image = ng.getImageData();
for (int y = 0; y < ng.getHeight(); y++) {
for (int x = 0; x < ng.getWidth(); x++) {
if (image[x][y]) {
System.out.print("*");
} else {
System.out.print(" ");
}
Nonogram ng = new Nonogram(new File("test2.png"));
Hints r = new Hints(ng, true);
Hints c = new Hints(ng, false);
for (int pos = 0; pos < r.getHintWidth(); pos++) {
for (int i = 0; i < r.getHintCount(pos); i++) {
System.out.print(r.getHint(pos, i) + " ");
}
System.out.println();
}
for (int ii = 0; ii < ng.getWidth() / 2 + 1; ii++) {
for (int i = 0; i < ng.getWidth(); i++) {
System.out.print(colHints[i][ii] + " ");
for (int pos = 0; pos < c.getHintWidth(); pos++) {
for (int i = 0; i < c.getHintCount(pos); i++) {
System.out.print(c.getHint(pos, i) + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < ng.getWidth(); i++) {
for (int ii = 0; ii < ng.getWidth() / 2 + 1; ii++) {
System.out.print(rowHints[i][ii] + " ");
for (int y = 0; y < ng.getHeight(); y++) {
for (int x = 0; x < ng.getWidth(); x++) {
if (ng.getPixel(x, y)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
......@@ -16,6 +16,17 @@ public class Nonogram {
loadImage(image);
}
public Nonogram(int width, int height) {
this.width = width;
this.height = height;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imageData[x][y] = false;
}
}
}
private int fixRGB(int rgb) {
if (((rgb & 0x00FF0000) > 0x00F00000) && ((rgb & 0x0000FF00) > 0x0000F000) && ((rgb & 0xFF) > 0xF0)) {
return RGB_WHITE;
......@@ -43,15 +54,18 @@ public class Nonogram {
imageData[x][y] = false;
break;
default:
int l = bi.getRGB(x, y);
throw new IOException("Invalid image format");
}
}
}
}
public void setPixel(int x, int y, boolean value) {
imageData[x][y] = value;
}
public boolean[][] getImageData() {
return imageData.clone();
public boolean getPixel(int x, int y) {
return imageData[x][y];
}
public int getWidth() {
......@@ -61,4 +75,16 @@ public class Nonogram {
public int getHeight() {
return height;
}
public boolean equals(Nonogram n2) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (imageData[x][y] != n2.imageData[x][y]) {
return false;
}
}
}
return true;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment