Updated Mastermind.

This commit is contained in:
Jacob Signorovitch
2024-11-21 18:00:03 -05:00
parent ebe930f390
commit 4e474e0598

View File

@@ -361,16 +361,17 @@ class Game extends World {
public Game onKeyEvent(String key) { public Game onKeyEvent(String key) {
if ("123456789".contains(key)) { // User has entered a number. if ("123456789".contains(key)) { // User has entered a number.
int choice = Integer.valueOf(key); int choice = Integer.valueOf(key);
return this.addDot(choice - 1); if (choice <= this.conf.options.len()) return this.addDot(
//} else if (key.equals("backspace")) { choice - 1
//return this.dropDot(); );
} else if (key.equals("backspace")) {
return this.dropDot();
} }
return this; return this;
} }
// Attempt to add the nth dot option to the incomplete guess. // Attempt to add the nth dot option to the incomplete guess.
Game addDot(int choice) { Game addDot(int choice) {
System.out.println("tying to ");
return new Game( return new Game(
this.conf, this.conf,
this.solution, this.solution,
@@ -384,8 +385,17 @@ class Game extends World {
); );
} }
// Attempt to remove the last dot from the incomplete guess. // Attempt to remove the last dot in the incomplete guess.
//Game dropDot() {} Game dropDot() {
return new Game(
this.conf,
this.solution,
this.guessesLeft,
this.guesses.dropFromIncomplete(),
this.won,
this.done
);
}
// Calculate the width of the window without drawing anything. // Calculate the width of the window without drawing anything.
int calcW() { int calcW() {
@@ -479,6 +489,7 @@ class GameConf {
interface ILoGuess { interface ILoGuess {
WorldImage draw(); WorldImage draw();
ILoGuess addToIncomplete(Dot dot, int limit); ILoGuess addToIncomplete(Dot dot, int limit);
ILoGuess dropFromIncomplete();
} }
class ConsGuess implements ILoGuess { class ConsGuess implements ILoGuess {
@@ -496,7 +507,11 @@ class ConsGuess implements ILoGuess {
} }
public ILoGuess addToIncomplete(Dot dot, int limit) { public ILoGuess addToIncomplete(Dot dot, int limit) {
return this.nxt.addToIncomplete(dot, limit); return new ConsGuess(this.guess, this.nxt.addToIncomplete(dot, limit));
}
public ILoGuess dropFromIncomplete() {
return new ConsGuess(this.guess, this.nxt.dropFromIncomplete());
} }
} }
@@ -515,15 +530,17 @@ class ConsIncompleteGuess implements ILoGuess {
} }
public ILoGuess addToIncomplete(Dot dot, int limit) { public ILoGuess addToIncomplete(Dot dot, int limit) {
System.out.println("Here");
return new ConsIncompleteGuess( return new ConsIncompleteGuess(
new IncompleteGuess(new ConsDot(dot, new MtDot())), this.guessSoFar.tryAppend(dot, limit),
this.nxt
);
}
public ILoGuess dropFromIncomplete() {
return new ConsIncompleteGuess(
this.guessSoFar.tryRemoveLast(),
this.nxt this.nxt
); );
// return new ConsIncompleteGuess(
// this.guessSoFar.tryAppend(dot, limit),
// this.nxt
// );
} }
} }
@@ -555,6 +572,10 @@ class ConsPlaceholderGuess implements ILoGuess {
public ILoGuess addToIncomplete(Dot dot, int limit) { public ILoGuess addToIncomplete(Dot dot, int limit) {
return new ConsPlaceholderGuess(this.nxt.addToIncomplete(dot, limit)); return new ConsPlaceholderGuess(this.nxt.addToIncomplete(dot, limit));
} }
public ILoGuess dropFromIncomplete() {
return new ConsPlaceholderGuess(this.nxt.dropFromIncomplete());
}
} }
class MtGuess implements ILoGuess { class MtGuess implements ILoGuess {
@@ -566,6 +587,10 @@ class MtGuess implements ILoGuess {
public ILoGuess addToIncomplete(Dot dot, int limit) { public ILoGuess addToIncomplete(Dot dot, int limit) {
return this; return this;
} }
public ILoGuess dropFromIncomplete() {
return this;
}
} }
// A guess. // A guess.
@@ -604,6 +629,13 @@ class IncompleteGuess {
); );
else return this; else return this;
} }
//Remove the last dot.
IncompleteGuess tryRemoveLast() {
int len = this.guessSoFar.len();
if (len == 0) return this;
else return new IncompleteGuess(this.guessSoFar.remove(len - 1));
}
} }
// Feedback for a guess. // Feedback for a guess.