reathh

HangMan BG Jsoup

May 18th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. import java.awt.Graphics;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Scanner;
  10.  
  11. import org.jsoup.Jsoup;
  12. import org.jsoup.nodes.Document;
  13. import org.jsoup.nodes.Element;
  14.  
  15.  
  16. public class HangManLogic {
  17.  
  18.     static Graphics g;
  19.     static  String realWord;
  20.     static char[] dots;
  21.     static StringBuffer guessWord;
  22.     static List<Character> usedChars = new ArrayList<Character>();
  23.     static String message;
  24.     static byte MAX_MISTAKES = 50;
  25.     static byte mistakes;
  26.     static boolean endIt = false;
  27.    
  28.     public static void main(String[] args) throws IOException {
  29.        
  30.         //First we initialize the game with InitGame()
  31.         //Then we call ProcessTurn(userInput) after a button is clicked
  32.         Scanner in = new Scanner(System.in);
  33.     InitGame();
  34.     while (true) {
  35.         String s = in.nextLine();
  36.         ProcessTurn(s);
  37.         paint(g);
  38.         if (endIt)
  39.             break;
  40.     }
  41. // 
  42.     }
  43.    
  44.     //Start a new game
  45.     private static void InitGame() throws IOException {
  46.             //Set errors to 0
  47.         mistakes = 0;
  48.             //Set a new game (Forget about last win/lose)
  49.         endIt = false;
  50.     //Get a random word using a GET request to an API
  51.              realWord = GetENWord().toLowerCase();
  52.            
  53.             //Make a masked with dots word that we can show to the user
  54.             dots = new char[realWord.length()];
  55.             for (int i = 0; i < dots.length; i++) {
  56.                 dots[i] = '*';
  57.             }
  58.             //Show the user the masked word (We should edit a Label in the GUI)
  59.             System.out.println(dots);
  60.            
  61.             //Make a StringBuffer which we can later edit when we have a guess from the user (It firstly is the same as the mask word)
  62.             String s = new String(dots);
  63.             guessWord = new StringBuffer(s);
  64.            
  65.             message = "";
  66.             usedChars.clear();
  67.     }
  68.    
  69.     //Call this method with userInput = TextBoxWithLetter.getText()
  70.     private static void ProcessTurn(String userInput) {
  71.        
  72.         char inputChar = userInput.toLowerCase().charAt(0);
  73.        
  74.        
  75.         //Check if the user input is a letter
  76.         if (!Character.isAlphabetic(inputChar)) {
  77.             message = "Input must be a letter";
  78.             return;
  79.         }
  80.         //Check if the user input is more than 1 letter
  81.         if (userInput.length() > 1) {
  82.             message = "Input must be 1 letter only";
  83.             return;
  84.         }
  85.         //Check if user input isn't already used
  86.         if (usedChars.contains(inputChar)) {
  87.             message = "Letter already used";
  88.             return;
  89.         }
  90.         //Check if user hasn't already guessed this letter
  91.         if (new String(guessWord).indexOf(inputChar) != -1) {
  92.             message = "Letter has been already guessed";
  93.             return;
  94.         }
  95.        
  96.        
  97.         //If user didn't guess a letter
  98.         if (realWord.indexOf(inputChar) == -1) {
  99.             message = "";
  100.             mistakes++;
  101.             usedChars.add(inputChar);
  102.         }
  103.         //User guessed
  104.         if (realWord.indexOf(inputChar) != -1) {
  105.             for (int i = 0; i < realWord.length(); i++) {
  106.                 if (realWord.charAt(i) == inputChar) {
  107.                     guessWord.setCharAt(i, inputChar);
  108.                 }
  109.             }
  110.         }
  111.        
  112.         //Check if max mistakes are reached
  113.                 if (mistakes == MAX_MISTAKES) {
  114.                     message = "You lost! The word you were searching for was: " + realWord + "\nClick \"Start New Game\"";
  115.                     endIt = true;
  116.                     return;
  117.                 }
  118.                 //Check if word is guessed
  119.                 if (guessWord.indexOf("*") == -1) {
  120.                     message = "You won! Click \"Start New Game\"";
  121.                     endIt = true;
  122.                     return;
  123.                 }
  124.        
  125.     }
  126.    
  127.    
  128.     //Paint some stuff
  129.       public static void paint(Graphics g) {
  130.            
  131.           //If you want more or less errors just change variable MAX_ERRORS and edit the below ifs (add or remove)
  132.  
  133.           if (mistakes >  0){    
  134.             //Show a picture
  135.               //g.DrawImage(pictureIllustrating 1 mistake)
  136.           }
  137.           if (mistakes ==  1){  
  138.             //Show a picture
  139.           }
  140.           if (mistakes == 2){
  141.             //Show a picture
  142.           }
  143.           if (mistakes == 3){
  144.             //Show a picture
  145.           }
  146.           if (mistakes == 4){    
  147.             //Show a picture
  148.           }
  149.           if (mistakes == 5){
  150.             //Show a picture
  151.           }
  152.           if (mistakes == 6){    
  153.             //Show a picture
  154.           }
  155.           if (mistakes == 7){  
  156.             //Show a picture
  157.           }
  158.           if (mistakes == 8){
  159.             //Show a picture
  160.           }
  161.           if (mistakes == 9){    
  162.             //Show a picture
  163.           }
  164.           if (mistakes == 10){
  165.             //Show a picture
  166.           }
  167.           if (mistakes == 11){    
  168.             //Show a picture
  169.           }
  170.           if (mistakes == 12){
  171.             //Show a picture
  172.           }
  173.          
  174.           // Draw the messages(Already used letter; More than one letter; etc..) + the masked word + all used chars
  175. //          g.drawString( message, x, y );
  176. //          g.drawString( new String (guessWord), x, y);
  177. //         
  178. //              for(Character usedchar : usedChars){
  179. //          g.drawString(usedchar + " ", x, y);
  180. //              }
  181.           System.out.println(message);
  182.           System.out.println(guessWord);
  183. //      for(Character usedchar : usedChars){
  184. //      System.out.println(usedchar + " ");
  185. //      }
  186.         System.out.println(usedChars + " ");
  187.         System.out.println("Mistakes: " + mistakes);
  188.  
  189.   }
  190.    
  191.    
  192.         private static String GetENWord() {
  193.               URL url;
  194.               HttpURLConnection conn;
  195.               BufferedReader rd;
  196.               String line;
  197.               String result = "";
  198.               try {
  199.                  url = new URL("http://randomword.setgetgo.com/get.php");
  200.                  conn = (HttpURLConnection) url.openConnection();
  201.                  conn.setRequestMethod("GET");
  202.                  rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  203.                  while ((line = rd.readLine()) != null) {
  204.                     result += line;
  205.                  }
  206.                  rd.close();
  207.               } catch (IOException e) {
  208.                  e.printStackTrace();
  209.               } catch (Exception e) {
  210.                  e.printStackTrace();
  211.               }
  212.               return result;
  213.            }
  214.         private static String GetBGWord() throws IOException {
  215.             Element word;
  216.             do {
  217.             Document doc = Jsoup.connect("http://rechnik.chitanka.info/random?").get();
  218.              word = doc.getElementById("first-heading");
  219.        
  220.             } while (word.text().indexOf(" ") != -1);
  221.                 return word.text();
  222.         }
  223.  
  224. }
Advertisement
Add Comment
Please, Sign In to add comment