Daniel Haro's profile

Hang Man Project using Java : GAME APP LEVEL


Some type of sub-title
Basic Hang Man project using the AWT java framework.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CopyRight extends JDialog
{
public CopyRight(String message, String title)
{
setLocationRelativeTo(null);
Icon image = new ImageIcon("hangmana.gif");
JOptionPane.showMessageDialog(this,message,
title,JOptionPane.INFORMATION_MESSAGE,image);
setResizable(false);
}
public static void main(String [] args)
{
CopyRight copyright1 = new CopyRight(message1,title1);
CopyRight copyright2 = new CopyRight(message2,title2);
//display game board next
}
static String message1 = "Hangman Version 1.0 \n CopyRight(C) 2003\n"
+"Daniel Haro";
static String message2 = "When Hangman begins, a category will be selected"
+" and one blank\n for each occurence of each letter"
+" in the word(s) that constitute \n the item in that"
+" category will be displayed.\n"
+"\n To play the game, try to guess the letters in "
+" the word(s), one \n at a time, by clicking on a letter"
+" typing alt + key where \n key represents the letter you "
+" wish to guess. If you guess \n correctly, each occurence"
+" of the letter will appear in the \n appropriate blank(s)"
+" If you guess incorrectly, another body \n part will"
+" appear under the hanging gallows and the number of \n"
+" remaing will decrease by one. The number of guesses \n"
+" remaining before you will be hung will appear bellow"
+" the gallows.\n After each guess, the letter guessed will"
+" appear in a used letter \n section. The total number of"
+" guesses for each new game is seven.\n\n\n"
+" Have fun and good luck!";
static String title1 = "About Hangman";
static String title2 = "How to play Hangman";
}
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class DrawPanel extends JPanel
{
public void resetState()
{
state = 1;
}

public void incState()
{
state++;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); // to paint the background
// dimension = getSize();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();

System.out.println("df2 " + d.width + " X " + d.height);
this.setBackground(Color.WHITE);
if(state <= 8)

if(d.width >= 1024)
currentPic = "z" + state + ".gif";
else
if(d.width >= 800)
currentPic = "x" + state + ".gif";
else
currentPic = "Gallows_" + state + " copy.gif";
guyPic = new ImageIcon(currentPic);
guyPic.paintIcon(this,g,0,0);
}
ImageIcon guyPic = new ImageIcon("Gallows_1 copy.gif");
int state = 1;
protected int x, y;
protected Dimension dimension;
String currentPic = null;
}
import java.util.*;
import java.io.*;
class FileIO
{
BufferedReader dataReader;
BufferedWriter dataWriter;
FileWriter dataFileOutput;
FileReader dataFileInput;
StringTokenizer nextLine = null;
String test = null;
boolean opFound = false;
String tempOp;
boolean eof = false;

//constructor to open file///
FileIO(String inFile)
{
try
{
dataFileInput = new FileReader(inFile);
dataReader = new BufferedReader(dataFileInput);
// getNextLine();
}
catch(FileNotFoundException e)
{}
catch(IOException e)
{}
}

// gets next line in file
public String getNextLine()
{
String tempLine = null;
if(eof == false)
{
try{
tempLine = dataReader.readLine();
if(tempLine == null)
{
eof = true;
nextLine = null;
}
else
{
nextLine = new StringTokenizer(tempLine);
}
}
catch(IOException e)
{}
}
return(tempLine);
}
import java.math.*;
import java.util.*;
public class GamePieceHandler
{
Vector Games = new Vector();
GameToken currentPiece;
Vector puzzelView;
int guesses = 7;
FileIO fileHandler;
Vector myFiles = new Vector();


public GamePieceHandler()
{
myFiles.add("Sports.dat" );
myFiles.add("Movies.dat" );
myFiles.add("Music.dat" );
myFiles.add("Quotes.dat" );
int fileCounter = 0;
String tempFile = null;
String loadHint;
String loadAnswer = "";
for(fileCounter = 0; fileCounter < myFiles.size(); fileCounter++)
{
Vector tempVector = new Vector();
tempFile = (String) myFiles.elementAt(fileCounter);
fileHandler = new FileIO(tempFile);
while( (loadAnswer = fileHandler.getNextLine()) != null)
{

loadHint = fileHandler.getNextLine();
GameToken temp = new GameToken(loadHint,loadAnswer);
tempVector.add(temp);
}
Games.add(tempVector);
}
}

public int getGuesses()
{
return guesses;
}

public void CreatePiece(int catagory)
{
tokenGrab(catagory);
setView();
}
public void updateView(char guess)
{
String temp = currentPiece.getAnswer();
boolean validChar = false;
for(int counter = 0 ; counter < temp.length() ; counter++)
{
if(temp.charAt(counter) == guess || temp.charAt(counter) == (guess - 32))
{
puzzelView.setElementAt(new Boolean(true), counter);
validChar = true;
}
}
if(validChar == false)
guesses--;
}

public boolean isWon()
{
String temp = currentPiece.getAnswer();
boolean complete = true;
for(int counter = 0 ; counter < temp.length() ; counter++)
{
Boolean check = (Boolean) puzzelView.elementAt(counter);
if(check.booleanValue() == false)
complete = false;
}
if(complete == true)
puzzelView.setElementAt(new Boolean(false),0);
return complete;
}
public boolean isLost()
{
if(guesses == 0)
{
guesses = -1;
return true;
}
else
return false;
}



public String getView()
{
String currentView ="";
String temp = currentPiece.getAnswer();
for(int counter = 0 ; counter < temp.length() ; counter++)
{
if( ((Boolean)(puzzelView.elementAt(counter))).booleanValue() == true)
currentView = currentView + temp.charAt(counter) + " ";
else
currentView = currentView + "_" + " ";
}
return currentView;
}

public String getHint()
{
return currentPiece.getHint();
}
public String getAnswer()
{
return currentPiece.getAnswer();
}

private void setView()
{
puzzelView = new Vector();
for(int counter = 0; counter < currentPiece.getAnswer().length(); counter++)
puzzelView.add(new Boolean(false));
}

private void tokenGrab(int cat)
class GameToken
{
private String hint;
private String answer;

public GameToken(String theHint,String theAnswer)
{
hint = theHint;
answer = theAnswer;
}

public String getAnswer()
{
return answer;
}
public String getHint()
{return hint;}
}
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;

class HangFrame extends JFrame
{
int currentState = 0;
DrawPanel hangPic;
JComboBox catagoryBox;
JComboBox levelBox;
GamePieceHandler game = null;
String Hint = null;
String pos = null;
String guesses = null;
JLabel puzzel = null;
JLabel puzzelHint = null;
JLabel guessesLeft = null;
JPanel stats = null;
int subject = 0;
Vector level = null;
Vector cats = null;
JPanel buttons = null;
JPanel keys = null;
JSplitPane splitPane;


//This is being added for the class version and left out the personal version

JPanel used = new JPanel();


public HangFrame()
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();
Res gack;
if(!(d.width == 640 ||d.width == 800 || d.width ==1024))
gack = new Res();
VectorSet();
MakeButtons();
musicLoader();
buildScreen();

}
private class ComboHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
subject = catagoryBox.getSelectedIndex();
}
}

private class ComboHandler2 implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Color temp;
// if("zyz".compareTo(e.getItem()) == 0)
}
}

private void updateUPanel(char temp)
{
JLabel dur = new JLabel(new String( "" + temp));
used.add(dur);
used.revalidate();
repaint();
}

private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int counter = 97; counter < 124; counter++)
{
String test = "" + (char) counter;
if(e.getActionCommand().compareTo(test) == 0)
{
String holdpos = null;
holdpos = pos;
JButton temp = (JButton) keys.getComponent(counter - 97);
temp.setEnabled(false);
if(counter != 123)
{
game.updateView((char)counter);
updateUPanel((char) counter);
}
else
{
game.updateView(' ');
updateUPanel('_');
}
pos = game.getView();
if(holdpos.equals(pos))
hangPic.incState();
puzzel.setText(pos);
guesses = "Guess Left " + game.getGuesses();
guessesLeft.setText(guesses);
used.repaint();
hangPic.repaint();
repaint();

}
if(game.isWon())
{
puzzel.setText("You Won");
musicHandler(2);
Won Winner = new Won();
for(counter = 97; counter < 124; counter++)
{
JButton temp = (JButton) keys.getComponent(
counter - 97);
temp.setEnabled(false);
}

pos = game.getView();
hangPic.repaint();
}
if(game.isLost())
{
puzzel.setText("You LOST");


for(counter = 97; counter < 124; counter++)
{
JButton temp = (JButton) keys.getComponent(
counter - 97);
temp.setEnabled(false);
}
musicHandler(1);
Lost looser = new Lost(game.getAnswer());
pos = game.getView();
hangPic.repaint();


}
}

if(e.getActionCommand().compareTo("__") == 0 && currentState == 0)
{
JButton temp = (JButton) keys.getComponent(26);
temp.setEnabled(false);
}

if(e.getActionCommand().compareTo("New Game") == 0 && currentState == 0)
{
hangPic.resetState();
musicHandler(0);
game = new GamePieceHandler();
game.CreatePiece(subject);
guesses = "Guess Left " + game.getGuesses();
guessesLeft.setText(guesses);
used.removeAll();
used.add(new JLabel("Letters Used-->"));
used.repaint();
Hint = game.getHint();
pos = game.getView();
puzzel.setText(pos);
puzzelHint.setText(Hint);
repaint();
for(int counter = 97; counter < 124; counter++)
{
JButton temp = (JButton) keys.getComponent(counter - 97);
temp.setEnabled(true);
}
hangPic.repaint();
}
if(e.getActionCommand().compareTo("Quit") == 0 && currentState == 0)
{
f.dispose();
System.exit(0);
}
}
}



public void VectorSet()
{
level = new Vector();
cats = new Vector();
level.add(new String ("Light foot"));
level.add(new String("Smarty pants"));
level.add( new String("Geek"));
cats.add(new String("Sports"));
cats.add(new String("Movies"));
cats.add(new String("Music"));
cats.add(new String("Quotes"));
}

public void MakeButtons()
{
keys = new JPanel();
buttons = new JPanel();
buttons.setBackground(Color.BLACK);
keys.setLayout(new GridLayout(7,4));
JButton reStart = new JButton("New Game");
JButton Exit = new JButton("Quit");
JButton toggel = new JButton("Toggel Hints");
for(int counter = 97; counter < 123; counter++)
{
String alphaChar = "Skeleton " + (char)counter + ".jpg";
ImageIcon alphaPic = new ImageIcon(alphaChar);
JButton TempButton = new JButton(alphaPic);
TempButton.setBackground(Color.BLACK);
keys.add(TempButton);
TempButton.setActionCommand(new String("" + (char) counter));
TempButton.addActionListener(new ButtonHandler());
TempButton.setMnemonic((char) counter);
}
JButton tempButton = new JButton(new ImageIcon("DashBone.gif"));
tempButton.setBackground(Color.BLACK);
tempButton.setActionCommand("{");
tempButton.addActionListener(new ButtonHandler());
tempButton.setMnemonic(' ');
keys.add(tempButton);
catagoryBox = new JComboBox(cats);
levelBox = new JComboBox(level);
reStart.setBackground(Color.GREEN);
reStart.setForeground(Color.WHITE);
Exit.setBackground(Color.RED);
Exit.setForeground(Color.WHITE);
catagoryBox.setForeground(Color.BLACK);
catagoryBox.setBackground(Color.YELLOW);
levelBox.setForeground(Color.RED);
levelBox.setBackground(Color.GREEN);
buttons.add(reStart);
buttons.add(Exit);
buttons.add(catagoryBox);
buttons.add(levelBox);
buttons.setBackground(Color.BLACK);
reStart.addActionListener(new ButtonHandler());
Exit.addActionListener(new ButtonHandler());
catagoryBox.addItemListener(new ComboHandler());
levelBox.addItemListener(new ComboHandler2());
}
JSplitPane dataPane;
JFrame f = new JFrame("Hang-Man");
public void buildScreen()
{

Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension d = toolkit.getScreenSize();
Container content = f.getContentPane();
hangPic = new DrawPanel();
f.setBounds(0, 0, d.width, d.height);
game = new GamePieceHandler();
game.CreatePiece(subject);
Hint = game.getHint();
pos = game.getView();
guesses = "Guess Left " + game.getGuesses();
guessesLeft = new JLabel(guesses);
stats = new JPanel();
puzzel = new JLabel(pos);
puzzelHint = new JLabel(Hint);
stats.setLayout(new GridLayout(2,1));
stats.add(puzzel);
stats.add(puzzelHint);

dataPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
used,stats);


dataPane.setDividerLocation(25);
content.add("South", buttons);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
dataPane,keys);


JPanel lowerLeft = new JPanel();
lowerLeft.add(guessesLeft);
splitPane.setDividerLocation(75);
JSplitPane splitPaneLeft = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
hangPic,guessesLeft);
splitPaneLeft.setDividerLocation(d.height - 105);


f.setResizable(false);
used.add(new JLabel("Letters Used-->"));
content.add("East",splitPane);
content.add("Center",splitPaneLeft);
pack();
f.setVisible(true);
}
private void musicHandler(int musicFunction)
{
for(int counter =0; counter < 3; counter++)
audioClip[counter].stop();
if(musicFunction == 0)
audioClip[musicFunction].loop();
else
audioClip[musicFunction].play();

}
AudioClip audioClip[] = new AudioClip[10];
private void musicLoader()
{


try
{
URL baseURL = new URL("file:" + System.getProperty("user.dir") + "/");
URL completeURL = new URL(baseURL, "game.wav");
audioClip[0] = Applet.newAudioClip(completeURL);
completeURL = new URL(baseURL, "TAPS.WAV");
audioClip[1] = Applet.newAudioClip(completeURL);
completeURL = new URL(baseURL, "YEAH.WAV");
audioClip[2] = Applet.newAudioClip(completeURL);
audioClip[0].loop();
}
catch (MalformedURLException exception) {}

}

public static void main (String args[])
{
HangFrame tester = new HangFrame();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lost extends JDialog
{
public Lost(String ans)
{
String message1 = "Sorry, you did not guess the word. It was\n"
+ans + "\n What do you want to do now?\n";
String title1 = "You've been hanged: Ha,ha,ha!";
setLocationRelativeTo(null);
Icon image = new ImageIcon("sad.gif");
Object[] options = {"Keep Playing", "Quit"};
int result = JOptionPane.showOptionDialog(this,message1,
title1,JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
image,options,options[1]);
if(result == JOptionPane.YES_OPTION)
{//call play
System.out.println("We are playing a game");
}
else
{//quit
System.exit(0);
}
setResizable(false);
}
}
Matrix
I Know Kung Fu...
Lord of the Rings
All Will Love Me and Despair
Spider Man
With Great Power Comes Great Responsibility
Tai Chi Master
I Will Destroy Your Skill
Office Space
How are those TPS Reports Coming.
Scarface
Who Put This Thing Together Me That's Who. Who do I Trust? Me
Gladiator
Am I Not Merciful!
The End of Evangelion
The Truth Causes Everyone Pain... The Truth is Very Very Traumatic
Star Wars Attack of The Clones
I'm a Jedi... I know I'm Better Than This
Crouching Tiger Hidden Dragon
No Desire Without Restraint, Now Give Yourself Up and Find Yourself Again
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Res extends JDialog
{
public Res()
{
setLocationRelativeTo(null);
Icon image = new ImageIcon("happy.gif");
Object[] options = {"Play Anyway", "Quit"};
int result = JOptionPane.showOptionDialog(this,message1,
title1,JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
image,options,options[1]);
if(result == JOptionPane.YES_OPTION)
{//call play
System.out.println("We are playing a game");
}
else
{//quit
System.exit(0);
}
setResizable(false);
}
public static void main(String [] args)
{

}
String message1 = "Hang Man requires one of the following resouitions to display properly\n" +
"640X480, 800X600, 1024 X 768";

String title1 = "Warning";
}
public class test
{
public test()
{}
public void main(String args[])
{}
}
Hang Man Project using Java : GAME APP LEVEL
Published:

Hang Man Project using Java : GAME APP LEVEL

Hnag Man game I created at San Jose State Univeristiy

Published:

Creative Fields