-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
237 lines (191 loc) · 4.9 KB
/
Solution.java
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Solution extends JFrame implements ActionListener
{
JPanel p,p1,p2;
MyButton b[][];
JButton next,prev,exit;
JLabel footer1,footer2,time;
ImageIcon normalButtonIcon =new ImageIcon("images/normal.jpg");
ImageIcon blankButtonIcon = new ImageIcon("images/blank.jpg");
ImageIcon selectedButtonIcon = new ImageIcon("images/selected.jpg");
//int xSelected,ySelected;
int mark;
File sol = new File("files/solution.txt");
FileReader fr;
//CONSTRUCTOR STARTS
Solution()
{
//Initialise the super class---just to get the text on the title bar
super("Brainvita-Solution");
//further changes
setLayout(new BorderLayout());
footer1 =new JLabel("This is the");
footer2 =new JLabel("Solution");
time = new JLabel("00:00");
footer1.setSize(100,10);
footer2.setSize(100,10);
p= new JPanel();
p1 = new JPanel();
p2 = new JPanel();
//THE OPTION SECTION
p1.setLayout(new FlowLayout());
next = new JButton("NEXT STEP");
prev =new JButton("PREVIOUS STEP");
exit = new JButton("EXIT");
next.addActionListener(this);
prev.addActionListener(this);
exit.addActionListener(this);
p1.add(next);
p1.add(prev);
p1.add(exit);
add(p1,BorderLayout.NORTH);
// THE MAIN BOARD SECTION
b=new MyButton[7][];
for(int i=0;i<7;i++)
b[i]=new MyButton[7];
p.setLayout(new GridLayout(7,7));
for(int i=0;i<7;i++)
for(int j=0;j<7;j++)
{
if(isValidBlock(i,j))
if(i==3&&j==3)
{ b[i][j] = new MyButton(blankButtonIcon,i,j);
b[i][j].resetFlag();
b[i][j].addActionListener(this);
}
else
{ b[i][j] = new MyButton(normalButtonIcon,i,j);
b[i][j].setFlag();
b[i][j].addActionListener(this);
}
else
b[i][j]= new MyButton(i,j);
p.add(b[i][j]);
if(!isValidBlock(i,j))
b[i][j].setEnabled(false);
}
add(p,BorderLayout.CENTER);
//THIS IS FOOTER TO DISPLAY MESSAGES
p2.setLayout(new GridLayout(0,3));
p2.add(footer1);
p2.add(footer2);
p2.add(time);
//p2.setSize(50,50);
add(p2,BorderLayout.SOUTH);
setSize(420,500);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
mark=0;
}
boolean isValidNumber(int a)
{
if(a==2||a==3||a==4)
return true;
else
return false;
}
//CHECKS IF THE SELECTED BLOCK IS VALID
boolean isValidBlock(int a ,int b)
{
if(isValidNumber(a)||isValidNumber(b))
return true;
else
return false;
}
void nextF()
{
int a1,a2,a3,a4,m;
char ch[]=new char[4];
a1=a2=a3=a4=-1;
m=mark;
try{
fr = new FileReader(sol);
while(m>-4)
{
fr.read(ch,0,4);
m-=4;
}
if(mark<=sol.length())
{
a1=ch[0]-48;
a2=ch[1]-48;
a3=ch[2]-48;
a4=ch[3]-48;
b[a1][a2].changeImage(blankButtonIcon);
b[a1][a2].resetFlag();
b[a3][a4].changeImage(normalButtonIcon);
b[a3][a4].setFlag();
if(a2==a4)
{ b[(a1+a3)/2][a4].changeImage(blankButtonIcon);
b[(a1+a3)/2][a4].resetFlag();
}
else
if(a1==a3)
{ b[a1][(a2+a4)/2].changeImage(blankButtonIcon);
b[a1][(a2+a4)/2].resetFlag();
}
if(mark<sol.length())
mark=mark+4;
}
fr.close();
}
catch(Exception ex)
{
}
}
void prevF()
{
int a1,a2,a3,a4,leng;
a1=a2=a3=a4=-1;
leng=mark;
char ch[]=new char[leng];
StringBuffer sbuf=null;
try {
fr = new FileReader(sol);
fr.read(ch);
if(leng>3)
{
a1=ch[leng-4]-48;
a2=ch[leng-3]-48;
a3=ch[leng-2]-48;
a4=ch[leng-1]-48;
{ b[a1][a2].changeImage(normalButtonIcon);
b[a1][a2].setFlag();
b[a3][a4].changeImage(blankButtonIcon);
b[a3][a4].resetFlag();
if(a2==a4)
{ b[(a1+a3)/2][a4].changeImage(normalButtonIcon);
b[(a1+a3)/2][a4].setFlag();
}
else
if(a1==a3)
{ b[a1][(a2+a4)/2].changeImage(normalButtonIcon);
b[a1][(a2+a4)/2].setFlag();
}
mark = mark - 4;
}
}
fr.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
public void actionPerformed (ActionEvent e)
{
JButton bt = (JButton)e.getSource();
if(bt==next)
nextF();
if(bt==prev)
prevF();
if(bt==exit)
setVisible(false);
}
/*public static void main(String args[])
{
new Solution();
}*/
}