patternjavaMinor
Mastermind code taking hours to calculate
Viewed 0 times
hoursmastermindcalculatecodetaking
Problem
I'm trying to implement Knuth's Five-guess Mastermind algorithm in my own version of Mastermind, but when running step 6 it takes my code hours to actually run through everything to get the neccesary information. I was wondering if any of you would know a better way to obtain the same results?
Here is my code with relevant classes, step 6 is mainly executed in the method
The following code is the class in which all of the calculation is being done (it exists mainly to do this, except for a few functions which are not called
```
import java.util.ArrayList;
import java.util.Collections;
public class PhaseComputer extends Phase {
private ArrayList possibleCombList = new ArrayList();
private ArrayList combList = new ArrayList();
private ArrayList possibleOutcomes = new ArrayList();
private String combination = "";
private String code = "";
private int totalGuesses = 0;
public PhaseComputer() {
setPossibleCombList();
setOutComeList();
this.guess = new ArrayList<>();
this.exactlyRight = new ArrayList<>();
this.wrongPlace = new ArrayList<>();
}
public void play() {
generateCode();
blackPins = 0;
whitePins = 0;
System.out.println("Guess " + (totalGuesses + 1) + ": " + code + "\n");
checkBlackPins();
if (blackPins == 4) {
this.win = true;
} else {
checkWhitePins();
}
exactlyRight.add(blackPins);
wrongPlace.add(whitePins);
guess.add(code);
}
public void generateCode() { // Generates the next guess.
if (totalGuesses == 0) { // If this is the first turn, put the next guess equal to 'AABB'.
code = "AABB";
}
else if (possibleCombList.size() == 1) { // If there's only one possible combination left;
code = possibleCombList.get(0);
}
else { // If none of the above are true;
Here is my code with relevant classes, step 6 is mainly executed in the method
calculateScore().The following code is the class in which all of the calculation is being done (it exists mainly to do this, except for a few functions which are not called
```
import java.util.ArrayList;
import java.util.Collections;
public class PhaseComputer extends Phase {
private ArrayList possibleCombList = new ArrayList();
private ArrayList combList = new ArrayList();
private ArrayList possibleOutcomes = new ArrayList();
private String combination = "";
private String code = "";
private int totalGuesses = 0;
public PhaseComputer() {
setPossibleCombList();
setOutComeList();
this.guess = new ArrayList<>();
this.exactlyRight = new ArrayList<>();
this.wrongPlace = new ArrayList<>();
}
public void play() {
generateCode();
blackPins = 0;
whitePins = 0;
System.out.println("Guess " + (totalGuesses + 1) + ": " + code + "\n");
checkBlackPins();
if (blackPins == 4) {
this.win = true;
} else {
checkWhitePins();
}
exactlyRight.add(blackPins);
wrongPlace.add(whitePins);
guess.add(code);
}
public void generateCode() { // Generates the next guess.
if (totalGuesses == 0) { // If this is the first turn, put the next guess equal to 'AABB'.
code = "AABB";
}
else if (possibleCombList.size() == 1) { // If there's only one possible combination left;
code = possibleCombList.get(0);
}
else { // If none of the above are true;
Solution
Since you said that Step 6 takes a lot of time, and is mainly executed in the
Here, you calculate
It can be faster if you have a variable that holds the minimum of
calculateScore() method, why don't we simplify it?public int calculateScore(String solution) { // Calculate the score of 'solution' on every available combination of pins.
ArrayList minimum = new ArrayList();
for (Outcome outcome : possibleOutcomes) {
int min = 0;
for (String combination : combList) {
if (!checkIfPossible(solution, combination, outcome)) {
min++;
}
}
minimum.add(min);
}
return Collections.min(minimum);
}Here, you calculate
min, add it to a List, and then return the minimum in the List.It can be faster if you have a variable that holds the minimum of
mins:public int calculateScore(String solution) {
int minimum = Integer.MAX_VALUE;
for (Outcome outcome : possibleOutcomes) {
int min = 0;
for (String combination : combList) {
if (!checkIfPossible(solution, combination, outcome)) {
min++;
}
}
if(minimum > min) {
minimum = min;
}
}
return min;
}Code Snippets
public int calculateScore(String solution) { // Calculate the score of 'solution' on every available combination of pins.
ArrayList<Integer> minimum = new ArrayList<Integer>();
for (Outcome outcome : possibleOutcomes) {
int min = 0;
for (String combination : combList) {
if (!checkIfPossible(solution, combination, outcome)) {
min++;
}
}
minimum.add(min);
}
return Collections.min(minimum);
}public int calculateScore(String solution) {
int minimum = Integer.MAX_VALUE;
for (Outcome outcome : possibleOutcomes) {
int min = 0;
for (String combination : combList) {
if (!checkIfPossible(solution, combination, outcome)) {
min++;
}
}
if(minimum > min) {
minimum = min;
}
}
return min;
}Context
StackExchange Code Review Q#77954, answer score: 3
Revisions (0)
No revisions yet.