2 Replies - 864 Views - Last Post: 05 December 2012 - 10:09 PM Rate Topic: -----

#1 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

Programming Hangman Game using MVC problem selecting letters

Posted 05 December 2012 - 05:25 PM

I'm trying to program a hangman GUI game using Model-View-Controller. For the most part, the code works and I am able to guess a word in full. My problem, however is that I cannot select individual letters to guess and as a result can't play the game. I am posting each of my classes below.

import java.io.File;
import java.util.*;

public class HangmanModel {
	private String hiddenWord, hiddenDisplay, fileError;
	String [] dictionary;
	int size=0;
	int errors=0;
	private boolean gameOver = true;
	
	public void wordsList() throws Exception{
		File file = new File("words.txt");
		Scanner scanner = new Scanner(file);
		if(file.exists()){
			while(scanner.hasNextInt()){
				size = scanner.nextInt();
			}
			dictionary = new String[size];
			for (int i = 0; i<size; i++){
				dictionary[i] = scanner.nextLine();
			}
			
		}
	}
	
	public void newGame(){
		hiddenDisplay = "";
		hiddenWord = getRandomWord();
	}
	
	public int guessWord(String B)/>/>/>{
		if (b.equalsIgnoreCase(hiddenWord)){
			return 1;
		} else {
			return 0;
		}
	}
	
	public String populateWord(){ //view calls this
		for (int i = 0; i < hiddenWord.length();i++){
			hiddenDisplay += " * ";
		}
	return hiddenDisplay;
	}
	
	public int processTurn(char c){
		String x ="",y="";
		if (hiddenWord.indexOf(c)== -1){ //not in the word
		errors++;
			if (errors == 10){
				return 1;
			}
			
		} else {
			for(int i = 0; i <hiddenDisplay.length(); i++){
				if (i != 0){
					x = hiddenDisplay.substring(0, i);
					return 0;
				}
				if (i != hiddenWord.length()){
					y = hiddenDisplay.substring(i+1,hiddenWord.length());
					hiddenDisplay = x + c + y;
					return 0;
			}
		}
		if(hiddenDisplay.equalsIgnoreCase(hiddenWord)){
			hiddenDisplay = getWinner();
		return 1;
		}
		}
	return 0;
	}

	private String getWinner() {
		return "Congratulations! You are a winner!";
	}

	private String getRandomWord() {
		int r = (int) ((Math.random()*9)-1);
		return dictionary[r];
	}

	private void resetVariables() {
		gameOver = false;
		hiddenDisplay = "";
		hiddenWord = "";
		errors = 1;
	}
	
	public String getFileError(){
		return fileError;
	}
	
	public int getErrors(){
		return (errors -1);
	}
	
	public String getHiddenDisplay(){
		return hiddenDisplay;
	}
	
	public boolean gameOver(){
		return gameOver;
	}
	
	

}

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;


public class HangmanView extends JFrame {

	String [] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M",
								"N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	JButton [] alphabet;
	HangmanModel model;
	JButton buttonNewGame, buttonGuess;
	ImageIcon [] hangmanImage;
	JLabel label, statusGame, guessLabel, labelHangman, welcome, dashes;
	JTextField guessWordFull;
	JPanel alphabetArea, mainArea, guessArea, statusArea, newGamePanel,topPanel;
	JPanel greeting,bottomPanel,lettersArea, imagePanel, guessWordPanel;
	
	public HangmanView(HangmanModel model) {
		this.model = model;
		setLayout(new BorderLayout());
		setSize(800,600);
		
		 topPanel = new JPanel(new BorderLayout());
			greeting = new JPanel(new FlowLayout());
			
				welcome = new JLabel("Welcome to Hang Man! Please click 'New Game' to begin!");
				greeting.add(welcome);
				
				newGamePanel = new JPanel(new GridLayout(1,1));
					buttonNewGame = new JButton("New Game");
					newGamePanel.add(buttonNewGame);
					
					buttonGuess = new JButton("Guess Word");
					buttonGuess.setEnabled(false);
					
					
		topPanel.add(greeting, BorderLayout.NORTH);
		topPanel.add(newGamePanel,BorderLayout.SOUTH);
					
		bottomPanel = new JPanel(new BorderLayout());
			guessWordPanel = new JPanel(new GridLayout(2,2));
		
			lettersArea = new JPanel(new GridLayout(2,13,2,2));
	
			alphabet = new JButton[letters.length];
				for(int i = 0; i < letters.length;i++){
					alphabet[i] = new JButton(letters[i]);
					alphabet[i].setEnabled(false);
					lettersArea.add(alphabet[i]);
				}
				bottomPanel.add(lettersArea,BorderLayout.SOUTH);
				bottomPanel.add(guessWordPanel,BorderLayout.CENTER);
					guessWordPanel.add(buttonGuess);
					guessWordFull = new JTextField("",7);
					guessWordPanel.add(guessWordFull);
				
				
		imagePanel = new JPanel(new BorderLayout());
			hangmanImage = new ImageIcon[10];
			for(int i = 0; i < hangmanImage.length;i++){
				hangmanImage[i] = new ImageIcon("hang" + i + ".gif");
			}
			labelHangman = new JLabel(hangmanImage[0]);
			imagePanel.add(labelHangman,BorderLayout.NORTH);
			dashes = new JLabel("Get Ready!");
			dashes.setHorizontalAlignment(SwingConstants.CENTER);
			imagePanel.add(dashes,BorderLayout.SOUTH);
			
			
		
		add(topPanel,BorderLayout.NORTH);
		add(imagePanel,BorderLayout.CENTER);
		add(bottomPanel,BorderLayout.SOUTH);
	}
	
	public void enableButtons(){
		for (int i =0; i <alphabet.length;i++){
			alphabet[i].setEnabled(true);
		}
		wrongLetters(0);
		buttonGuess.setEnabled(true);
	}
	
	public void setController(HangmanController c){
		this.buttonNewGame.addActionListener(c);
		this.buttonGuess.addActionListener(c);
	}
	
	public void setHiddenWord(String s){
		this.dashes.setText(s);
	}
	
	public void initialize() throws Exception{
		model.wordsList();
		setVisible(true);
	}
	
	public void wrongLetters(int errors){
		labelHangman.setIcon(hangmanImage[errors]);
	}
	
	public void winner(){
		for (int i = 0; i < letters.length; i++){
			alphabet[i].setEnabled(false);
		}
		JOptionPane.showMessageDialog(null,"You won!");
		wrongLetters(0);
		buttonGuess.setEnabled(false);
	}
	public void loser(){
		for (int i = 0; i < letters.length; i++){
			alphabet[i].setEnabled(false);
		}
		JOptionPane.showMessageDialog(null,"You lost!");
		wrongLetters(9);
		buttonGuess.setEnabled(false);
	}
}	

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;


public class HangmanController implements ActionListener {
	HangmanModel model;
	HangmanView view;
	
	public HangmanController(HangmanModel model, HangmanView view) {
		this.model = model;
		this.view = view;
	}
	

	public void actionPerformed(ActionEvent arg0) {
		JButton newGame = (JButton) arg0.getSource();
		JButton guessWord = (JButton) arg0.getSource();
		JButton [] guessLetters = new JButton[view.alphabet.length];
		
		for (int i = 0; i < guessLetters.length; i++){
			guessLetters[i] = (JButton) arg0.getSource();
		}
		if ("New Game".equals(newGame.getText())){
			model.newGame();
			view.setHiddenWord(model.populateWord());
			view.enableButtons();
		} else if ("Guess Word".equals(guessWord.getText())){
				if (model.guessWord(view.guessWordFull.getText())== 1){
					view.winner();	
				} else {
					view.loser();
				}
		} else {
			for (int i = 0; i < guessLetters.length; i++){
				if (guessLetters[i].equals(view.alphabet)){
					model.processTurn(arg0.getActionCommand().charAt(i));
					//view.hangmanImage;
				} else {
					view.wrongLetters(i);
				}
			}
		}
		
	}
}





I think the issue is that my controller is not properly calling something in the view, or perhaps I'm not filling the array of letter buttons correctly. I notice that when tracing hte program, the array that i'm trying to create in HangmanController.java doesn't fill with buttons or with strings.

Any help or advice would be greatly appreciated.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Programming Hangman Game using MVC problem selecting letters

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Programming Hangman Game using MVC problem selecting letters

Posted 05 December 2012 - 09:22 PM

The String class has two methods that may interest you

.toCharArray(); which returns an array of char[] form a String
.chatAt(N); which returns the char at position N in the String
Was This Post Helpful? 0
  • +
  • -

#3 dfl2m  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-December 12

Re: Programming Hangman Game using MVC problem selecting letters

Posted 05 December 2012 - 10:09 PM

View Postpbl, on 05 December 2012 - 09:22 PM, said:

The String class has two methods that may interest you

.toCharArray(); which returns an array of char[] form a String
.chatAt(N); which returns the char at position N in the String



Should I be using .toCharArray() and charAt(n) in my model then call them from my controller or use them in controller?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1