-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.java
More file actions
140 lines (128 loc) · 4.19 KB
/
Copy pathCode.java
File metadata and controls
140 lines (128 loc) · 4.19 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
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
package types;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a code sequence in the Mastermind game.
* A code consists of a list of colours and provides functionality
* to compare codes and determine correct positions and colours.
* Implements Cloneable interface.
*
* @author pco10
*/
public class Code implements Cloneable {
private final List<Colour> code;
/**
* Constructor for a Code/
*
* @param code - list of colours that make up the code
*
* @requires - code is a list Colours
*/
public Code(List<? extends Colour> code) {
this.code = new ArrayList<>(code);
}
/**
* Returns a copy of the code sequence.
*
* @return - new list containing the colours in this code
*/
public List<Colour> getCode() {
return new ArrayList<>(code);
}
/**
* Returns the length of the code sequence.
*
* @return - the number of colours in the code
*/
public int getLength() {
return code.size();
}
/**
* Compares this code with another code and returns the number of
* exact matches and colour matches in wrong positions.
*
* @param other - the code to compare with
* @return result - an array where the first element is the number of exact matches
* and the second element is the number of colour matches in wrong positions
* @requires other - is a Code entity
*/
public int[] howManyCorrect(Code other) {
int[] result = new int[2];
List<Colour> thisCopy = new ArrayList<>(this.code);
List<Colour> otherCopy = new ArrayList<>(other.code);
// count exact matches (right colour in right position)
for (int i = 0; i < thisCopy.size(); i++) {
if (thisCopy.get(i).toString().equals(otherCopy.get(i).toString())) {
result[0]++;
thisCopy.set(i, null);
otherCopy.set(i, null);
}
}
// count colour matches in wrong positions
for (int i = 0; i < thisCopy.size(); i++) {
if (thisCopy.get(i) != null) {
for (int j = 0; j < otherCopy.size(); j++) {
if (otherCopy.get(j) != null &&
thisCopy.get(i).toString().equals(otherCopy.get(j).toString())) {
result[1]++;
otherCopy.set(j, null);
break;
}
}
}
}
return result;
}
/**
* Returns a string representation of the code.
*
* @return - a string in the format [colour1, colour2, ...]
*/
@Override
public String toString() {
StringBuilder res = new StringBuilder("[");
for (int i = 0; i < code.size(); i++) {
res.append(code.get(i).toString());
if (i < code.size() - 1) {
res.append(", ");
}
}
res.append("]");
return res.toString();
}
/**
* Creates and returns a copy of this code.
* Overrides Cloneable clone method,
* gives an error, if clone is not supported.
*
* @return - a clone of this code
*/
@Override
public Code clone() {
try {
Code code1 = (Code) super.clone(); // call the superclass clone method
return new Code(this.code); // return a new Code instance initialized with the current object's data
} catch (CloneNotSupportedException e) {
throw new AssertionError("Cloning not supported.", e);
}
}
/**
* Checks if this code equals another object.
*
* @param obj - the object to compare with
* @return - true if the codes are equal, false otherwise
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Code other = (Code) obj;
if (code.size() != other.code.size()) return false;
for (int i = 0; i < code.size(); i++) {
if (!code.get(i).toString().equals(other.code.get(i).toString())) {
return false;
}
}
return true;
}
}