-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMasterMind.java
More file actions
93 lines (69 loc) · 2.87 KB
/
MasterMind.java
File metadata and controls
93 lines (69 loc) · 2.87 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
/* Develop a program to mimic the master mind game
step 1: create a class that is a set of unique objects(coloreMarbles)
step 2: Method of the class should implement a
mathematical abstraction of a set and support the following operations
(i) inquiring the number of unique colored in the set, (ii) check that the set is empty,
(iii) add element to the set, (iv) check if a input object is in the set (v) clear the set*/
public class MasterMind{
static void status(ColorBoxClass color_box) {
System.out.println("Size of the color box is : " + color_box.size());
System.out.println("Is empty? " + color_box.isEmpty());
System.out.println("Is RED there? " + color_box.isPresent(ColoredMarbleEnum.RED));
System.out.println("Is GREEN there? " + color_box.isPresent(ColoredMarbleEnum.GREEN));
}
public static void main (String[] args){
// color_box of class ColorBoxClass is created below:
ColorBoxClass color_box = new ColorBoxClass();
status(color_box);
System.out.println("Adding color RED");
color_box.add(ColoredMarbleEnum.RED);
status(color_box);
System.out.println("Adding color RED");
color_box.add(ColoredMarbleEnum.RED);
status(color_box);
System.out.println("Adding color GREEN");
color_box.add(ColoredMarbleEnum.GREEN);
status(color_box);
System.out.println("Clearing the set");
color_box.clear();
status(color_box);
}
}
/// The class representing a colored marble.
enum ColoredMarbleEnum {
RED, GREEN, BLUE, YELLOW, BLACK, WHITE
};
/// The class contains a set of marbles of unique colors.
class ColorBoxClass{
// maximum 8 marbles will fit here, but as only unique marbles are stored,
// and there are only 6 different colors, 8 is enough.
ColoredMarbleEnum[] box = new ColoredMarbleEnum[8];
int nMarbles = 0; // number of marbles in the box is initially 0
/// @Returns the number of unique marbles in the box
public int size(){
return nMarbles;
};
public boolean isEmpty(){
// if nMarbles is 0, then the box is empty
return (nMarbles==0);
};
//for the add method we are passing an argument: a reference to a marble object that is a member of ColoredMarbleEnum
public void add(ColoredMarbleEnum marble){
// we accept only unique colored marbles.
if (isPresent(marble)) return;
// every time a marble is added, nMarbles is updated.
box[nMarbles]=marble;
nMarbles++;
};
// check whether a marble of this color is present in the box
public boolean isPresent(ColoredMarbleEnum marble) {
for (int i=0; i<nMarbles; ++i) {
if (box[i]==marble) return true;
}
return false;
};
// Empty the box
public void clear() {
nMarbles=0;
}
}