Skip to content
This repository was archived by the owner on Jan 6, 2022. It is now read-only.

Commit ef2bf5a

Browse files
Added highlighting function
1 parent 05dba51 commit ef2bf5a

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

src/controller/SudokuController.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package controller;
22

3+
import java.awt.Color;
34
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionListener;
56

@@ -26,14 +27,15 @@ public SudokuController(Sudoku model, MasterController master)
2627
this.master = master;
2728
this.setView(new SudokuView(this));
2829
view.fixGridNumbers(model.getOriginal());
30+
view.resetColor();
2931
}
3032

3133
@Override
3234
/**
33-
* If the action starts with grid, changes the grid at that position to
35+
* If the action starts with "Grid", changes the grid at that position to
3436
* selected number
3537
*
36-
* If the action is Check, it will check if the grid is solved or not
38+
* If the action is "Check", it will check if the grid is solved or not
3739
*/
3840
public void actionPerformed(ActionEvent e)
3941
{
@@ -43,7 +45,10 @@ public void actionPerformed(ActionEvent e)
4345
int i = action.charAt(4) - '0';
4446
int j = action.charAt(5) - '0';
4547
if (view.getSelectedNumber() != 0)
48+
{
4649
view.setGridAt(i, j, "" + view.getSelectedNumber());
50+
view.changeColor(Color.yellow, view.getSelectedNumber());
51+
}
4752
else
4853
view.setGridAt(i, j, "");
4954
}
@@ -55,6 +60,13 @@ else if (action.equals("Check"))
5560
else
5661
JOptionPane.showMessageDialog(null, "It is Wrong");
5762
}
63+
else if (action.substring(0, 5).equals("Radio"))
64+
{
65+
view.resetColor();
66+
int val = action.charAt(6) - '0';
67+
if (val != 0)
68+
view.changeColor(Color.yellow, val);
69+
}
5870
}
5971

6072
/**

src/view/SudokuView.java

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public SudokuView(ActionListener controller)
2929
{
3030
setLayout(new BorderLayout());
3131

32+
//************************
33+
// Created the Sudoku grid
34+
//************************
3235
JPanel sudoGrids = new JPanel();
33-
// sudoGrids.setPreferredSize(new Dimension(100,100));
3436
sudoGrids.setLayout(new GridLayout(9,9,0,0));
3537
sudoGrids.setBorder(BorderFactory.createTitledBorder("Sudoku"));
3638

@@ -51,13 +53,13 @@ public SudokuView(ActionListener controller)
5153
if (j == 8)
5254
b[3] = 0;
5355
if (i == 3 || i == 6)
54-
b[0] = 2;
56+
b[0] = 3;
5557
if (j == 3 || j == 6)
56-
b[1] = 2;
58+
b[1] = 3;
5759
if (i == 2 || i == 5)
58-
b[2] = 2;
60+
b[2] = 3;
5961
if (j == 2 || j == 5)
60-
b[3] = 2;
62+
b[3] = 3;
6163
grid.setBorder(BorderFactory.createMatteBorder(b[0], b[1], b[2], b[3], Color.black));
6264
grid.setActionCommand("Grid"+i+j);
6365
grid.addActionListener(controller);
@@ -69,6 +71,9 @@ public SudokuView(ActionListener controller)
6971

7072
add(sudoGrids, BorderLayout.CENTER);
7173

74+
//*********************
75+
// Radio Button Created
76+
//*********************
7277
noBtn = new JRadioButton[10];
7378
JPanel radios = new JPanel();
7479
radios.setLayout(new GridLayout(10,1));
@@ -81,19 +86,19 @@ public SudokuView(ActionListener controller)
8186
radioBtn.setText("Empty");
8287
radioBtn.setSelected(true);
8388
}
89+
radioBtn.addActionListener(controller);
8490
radioBtn.setMnemonic(KeyEvent.VK_0 + i);
91+
radioBtn.setActionCommand("Radio " + i);
8592
noBtnGroup.add(radioBtn);
8693
radios.add(radioBtn);
8794
noBtn[i] = radioBtn;
8895
}
8996
add(radios, BorderLayout.EAST);
9097

98+
//****************
99+
// Button to Check
100+
//****************
91101
JPanel buttonsP = new JPanel();
92-
JButton saveBtn = new JButton();
93-
saveBtn.setText("Save");
94-
saveBtn.setActionCommand("Save");
95-
saveBtn.addActionListener(controller);
96-
buttonsP.add(saveBtn);
97102

98103
JButton checkBtn = new JButton();
99104
checkBtn.setText("Check");
@@ -210,6 +215,37 @@ public int getSelectedNumber()
210215
return 0;
211216
}
212217

218+
/**
219+
* Change the color of cell which has the given value
220+
* @param color The color of the cell
221+
* @param val The value to check
222+
*/
223+
public void changeColor(Color color, int val)
224+
{
225+
for (int i = 0; i < 9; i++)
226+
{
227+
for (int j = 0; j < 9; j++)
228+
{
229+
if (grids[i][j].getText().equals(""+val))
230+
grids[i][j].setBackground(color);
231+
}
232+
}
233+
}
234+
235+
/**
236+
* Resets the color of cell to original
237+
*/
238+
public void resetColor()
239+
{
240+
for (int i = 0; i < 9; i++)
241+
{
242+
for (int j = 0; j < 9; j++)
243+
{
244+
grids[i][j].setBackground(Color.white);
245+
}
246+
}
247+
}
248+
213249
private JButton[][] grids;
214250
private JRadioButton[] noBtn;
215251
private ButtonGroup noBtnGroup;

0 commit comments

Comments
 (0)