Advertisement
Guest User

Hangman

a guest
Dec 9th, 2013
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*; //Makes it a Japplet
  5.  
  6. import java.util.Random;
  7.  
  8. public class Hangman extends JApplet implements ActionListener {
  9.  
  10.     // How many times you can guess the wrong letter till you lose
  11.     static final int DEAD = 7;  
  12.    
  13.     private int errors;        // amount of errors
  14.     private String message;   // Message displaying either Error or Victory
  15.     private String information; // Secondary Message
  16.     private String RealWord;      // The Real word
  17.     private StringBuffer GuessWord;// The Guessed word
  18.     private Button StartBtn;      // The Restart Button
  19.     private Button GoBtn;         // The Go Button
  20.     private TextField LetterBox; // The LetterBox
  21.    
  22.     public void actionPerformed(ActionEvent e){
  23.  
  24.         if (e.getSource() == StartBtn){
  25.             initGame();
  26.         }
  27.  
  28.         if (e.getSource() == GoBtn){
  29.  
  30.             processTurn();
  31.  
  32.             LetterBox.setText("");
  33.             repaint();
  34.         }
  35.     }
  36.    
  37.     public void init() {
  38.        
  39.         // Create a "Textbox" for the letter guessing
  40.         LetterBox = new TextField();
  41.        
  42.         //Create my buttons and labels
  43.         StartBtn = new Button("Restart");
  44.         GoBtn = new Button("Go");
  45.        
  46.         //Add the elements to the applet
  47.        
  48.         JPanel p = new JPanel();
  49.         p.setLayout(new FlowLayout());
  50.        
  51.         p.add(StartBtn);
  52.         p.add(new Label("Guess a letter"));
  53.         p.add(LetterBox);
  54.         p.add(GoBtn);
  55.        
  56.         add(p, BorderLayout.SOUTH);
  57.        
  58.         /*
  59.         add(StartBtn);
  60.         add(new Label("Guess a letter"));
  61.         add(LetterBox);
  62.         add(GoBtn);
  63.         */
  64.        
  65.         //Make buttons event listeners
  66.         StartBtn.addActionListener(this);
  67.         GoBtn.addActionListener(this);
  68.        
  69.         //Startup the Game
  70.         initGame();
  71.        
  72.     }
  73.    
  74.     public void initGame() {
  75.         //Set the errors to 0
  76.         errors = 0;
  77.        
  78.         //Enter the wordslist, separated by a | here
  79.         String str = "write|program|receive|positive|variables|temporary|good|bad|test";        
  80.         String[] temp;
  81.                
  82.      
  83.         //delimiter
  84.         String delimiter = "\\|";
  85.                
  86.         // given string will be split by the argument delimiter provided.
  87.         temp = str.split(delimiter);
  88.        
  89.         //Create the Random Seed Generator
  90.         Random RandGenerator = new Random();
  91.        
  92.         //Generate my Random Number
  93.         int randomInt = RandGenerator.nextInt(temp.length);
  94.        
  95.         RealWord = new String(temp[randomInt]);
  96.        
  97.         char positions[] = new char[RealWord.length()];
  98.        
  99.         for (int i = 0; i < RealWord.length(); i++) {
  100.             positions[i] = '*';
  101.         }
  102.        
  103.         String s = new String(positions);
  104.         GuessWord = new StringBuffer(s);
  105.        
  106.         LetterBox.setText("");
  107.        
  108.         //Delete Messages
  109.         message = "";
  110.         information = "";
  111.         repaint();
  112.     }
  113.  
  114.     @Override
  115.     public void paint(Graphics g) {
  116.        
  117.         int BaseY = 250;
  118.              
  119.         //THE HANGING STAND
  120.         if (errors > 0) { //1 Error
  121.             g.drawLine(90, BaseY, 200, BaseY); //The ground
  122.             g.drawLine(125, BaseY, 125, BaseY-100); //The bar going up
  123.             g.drawLine(125, BaseY-100, 175, BaseY-100); //The sidebar
  124.             g.drawLine(175, BaseY-100, 175, BaseY-75); //The Rope
  125.         }
  126.        
  127.         //THE PERSON
  128.         if (errors > 1) {
  129.            g.drawOval(170, BaseY-75, 10, 12); // The Head      
  130.         }
  131.  
  132.         if (errors > 2) {
  133.            g.drawLine(175, BaseY-62, 175, BaseY-45); // The Body    
  134.         }
  135.  
  136.         if (errors > 3) {
  137.            g.drawLine(165, BaseY-65, 175, BaseY-55); // Left Arm  
  138.         }
  139.                
  140.         if (errors > 4) {
  141.            g.drawLine(185, BaseY-65, 175, BaseY-55); // Right Arm
  142.         }
  143.        
  144.         if (errors > 5) {
  145.            g.drawLine(170, BaseY-30, 175, BaseY-45); //Left Leg      
  146.         }
  147.        
  148.         if (errors > 6) {  //7 Errors
  149.            g.drawLine(175, BaseY-45, 180, BaseY-30); // Right Left
  150.         }
  151.        
  152.  
  153.         //Show Messages/Errors
  154.         g.drawString(message, 40, BaseY+25);
  155.         g.drawString(information, 25, BaseY+45);
  156.         g.drawString(new String (GuessWord), 140, BaseY-120);
  157.        
  158.         g.drawString(new String("WELCOME TO HANGMAN!"), 75, 40);
  159.     }
  160.    
  161.     private void processTurn() {
  162.        
  163.         String s, t;
  164.         char a;
  165.        
  166.         s = LetterBox.getText();
  167.         a = s.charAt(0);
  168.        
  169.         if (!Character.isLetter(a)) {
  170.             message = "Only enter letters please.";
  171.             return;
  172.         }
  173.        
  174.         if (s.length() > 1) {
  175.             message = "One letter at a time please.";
  176.             return;
  177.         }
  178.        
  179.         //Check if letter has been used already
  180.         t = new String(GuessWord);
  181.         if (t.indexOf(s) != -1) {
  182.             message = "You have already guessed with that letter!";
  183.             return;
  184.         }
  185.        
  186.         //If the letter you guessed does not occur int he Real Word
  187.         if (RealWord.indexOf(s) == -1) {
  188.            
  189.             message = "";
  190.             errors++;
  191.            
  192.             if (errors==DEAD) {
  193.                 message = "Sorry, you lose";
  194.                 information = "Click restart to try again!";
  195.                
  196.                 //INSERT MOVING HANGMAN HERE.
  197.             }
  198.            
  199.             return;
  200.         }
  201.        
  202.         //Replace stars in the Guessed Word with the found letter
  203.         for (int i = 0; i < RealWord.length(); i++) {
  204.             if (RealWord.charAt(i) == a) {
  205.                 GuessWord.setCharAt(i, a);
  206.             }
  207.         }
  208.        
  209.         t = new String(GuessWord);
  210.        
  211.         //If all of the stars have been filled, then you win!
  212.         if (t.indexOf('*') == -1) {
  213.             message = "You have won!";
  214.             return;
  215.         }
  216.        
  217.         //Delete the Message
  218.         message = "";
  219.         repaint();
  220.     }
  221.    
  222.     public static void main(String[] args) {
  223.        
  224.         JFrame frame = new JFrame();
  225.         JApplet applet = new Hangman();
  226.  
  227.         applet.init();
  228.         applet.start();
  229.         frame.add(applet);
  230.  
  231.         frame.setSize(300, 400);
  232.         frame.setLocationRelativeTo(null); // Center the frame
  233.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  234.         frame.setVisible(true);
  235.        
  236.  
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement