diff --git a/generics/src/generics/Main.java b/generics/src/generics/Main.java index 3300ad6..18dc67c 100644 --- a/generics/src/generics/Main.java +++ b/generics/src/generics/Main.java @@ -174,8 +174,6 @@ interface ILo { // Find the largest from the evaluator. A argmax(IEvaluator eval); A argmaxHelper(IEvaluator eval, A prev, Integer prevVal); - // Skip this element of the list. - ILo skip(); // Sort the list with the comparator. ILo sort(IComparator comp); ILo sortHelper(IComparator comp, ILo sorted); @@ -207,11 +205,7 @@ class Cons implements ILo { return prevVal >= nowVal ? this.rest.argmaxHelper(eval, prev, prevVal) - : this.rest.skip().argmaxHelper(eval, now, nowVal); - } - - public ILo skip() { - return this.rest; + : this.rest.argmaxHelper(eval, now, nowVal); } public ILo sort(IComparator comp) { @@ -246,10 +240,6 @@ class Mt implements ILo { return prev; } - public ILo skip() { - return this; - } - public ILo sort(IComparator comp) { return this; } diff --git a/mastermind/src/mastermind/Main.java b/mastermind/src/mastermind/Main.java index 78995d4..8f25dd9 100644 --- a/mastermind/src/mastermind/Main.java +++ b/mastermind/src/mastermind/Main.java @@ -67,7 +67,34 @@ class Examples { Feedback exampleFeedback = new Feedback(2, 2); - Game exampleGame = new Game(); + Game exampleGame = new Game( + new GameConf( + true, + 6, + 4, + new ConsDot( + redDot, + new ConsDot(greenDot, new ConsDot(blueDot, new MtDot())) + ) + ), + new ConsDot( + redDot, + new ConsDot( + greenDot, + new ConsDot( + blueDot, + new ConsDot( + greenDot, + new ConsDot(redDot, new ConsDot(greenDot, new MtDot())) + ) + ) + ) + ), + 4, + ConsPlaceholderGuess.mkN(4), + false, + false + ); boolean testDrawMethods(Tester t) { // WorldImage incomplete = new IncompleteGuess(exampleDotsOne).draw(); @@ -364,9 +391,8 @@ class Game extends World { if (choice <= this.conf.options.len()) return this.addDot( choice - 1 ); - } else if (key.equals("backspace")) { - return this.dropDot(); - } + } else if (key.equals("backspace")) return this.dropDot(); + else if (key.equals("enter")) return this.commitGuess(); return this; } @@ -397,6 +423,17 @@ class Game extends World { ); } + Game commitGuess() { + return new Game( + this.conf, + this.solution, + this.guessesLeft - 1, + this.guesses.commitIncomplete(this.conf.len, this.solution), + this.won, + this.done + ); + } + // Calculate the width of the window without drawing anything. int calcW() { return ( @@ -411,7 +448,7 @@ class Game extends World { int calcH() { return ( ((this.conf.nguesses + 2) * Dot.r * 2) + - ((this.conf.nguesses) * Util.gapW) + ((this.conf.nguesses) * (Util.gapW + 2)) ); } @@ -427,8 +464,8 @@ class Game extends World { // Draw the solution. WorldImage draw_sol() { - if (this.done) return this.draw_sol_rev(); - else return this.draw_sol_hid(); + /*if (this.done)*/return this.draw_sol_rev(); + /*else return this.draw_sol_hid();*/ } // Draw the revealed answer. @@ -490,6 +527,12 @@ interface ILoGuess { WorldImage draw(); ILoGuess addToIncomplete(Dot dot, int limit); ILoGuess dropFromIncomplete(); + ILoGuess commitIncomplete(int limit, ILoDot solution); + ILoGuess commitIncompleteHelper( + int limit, + ILoDot solution, + boolean premoved + ); } class ConsGuess implements ILoGuess { @@ -513,6 +556,21 @@ class ConsGuess implements ILoGuess { public ILoGuess dropFromIncomplete() { return new ConsGuess(this.guess, this.nxt.dropFromIncomplete()); } + + public ILoGuess commitIncomplete(int limit, ILoDot solution) { + return this.commitIncompleteHelper(limit, solution, false); + } + + public ILoGuess commitIncompleteHelper( + int limit, + ILoDot solution, + boolean premoved + ) { + return new ConsGuess( + this.guess, + this.nxt.commitIncompleteHelper(limit, solution, premoved) + ); + } } class ConsIncompleteGuess implements ILoGuess { @@ -542,6 +600,26 @@ class ConsIncompleteGuess implements ILoGuess { this.nxt ); } + + public ILoGuess commitIncomplete(int limit, ILoDot solution) { + return this.commitIncompleteHelper(limit, solution, false); + } + + public ILoGuess commitIncompleteHelper( + int limit, + ILoDot solution, + boolean premoved + ) { + if (!premoved) throw new Error("Here"); + if (this.guessSoFar.full(limit)) { + ILoDot guessDots = this.guessSoFar.guessSoFar; + Feedback feedback = guessDots.compare(solution); + return new ConsIncompleteGuess( + new IncompleteGuess(new MtDot()), + new ConsGuess(new Guess(guessDots, feedback), this.nxt) + ); + } else return this; + } } // Placeholder for guesses yet to be guessed. @@ -562,7 +640,7 @@ class ConsPlaceholderGuess implements ILoGuess { // Fill a list with n placeholders. static ILoGuess mkN(int n) { - if (n == 0) return new ConsIncompleteGuess( + if (n == 1) return new ConsIncompleteGuess( new IncompleteGuess(new MtDot()), new MtGuess() ); @@ -576,6 +654,25 @@ class ConsPlaceholderGuess implements ILoGuess { public ILoGuess dropFromIncomplete() { return new ConsPlaceholderGuess(this.nxt.dropFromIncomplete()); } + + public ILoGuess commitIncompleteHelper( + int limit, + ILoDot solution, + boolean premoved + ) { + if (!premoved) return this.nxt.commitIncompleteHelper( + limit, + solution, + true + ); + else return new ConsPlaceholderGuess( + this.nxt.commitIncomplete(limit, solution) + ); + } + + public ILoGuess commitIncomplete(int limit, ILoDot solution) { + return this.commitIncompleteHelper(limit, solution, false); + } } class MtGuess implements ILoGuess { @@ -591,6 +688,18 @@ class MtGuess implements ILoGuess { public ILoGuess dropFromIncomplete() { return this; } + + public ILoGuess commitIncomplete(int limit, ILoDot solution) { + return this; + } + + public ILoGuess commitIncompleteHelper( + int limit, + ILoDot solution, + boolean premoved + ) { + return this; + } } // A guess. @@ -636,6 +745,11 @@ class IncompleteGuess { if (len == 0) return this; else return new IncompleteGuess(this.guessSoFar.remove(len - 1)); } + + // Is the guess full? + boolean full(int limit) { + return this.guessSoFar.len() == limit; + } } // Feedback for a guess.