-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlot.java
96 lines (91 loc) · 2.91 KB
/
Slot.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
import java.util.Observable;
/**
* Slot
* This class handles the state of each slot in the game. Extending observable in preparation for the addition of a GUI.
*
* @author Lauren Scott
* @author Arun Kumar Sekar
*
* @version January 2024
*/
public class Slot extends Observable{
private String state;//The current state of the slot
private int row, col;//The row and column number of the slot
private Boolean fillable;//whether that slot can be changed
/**
* Constructor of the class slot
* This creates the slot and denotes where it is placed in the game board.
*
* @param col - the slot's column number
* @param row - the slot's row number
* @param number - the number that is in that cell
*/
public Slot (int col, int row, String number) {
this.row = row;
this.col = col;
this.state = number;
this.fillable = true;
}
/**
* Constructor of the class slot when importing the level file
* This creates the slot and denotes where it is placed in the game board.
*
* @param col - the slot's column number
* @param row - the slot's row number
* @param number - the number that is in that cell
* @param fillable - whether that cell can be filled by the user, cells in the level file that already have numbers should not be changed.
*/
public Slot (int col, int row, String number, Boolean fillable) {
this.row = row;
this.col = col;
this.state = number;
if (!number.contains("-") ){
this.fillable = false;
} else {
this.fillable = true;
}
}
/**
* setState
* This method sets the current state of the slot
* @param newState - the new state of the slot
*/
public void setState(String newState) {
if (!this.state.equals(newState)) {
this.state = newState;
setChanged(); // Mark the observable as changed
notifyObservers(); // Notify observers
}
}
/**
* getState
* This provides the current state of the slot
* @return the current state of the slot
*/
public String getState(){
return state;
}
/**
* isValidState
* This method checks whether the selected state is valid
* @param state - the state that is being checked
* @return a Boolean value showing whether the state is valid or not
*/
public static boolean isValidState(String state) {
if (!state.contains("-") ){
try {
Integer.parseInt(state);
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* This method checks whether the cell the user is wanting to interact with can be filled
* @return whether it can be filled.
*/
public Boolean getFillable() {
return fillable;
}
}//End of class Slot