====== Lab 6 - Hangman ====== In this lab you will create a GUI version of the hangman game, something like the screenshots in [[http://www.cs.bilkent.edu.tr/~bgedik/courses/cs102/2012-fall/SimpleHangmanView.jpg|figures 1]] & [[http://www.cs.bilkent.edu.tr/~bgedik/courses/cs102/2012-fall/GallowsHangmanView.jpg|2]]. Your solution will use the MVC (Model-View-Controller) design pattern. This pattern separates the core logic of the game (the model), from the way in which the user interacts with it -- how the user views and controls it. There are literally hundreds of variations of this basic theme. This lab introduces you to one that is (hopefully) easy to understand, yet principled and extensible. This should allow you to write a simple program to begin with and later, with minimum changes/effort, adapt it to much more sophisticated situations. The basic MVC pattern allows for multiple controls to update a model, which then tells its views to update themselves to reflect the model's new state. In this assignment you are given the model (the core logic) for the hangman game. Its API is shown in [[http://www.cs.bilkent.edu.tr/~bgedik/courses/cs102/2012-fall/cs102_HangmanClasses.png|figure 3]]. The source code is not available, only the Java class file, which means you cannot modify it (a common situation in computer engineering). Download this [[http://www.cs.bilkent.edu.tr/~bgedik/courses/cs102/2012-fall/Hangman.zip|JCreator Project]] to start work (note: In JCreator, you need to go to the project settings and add a required library. Name the library as cs102_Hangman and provide the path to the Hangman directory that contains cs102 as a sub-directory. Without this step, your code may not compile). The first task is to associate a view with the model and have the model tell the view to update itself whenever it is changed. You already know the advantages of Java interfaces, so rather than fixing the view type, you define an interface, ''IHangmanView'', which has a single method ''updateView(Hangman)'', whose parameter allows the view to access the Hangman model, if necessary. You are given this interface, so all you need to do is to subclass ''cs102.Hangman''; call this new class ''HangmanModel''. It should have a ''IHangmanView'' property, and provide a method ''addView(IHangmanView)'' that sets the view, plus another method, ''update()'', that calls the view's ''updateView()'' method. Finally, it should override any methods that affect the model's state, such that they not only perform as before, but afterwards tell the view to update itself. In the case of the Hangman game, only the ''tryThis()'' & ''initNewGame()'' methods change its state and so need overriding. Note: It is a good idea to test the pieces of your program as you build them (so called unit testing). At this point you might like to test the code so far by writing a class that implements the ''IHangmanView'' interface and simply prints the Hangman game's state on the Java console. Have a main method that creates an instance of ''HangmanModel'', tries various letters and initializes new games. Each call to one of these methods should cause your text view to automatically print the new state of the game. The next step is to build the GUI. You are given the basic user-interface, ''HangmanUI''. Take a look at the code. It includes two controls (the ''LetterButtonsControls'' and the ''newGame'' button) and has properties, ''IHangmanView'' and ''HangmanModel''. Notice how the UI controls call the ''tryThis()'' and ''initNewGame()'' methods in the ''HangmanUI'' class. These call the corresponding methods in the ''HangmanModel''. It would have been possible for the controls to call the model's methods directly, but this indirection is often useful, especially if multiple controls perform the same task and/or if the controls require UI changes too (as is the case here)! You have already created the ''HangmanModel'' class, so the next step is to build a Swing version of ''IHangmanView'' that can be used in a GUI. As usual, it probably makes sense to start by subclassing ''JPanel''. See if you can build a ''SimpleHangmanView'', as shown in [[http://www.cs.bilkent.edu.tr/~bgedik/courses/cs102/2012-fall/SimpleHangmanView.jpg|figure 1]], and include it in the HangmanUI. Having done that, build another fancier view, ''GallowsHangmanView'' (similar to [[http://www.cs.bilkent.edu.tr/~bgedik/courses/cs102/2012-fall/GallowsHangmanView.jpg|figure 2]]), that provides a more conventional view of the hanging man. You should be able to demonstrate your program using either of these views. Finally, do any of the remaining wiring-up, for example, having the ''LetterButtonControls'' & ''NewGame'' button be enabled & disabled at the appropriate times.