Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.hangman.cs69;
- import java.util.Random;
- import eg.edu.alexu.csd.datastructure.hangman.IHangman;
- public class Hangman implements IHangman {
- String[] dict = new String[1500];
- int arrSize = 0, maxGuesses;
- int wrongGuesses = 0;
- static String word = "",sec = "";
- static String res = "";
- @Override
- public void setDictionary(String[] words) {
- for(String i : words )
- {
- dict[arrSize++] = i;
- }
- }
- @Override
- public String selectRandomSecretWord() {
- Random ran = new Random();
- int index = ran.nextInt(arrSize);
- word = dict[index].toUpperCase();
- sec = dict[index];
- int len = word.length();
- for(int i = 0 ; i < len ; i++)res+='-';
- return dict[index];
- }
- @Override
- public String guess(Character c) {
- char[] temp;
- if(c == null)return res;
- if(word.indexOf(c) == -1)
- {
- wrongGuesses++;
- if(wrongGuesses == maxGuesses)return null;
- return res;
- }
- else
- {
- int pos = 0;
- while(word.indexOf(c) != -1)
- {
- pos = word.indexOf(c);
- temp = res.toCharArray();
- temp[pos] = sec.charAt(pos);
- res = String.valueOf(temp);
- temp = word.toCharArray();
- temp[pos] = '-';
- word = String.valueOf(temp);
- }
- return res;
- }
- }
- @Override
- public void setMaxWrongGuesses(Integer max) {
- if(max == null){maxGuesses = 0;
- return;
- }
- maxGuesses = max;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement