More mastermind.

This commit is contained in:
Jacob Signorovitch
2024-11-09 22:55:32 -05:00
parent de02377d84
commit 92523e3bde

View File

@@ -100,17 +100,20 @@ class Examples {
} }
boolean testDrawMethods(Tester t) { boolean testDrawMethods(Tester t) {
WorldCanvas c = new WorldCanvas(1000, 1000);
WorldScene s = new WorldScene(1000, 1000);
// WorldImage incomplete = new IncompleteGuess(exampleDotsOne).draw(); // WorldImage incomplete = new IncompleteGuess(exampleDotsOne).draw();
//WorldImage guesses = new ConsGuess(new Guess(dotsOne, new Feedback(1, 2)), new ConsGuess(new Guess(dotsTwo, new Feedback(2, 5)), new MtGuess())).draw();
Game game = new Game().addGuess(new Guess(dotsOne, exampleFeedback));
WorldImage guesses = new ConsGuess(new Guess(dotsOne, new Feedback(1, 2)), new ConsGuess(new Guess(dotsTwo, new Feedback(2, 5)), new MtGuess())).draw(); WorldImage gameImg = game.draw();
int w = (int) gameImg.getWidth();
int h = (int) gameImg.getHeight();
WorldCanvas c = new WorldCanvas(w, h);
WorldScene s = new WorldScene(w, h);
return return
c.drawScene(s.placeImageXY(guesses, (int)guesses.getWidth()/2, 100)) c.drawScene(s.placeImageXY(gameImg, w/2, h/2))
//c.drawScene(s.placeImageXY(incomplete, (int) (incomplete.getWidth()/2), 100)) //c.drawScene(s.placeImageXY(incomplete, (int) (incomplete.getWidth()/2), 100))
//c.drawScene(s.placeImageXY(exampleFeedback.draw(), 100, 100)) //c.drawScene(s.placeImageXY(exampleFeedback.draw(), 100, 100))
//c.drawScene(s.placeImageXY(redDot.draw(), 100, 100)) //c.drawScene(s.placeImageXY(redDot.draw(), 100, 100))
@@ -147,22 +150,58 @@ class Game {
static ILoDot DEFAULTDOTS = new ConsDot(new Dot(Color.RED), new ConsDot(new Dot(Color.GREEN), new ConsDot(new Dot(Color.BLUE), new MtDot()))); static ILoDot DEFAULTDOTS = new ConsDot(new Dot(Color.RED), new ConsDot(new Dot(Color.GREEN), new ConsDot(new Dot(Color.BLUE), new MtDot())));
static GameConf DEFAULTCONF = new GameConf(true, 5, 5, DEFAULTDOTS); static GameConf DEFAULTCONF = new GameConf(true, 5, 5, DEFAULTDOTS);
GameConf conf; GameConf conf; // The game's configuration.
int guessesLeft; ILoDot solution; // The solution to the game.
ILoGuess guesses; int guessesLeft; // The number of guesses the player has left.
ILoGuess guesses; // The guesses the player has taken.
boolean done; // Is the game over?
boolean won; // Did they player win?
Game(GameConf conf, int guessesLeft, ILoGuess guesses) { Game(GameConf conf, ILoDot solution, int guessesLeft, ILoGuess guesses, boolean won, boolean done) {
if (this.won && !this.done) throw new IllegalArgumentException("Can't win before you've finished playing.");
this.conf = conf; this.conf = conf;
this.solution = solution;
this.won = won;
this.done = done;
this.guessesLeft = guessesLeft; this.guessesLeft = guessesLeft;
this.guesses = guesses; this.guesses = guesses;
} }
// Convenience constructor using default config. // Convenience constructor using default config and starting values.
Game() { this(DEFAULTCONF, DEFAULTCONF.nguesses, new MtGuess()); } Game() { this(DEFAULTCONF, DEFAULTCONF.options.gen(DEFAULTCONF.len), DEFAULTCONF.nguesses, new MtGuess(), false, false); }
// Draw the current game state.
WorldImage draw() { WorldImage draw() {
return new EmptyImage(); return new AboveAlignImage(AlignModeX.LEFT, this.draw_sol(), this.draw_guesses(), this.draw_options());
} }
// Draw the solution.
WorldImage draw_sol() {
if (this.done) return this.draw_sol_rev();
else return this.draw_sol_hid();
}
// Draw the revealed answer.
WorldImage draw_sol_rev() {
return this.solution.draw();
}
// Draw the hidden answer.
WorldImage draw_sol_hid() {
return new RectangleImage(this.solution.getW(), 2*Dot.r, OutlineMode.SOLID, Color.BLACK);
}
WorldImage draw_guesses() {
return this.guesses.draw();
}
WorldImage draw_options() {
return this.conf.options.draw();
}
// Convenience methods for testing -- not part of program.
Game win() { return new Game(this.conf, this.solution, this.guessesLeft, this.guesses, true, true); }
Game addGuess(Guess guess) { return new Game(this.conf, this.solution, this.guessesLeft, new ConsGuess(guess, this.guesses), this.won, this.done); }
} }
// A game configuration. // A game configuration.