Advertisement
Guest User

Untitled

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