I am having trouble completing This hangman project. It mmost be done using model view controller pattern.
Here is what i have so far

//Header file declarations
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
//Class declaration
class HangmanModel extends JFrame implements ActionListener {
public int attempts = 13;
private JTextField _tfWordIO = new JTextField();
private JTextField _tfGuessChar = new JTextField();
private JButton _resetBtn = new JButton("Restart");
private JLabel _lblResponse = new JLabel();
public int errors;
int wordlength;
boolean solved;
String secret;
//read in secret string
//String displaySecret;
//generate as many "*"s as secret is long and store them in displaySecret
//String Wordcollection[] = new String[] {"plasma", "chicken", "mouse"}; //Array of words

// Constructor
public void Hangman( )
add(new HangmanModel());

//main function
public static void main(String[] args) throws IOException{

//the fileScan gets the first word for the game
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
String secretWord = fileScan.next();

//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(int i = 0; i <= secretWord.length(); i++)

System.out.println("Welcome to the game of HANGMAN!!!!");
System.out.println("You will have 13 chances to guess what the word is.");

HangmanModel g = new HangmanModel();

//Set the title
g.setTitle("Hangman Game");

//Set the width and height
g.setSize(300, 400);

//Set close operation

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub


import java.awt.Graphics;

public class HangmanView extends HangmanModel {

public void paint(Graphics g) {

// Draw the hangman
int baseY = 250;

if (errors > 0) { // ground
g.drawLine(90, baseY, 200, baseY);
if (errors > 1) { // bar up
g.drawLine(125, baseY, 125, baseY - 100);
if (errors > 2) {
g.drawLine(110, baseY, 125, baseY - 15);
if (errors > 3) {
g.drawLine(140, baseY, 125, baseY - 15);
if (errors > 4) { // side bar
g.drawLine(125, baseY - 100, 175, baseY - 100);
if (errors > 5) {
g.drawLine(125, baseY - 85, 140, baseY - 100);
if (errors > 6) { // rope
g.drawLine(175, baseY - 100, 175, baseY - 75);
if (errors > 7) { // body
g.drawOval(170, baseY - 75, 10, 12);
if (errors > 8) {
g.drawOval(170, baseY - 65, 15, 25);
if (errors > 9) { // arms
g.drawLine(160, baseY - 65, 170, baseY - 60);
if (errors > 10) {
g.drawLine(183, baseY - 60, 193, baseY - 65);
if (errors > 11) { // legs
g.drawLine(165, baseY - 30, 170, baseY - 45);
if (errors > 12) {
g.drawLine(183, baseY - 45, 193, baseY - 30);



public class HangmanController {


Edited by Nick Evan: Fixed formatting

5 Years
Discussion Span
Last Post by stultuske

you forgot to mention what problems you are having.
if it's as you posted it above: it's badly indented and hardly readable


I am having trouble finishing it. And yes i am aware that its badly indented and hardly readable, thats why I posted it on here because i need help. Doing this using the model view controller pattern is diffucult for me.


obviously, you're also very bad in describing what your problem is
"finishing it" is a very wide concept.
are you getting exceptions when compiling/running the code and don't know how to get rid of them, have you reached a part where you don't know how to go on, do you not understand the logic you should write, don't you know the syntaxis or classes to use, ...


you are right that was very vague. i have reached a point where i do not know how to go on and i do not understand the logic i shuld write next.


what is the point you reached, what is the point you're trying to reach and how do you think you should get there.

do you think you can write it out in pseudo code? that might help you out a lot.


Do you have a design for the program? A list of things it needs to do.
What items have you completed and tested and now having working?
What items on the list remain to be done?


ok right now i am at the point where im having trouble getting a random word from the textfile and inserting it as a secretword in the game like this (*****) so the user can try and guess the word


No i dont have a design or a list of things to do. The instructions for the assignment were very vague. It basically says complete a hangman game using the model view controller pattern


No i dont have a design or a list of things to do

That is one of the first things you should do before starting to write your code.
Think though the problem and make the list of steps you need to take to solve it.

trouble getting a random word from the textfile

There are two steps here:
1)Read all of the file into an ArrayList
2)and pick the word randomly from the list.

Edited by NormR1: n/a


Im sorry i do have some what of a design. In the model class i need to put the textfile containing the words, set up the guesses, the layout of the frame and the buttons. the View class needs to draw the lines if the user guesses the wrong letters. The Controller class checks the guesses, makes sure words arent used twice, makes sure the word is complete, restart and quit the game.


well, not really...
you don't put a textfile in a class, you can open it in one, but ...
the View is your GUI

you might look at it like:
Model: your data objects
View: your GUI, the entire visual aspect
Controller: basically, your business logic

your plan should really contain step-to-step details about what your code must do, not just what the person who uses your code should see it do


Ok and since i have to do it in the model view control pattern when im done how do i connect them all because you canonly extend one class at a time. I know i would have to probably imlement them in a interface but i dont know what i should put in the interfaces.


ehm .. no
just because a method is in another class, doesn't mean your class can't use it.
are you familiar with Object Oriented programming, even without being concerend about the MVC pattern?


yes you mean inheritance, encapsulaion, polymorphism yes i am familiar with it.


Then what do you mean by

how do i connect them all because you canonly extend one class at a time.

you don't need to extend a class to be able to use it.

for instance, you're using the String class in your application, so you have access to all actions that you can perform on a String (which are simply the methods within the String class), but you 're not inheriting or implementing it.

so, for the 'connecting', either you create (an)instance(s) of the class you want to use, or you call static methods like [classname].[methodname]

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.