Advertisement
What_Ever

Untitled

Mar 1st, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.hangman.cs69;
  2. import java.util.Random;
  3. import eg.edu.alexu.csd.datastructure.hangman.IHangman;
  4. public class Hangman implements IHangman {
  5.     String[] dict = new String[1500];
  6.     int arrSize = 0, maxGuesses;
  7.     int wrongGuesses = 0;
  8.     static String word = "",sec = "";
  9.     static String res = "";
  10.     @Override
  11.     public void setDictionary(String[] words) {
  12.         for(String i : words )
  13.         {
  14.             dict[arrSize++] = i;
  15.         }
  16.     }
  17.  
  18.     @Override
  19.     public String selectRandomSecretWord() {
  20.         Random ran = new Random();
  21.         int index = ran.nextInt(arrSize);
  22.         word = dict[index].toUpperCase();
  23.         sec = dict[index];
  24.         int len = word.length();
  25.         for(int i = 0 ; i < len ; i++)res+='-';
  26.         return dict[index];
  27.     }
  28.  
  29.     @Override
  30.     public String guess(Character c) {
  31.         char[] temp;
  32.         if(c == null)return res;
  33.         if(word.indexOf(c) == -1)
  34.         {
  35.             wrongGuesses++;
  36.             if(wrongGuesses == maxGuesses)return null;
  37.             return res;
  38.         }
  39.         else
  40.         {
  41.             int pos = 0;
  42.             while(word.indexOf(c) != -1)
  43.             {
  44.                 pos = word.indexOf(c);
  45.                 temp = res.toCharArray();
  46.                 temp[pos] = sec.charAt(pos);
  47.                 res = String.valueOf(temp);
  48.                 temp = word.toCharArray();
  49.                 temp[pos] = '-';
  50.                 word = String.valueOf(temp);
  51.             }
  52.             return res;
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public void setMaxWrongGuesses(Integer max) {
  58.         if(max == null){maxGuesses = 0;
  59.         return;
  60.     }
  61.         maxGuesses = max;
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement