Don't like ads? PRO users don't see any ads ;-)
Guest

game

By: a guest on Aug 21st, 2012  |  syntax: Java  |  size: 5.78 KB  |  hits: 51  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.awt.image.BufferedImage;
  7. import javax.imageio.ImageIO;
  8.  
  9. public class hangmanGUI extends JFrame implements ActionListener
  10. {
  11.         private hangman hangman = new hangman();
  12.         private String selectedWord = new String();
  13.         private String disguisedWord = new String();
  14.         private Vector<String> words = new Vector<String>();
  15.         private Button wordCheck;
  16.         private Button letterCheck;
  17.         private JTextArea letterIn;
  18.         private JTextArea wordIn;
  19.         private int guesses;
  20.        
  21.         public void gameLoop()throws IOException
  22.         {      
  23.                 JFrame hangFrame = new JFrame();
  24.                 JPanel main = new JPanel();            
  25.                 main.setLayout(new BorderLayout());
  26.                 words = hangman.load();//retrives a vector of words from the text file
  27.                 guesses = 10;//sets the number of guesses a person can make
  28.                 selectedWord = hangman.selectWord(words);//randomly (pseudorandom) chosses a single word from a vector
  29.                 disguisedWord = hangman.disguseWord(selectedWord);//takes selected words and converts into a string of '-' for displaying to the player
  30.                
  31.                 JPanel displayBar = new JPanel();              
  32.                 displayBar.setLayout(new BorderLayout());
  33.                 JLabel guessCount = new JLabel("You have " + guesses + " guesses left ");
  34.                 JLabel wordClue = new JLabel("Your Guessing the Word " + disguisedWord);
  35.                 guessCount.setFont(new Font("Arial", Font.PLAIN, 12));
  36.                 wordClue.setFont(new Font("Arial", Font.PLAIN, 12));
  37.                 displayBar.add(guessCount, BorderLayout.EAST);
  38.                 displayBar.add(wordClue, BorderLayout.WEST);
  39.                 main.add(displayBar, BorderLayout.NORTH);
  40.        
  41.                 JPanel guessBar = new JPanel();
  42.                 guessBar.setLayout(new BorderLayout());
  43.                 Button letterCheck = new Button("Guess a letter");
  44.                 letterCheck.addActionListener(this);
  45.                 JTextArea wordIn = new JTextArea(1, 20);
  46.                 JTextArea letterIn = new JTextArea(1, 20);
  47.                 guessBar.add(letterIn, BorderLayout.NORTH);
  48.                 guessBar.add(letterCheck, BorderLayout.SOUTH);
  49.                 main.add(guessBar, BorderLayout.SOUTH);
  50.                 if(guesses == 11)
  51.                 {
  52.                         String path = "hangEleven.jpg";
  53.                         File file = new File(path);
  54.                         BufferedImage image = ImageIO.read(file);
  55.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  56.                         main.add(hangingMan, BorderLayout.CENTER);
  57.                 }
  58.                 else if(guesses == 10)
  59.                 {
  60.                         String path = "hangZero.jpg";
  61.                         File file = new File(path);
  62.                         BufferedImage image = ImageIO.read(file);
  63.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  64.                         main.add(hangingMan, BorderLayout.CENTER);
  65.                 }
  66.                 else if(guesses == 9)
  67.                 {
  68.                         String path = "hangOne.jpg";
  69.                         File file = new File(path);
  70.                         BufferedImage image = ImageIO.read(file);
  71.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  72.                         main.add(hangingMan, BorderLayout.CENTER);
  73.                 }
  74.                 else if(guesses == 8)
  75.                 {
  76.                         String path = "hangTwo.jpg";
  77.                         File file = new File(path);
  78.                         BufferedImage image = ImageIO.read(file);
  79.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  80.                         main.add(hangingMan, BorderLayout.CENTER);
  81.                 }
  82.                 else if(guesses == 7)
  83.                 {
  84.                         String path = "hangThree.jpg";
  85.                         File file = new File(path);
  86.                         BufferedImage image = ImageIO.read(file);
  87.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  88.                         main.add(hangingMan, BorderLayout.CENTER);
  89.                 }
  90.                 else if(guesses == 6)
  91.                 {
  92.                         String path = "hangFour.jpg";
  93.                         File file = new File(path);
  94.                         BufferedImage image = ImageIO.read(file);
  95.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  96.                         main.add(hangingMan, BorderLayout.CENTER);
  97.                 }
  98.                 else if(guesses == 5)
  99.                 {
  100.                         String path = "hangFive.jpg";
  101.                         File file = new File(path);
  102.                         BufferedImage image = ImageIO.read(file);
  103.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  104.                         main.add(hangingMan, BorderLayout.CENTER);
  105.                 }
  106.                 else if(guesses == 4)
  107.                 {
  108.                         String path = "hangSix.jpg";
  109.                         File file = new File(path);
  110.                         BufferedImage image = ImageIO.read(file);
  111.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  112.                         main.add(hangingMan, BorderLayout.CENTER);
  113.                 }
  114.                 else if(guesses == 3)
  115.                 {
  116.                         String path = "hangSeven.jpg";
  117.                         File file = new File(path);
  118.                         BufferedImage image = ImageIO.read(file);
  119.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  120.                         main.add(hangingMan, BorderLayout.CENTER);
  121.                 }
  122.                 else if(guesses == 2)
  123.                 {
  124.                         String path = "hangEight.jpg";
  125.                         File file = new File(path);
  126.                         BufferedImage image = ImageIO.read(file);
  127.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  128.                         main.add(hangingMan, BorderLayout.CENTER);
  129.                 }
  130.                 else if(guesses == 1)
  131.                 {
  132.                         String path = "hangNine.jpg";
  133.                         File file = new File(path);
  134.                         BufferedImage image = ImageIO.read(file);
  135.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  136.                         main.add(hangingMan, BorderLayout.CENTER);
  137.                 }
  138.                 else if(guesses == 1)
  139.                 {
  140.                         String path = "hangTen.jpg";
  141.                         File file = new File(path);
  142.                         BufferedImage image = ImageIO.read(file);      
  143.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  144.                         main.add(hangingMan, BorderLayout.CENTER);
  145.                 }
  146.                 else
  147.                 {
  148.                         String path = "hangTen.jpg";
  149.                         File file = new File(path);
  150.                         BufferedImage image = ImageIO.read(file);      
  151.                         JLabel hangingMan = new JLabel(new ImageIcon(image));
  152.                         main.add(hangingMan, BorderLayout.CENTER);
  153.                 }
  154.                 hangFrame.setSize(400,300);
  155.                 hangFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  156.                 hangFrame.add(main);
  157.                 hangFrame.setVisible(true);
  158.         }
  159.        
  160.         public void actionPerformed(ActionEvent evt)
  161.         {
  162.                 String inputChar = letterIn.getText();
  163.                 Character letterGuess = inputChar.charAt(0);
  164.                 Vector<Integer> indexChar = new Vector<Integer>();
  165.                 indexChar = hangman.checkLetter(letterGuess, selectedWord, selectedWord.length());
  166.                 String newDisguise = hangman.revealWord(indexChar, letterGuess, disguisedWord);
  167.                 if (indexChar.isEmpty())
  168.                 {
  169.                         guesses = guesses - 1;
  170.                 }
  171.                 else
  172.                 {
  173.                         disguisedWord = newDisguise;
  174.                 }
  175.         }
  176. }