import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class Hangman extends JFrame { private Hangman.Hangman_Panel canvas = new Hangman.Hangman_Panel(); // How many times you can guess the wrong letter till you lose static final int DEAD = 7; private int errors; // amount of errors private String message; // Message displaying either Error or Victory private String information; // Secondary Message private String RealWord; // The Real word private StringBuffer GuessWord;// The Guessed word private Button StartBtn; // The Restart Button private Button GoBtn; // The Go Button private TextField LetterBox; // The LetterBox //Contructor public Hangman() { this.add(canvas, BorderLayout.CENTER); // Add canvas to center // Create a "Textbox" for the letter guessing LetterBox = new TextField(); JPanel p = new JPanel(); p.setLayout(new FlowLayout()); p.add(StartBtn = new Button("Restart")); p.add(new Label("Guess a letter")); p.add(LetterBox); p.add(GoBtn = new Button("Go")); add(p, BorderLayout.SOUTH); } public static void main(String[] args) { JFrame frame = new Hangman(); frame.setSize(300, 400); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } class Hangman_Panel extends JPanel implements ActionListener { public void actionPerformed(ActionEvent e){ if (e.getSource() == StartBtn){ initGame(); } if (e.getSource() == GoBtn){ processTurn(); LetterBox.setText(""); repaint(); } } @Override public void paint(Graphics g) { super.paintComponent(g); int BaseY = 250; //THE HANGING STAND if (errors > 0) { //1 Error g.drawLine(90, BaseY, 200, BaseY); //The ground g.drawLine(125, BaseY, 125, BaseY-100); //The bar going up g.drawLine(125, BaseY-100, 175, BaseY-100); //The sidebar g.drawLine(175, BaseY-100, 175, BaseY-75); //The Rope } //THE PERSON if (errors > 1) { g.drawOval(170, BaseY-75, 10, 12); // The Head } if (errors > 2) { g.drawLine(175, BaseY-62, 175, BaseY-45); // The Body } if (errors > 3) { g.drawLine(165, BaseY-65, 175, BaseY-55); // Left Arm } if (errors > 4) { g.drawLine(185, BaseY-65, 175, BaseY-55); // Right Arm } if (errors > 5) { g.drawLine(170, BaseY-30, 175, BaseY-45); //Left Leg } if (errors > 6) { //7 Errors g.drawLine(175, BaseY-45, 180, BaseY-30); // Right Left } //Show Messages/Errors g.drawString(message, 40, BaseY+25); g.drawString(information, 25, BaseY+45); g.drawString(new String (GuessWord), 140, BaseY-120); g.drawString(new String("WELCOME TO HANGMAN!"), 75, 40); } public void init() { //Make buttons event listeners StartBtn.addActionListener(this); GoBtn.addActionListener(this); //Startup the Game initGame(); } public void initGame() { //Set the errors to 0 errors = 0; //Enter the wordslist, separated by a | here String str = "write|program|receive|positive|variables|temporary|good|bad|test"; String[] temp; //delimiter String delimiter = "\\|"; // given string will be split by the argument delimiter provided. temp = str.split(delimiter); //Create the Random Seed Generator Random RandGenerator = new Random(); //Generate my Random Number int randomInt = RandGenerator.nextInt(temp.length); RealWord = new String(temp[randomInt]); char positions[] = new char[RealWord.length()]; for (int i = 0; i < RealWord.length(); i++) { positions[i] = '*'; } String s = new String(positions); GuessWord = new StringBuffer(s); LetterBox.setText(""); //Delete Messages message = ""; information = ""; repaint(); } private void processTurn() { String s, t; char a; s = LetterBox.getText(); a = s.charAt(0); if (!Character.isLetter(a)) { message = "Only enter letters please."; return; } if (s.length() > 1) { message = "One letter at a time please."; return; } //Check if letter has been used already t = new String(GuessWord); if (t.indexOf(s) != -1) { message = "You have already guessed with that letter!"; return; } //If the letter you guessed does not occur int he Real Word if (RealWord.indexOf(s) == -1) { message = ""; errors++; if (errors==DEAD) { message = "Sorry, you lose"; information = "Click restart to try again!"; //INSERT MOVING HANGMAN HERE. } return; } //Replace stars in the Guessed Word with the found letter for (int i = 0; i < RealWord.length(); i++) { if (RealWord.charAt(i) == a) { GuessWord.setCharAt(i, a); } } t = new String(GuessWord); //If all of the stars have been filled, then you win! if (t.indexOf('*') == -1) { message = "You have won!"; return; } //Delete the Message message = ""; repaint(); } } }