-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
120 lines (94 loc) · 3.96 KB
/
GameWindow.java
File metadata and controls
120 lines (94 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class GameWindow extends JFrame{
private static final JPanel mainPanel = new JPanel();
public static PlayableTile[][] tileGrid = new PlayableTile[0][0];
private static int height = 0;
private static int width = 0;
// The whole window is basically just a GridLayout and each item in the GridLayout is a subclass of Tile to give different behaviour depending on where it is added.
public GameWindow() {
height = ParsedImage.getHeight();
width = ParsedImage.getWidth();
tileGrid = new PlayableTile[height+1][width+1];
GridLayout innerLayout = new GridLayout(height + 1, width + 1);
mainPanel.setLayout(innerLayout);
populateNonogram();
this.setTitle("nonograms");
this.setContentPane(mainPanel);
this.pack();
this.setVisible(true);
}
// Fill in the grid with the suitable instance
public void populateNonogram() {
for(int row = 0; row < height+1; row++) {
for(int column = 0; column < width+1; column++) {
Tile tile = getSuitableTile(column, row);
tile.setPreferredSize(new Dimension(64, 64));
mainPanel.add(tile);
}
}
}
// Returns a suitable Tile subclass, depending on its position and the bit depth of the image to be added to the grid.
public Tile getSuitableTile(int x, int y) {
Tile tile;
if(ParsedImage.isColoured()) {
tile = (x == 0 || y == 0) ? new ColouredInfoTile(x, y) : new ColouredTile(x, y, new Color(ParsedImage.ignoredColour));
} else {
tile = (x == 0 || y == 0) ? new MonochromeInfoTile(x, y) : new MonochromeTile(x, y);
}
if(x == 0 && y == 0) {
tile = new GameStateTile(x, y);
}
if (tile instanceof PlayableTile) {
tileGrid[y-1][x-1] = (PlayableTile) tile;
}
return tile;
}
// Marks the grid, adding a red border if the tile was wrong and a green if it was right, reveals the solution
public static void markSolution() {
int errors = 0;
for(int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
boolean correct = tileGrid[x][y].reveal();
Color borderColor;
if(!correct) {
borderColor = Color.red;
errors++;
} else {
borderColor = Color.green;
}
if(tileGrid[x][y].getBackground().getRGB() == borderColor.getRGB()) {
borderColor = borderColor.darker();
}
tileGrid[x][y].setBorder(new LineBorder(borderColor, 5));
}
}
// Adds a polite popup about the game statistics
String message = "Thanks for playing!\nBetter luck next time... You got " + errors + " errors out of " + width*height ;
if(errors == 0) {
message = "Wow, impressive! You completed the puzzle successfully!";
}
JOptionPane.showMessageDialog(null, message);
}
// Gets a slice of the tileGrid 2d array. Takes in the InfoTile coordinate that is asking to determine if the slice should be horizontal or vertical
// from this position
public static PlayableTile[] getTileSlice(int x, int y) {
PlayableTile[] slice = new PlayableTile[0];
if(x == 0 && y == 0) {
return slice;
}
if (y == 0) {
slice = new PlayableTile[height];
//slice[i] = getBoolean(x-1, y+i) ? 1 : 0;
System.arraycopy(tileGrid[x - 1], y, slice, 0, height);
} else if (x == 0) {
slice = new PlayableTile[width];
for(int i = 0; i < width; i++) {
//slice[i] = getBoolean(x+i, y-1) ? 1 : 0;
slice[i] = tileGrid[x+i][y-1];
}
}
return slice;
}
}