-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.java
112 lines (90 loc) · 2.66 KB
/
Cell.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
/*
File: Board.java
Author: Mahdeen Ahmed Khan Sameer
Date: 03/16/2023
CS231B
Creates a single cell in a Sudoku game board
*/
import java.awt.Graphics;
import java.awt.Color;
public class Cell {
private int row;
private int col;
private int value;
private boolean locked;
public Cell() {
this.row = 0;
this.col = 0;
this.value = 0;
this.locked = false;
}
public Cell(int row, int col, int value) {
this.row = row;
this.col = col;
this.value = value;
this.locked = false;
}
public Cell(int row, int col, int value, boolean locked) {
this.row = row;
this.col = col;
this.value = value;
this.locked = locked;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getValue() {
return value;
}
public void setValue(int newval) {
this.value = newval;
}
public boolean isLocked() {
return locked;
}
public void setLocked(boolean lock) {
this.locked = lock;
}
public String toString() {
return Integer.toString(value);
}
public void draw(Graphics g, int x, int y, int scale) {
char toDraw = (char) ((int) '0' + getValue());
g.setColor(isLocked() ? Color.BLUE : Color.RED);
g.drawChars(new char[] { toDraw }, 0, 1, x, y);
}
public static void main(String[] args) {
Cell cell1 = new Cell();
Cell cell2 = new Cell(2, 3, 5);
Cell cell3 = new Cell(1, 1, 3, true);
// Test default constructor
assert cell1.getRow() == 0;
assert cell1.getCol() == 0;
assert cell1.getValue() == 0;
assert !cell1.isLocked();
// Test constructor with row, col, and value
assert cell2.getRow() == 2;
assert cell2.getCol() == 3;
assert cell2.getValue() == 5;
assert !cell2.isLocked();
// Test constructor with row, col, value, and locked
assert cell3.getRow() == 1;
assert cell3.getCol() == 1;
assert cell3.getValue() == 3;
assert cell3.isLocked();
// Test setting and getting value
cell1.setValue(7);
assert cell1.getValue() == 7;
// Test setting and getting locked status
cell2.setLocked(true);
assert cell2.isLocked();
// Test toString method
assert cell1.toString().equals("7");
assert cell2.toString().equals("5");
assert cell3.toString().equals("3");
System.out.println("All tests passed!");
}
}