diff --git a/mastermind/src/mastermind/Main.java b/mastermind/src/mastermind/Main.java index be9ff5f..bda310b 100644 --- a/mastermind/src/mastermind/Main.java +++ b/mastermind/src/mastermind/Main.java @@ -100,17 +100,20 @@ class Examples { } boolean testDrawMethods(Tester t) { - WorldCanvas c = new WorldCanvas(1000, 1000); - WorldScene s = new WorldScene(1000, 1000); + // 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 gameImg = game.draw(); + int w = (int) gameImg.getWidth(); + int h = (int) gameImg.getHeight(); - - // 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(); + WorldCanvas c = new WorldCanvas(w, h); + WorldScene s = new WorldScene(w, h); 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(exampleFeedback.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 GameConf DEFAULTCONF = new GameConf(true, 5, 5, DEFAULTDOTS); - GameConf conf; - int guessesLeft; - ILoGuess guesses; + GameConf conf; // The game's configuration. + ILoDot solution; // The solution to the game. + 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.solution = solution; + this.won = won; + this.done = done; this.guessesLeft = guessesLeft; this.guesses = guesses; } - // Convenience constructor using default config. - Game() { this(DEFAULTCONF, DEFAULTCONF.nguesses, new MtGuess()); } + // Convenience constructor using default config and starting values. + Game() { this(DEFAULTCONF, DEFAULTCONF.options.gen(DEFAULTCONF.len), DEFAULTCONF.nguesses, new MtGuess(), false, false); } + // Draw the current game state. 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.