Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.hangman.cs01;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Random;
- import eg.edu.alexu.csd.datastructure.hangman.IHangman;
- /**
- * @author abanoub ashraf zaki
- *
- *
- */
- public class Hangman implements IHangman {
- private String sword;
- private int remain = -1;
- private int dashesleft = 0;
- private String tries;
- private String[] dictionary;
- StringBuilder dashed = new StringBuilder("");
- public int read() {
- // TODO Auto-generated method stub
- BufferedReader in = null;
- int counter = 0;
- ArrayList<String> dic = new ArrayList<String>();
- try {
- in = new BufferedReader(new FileReader("input.txt"));
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- String str;
- try {
- while ((str = in.readLine()) != null) {
- dic.add(str);
- counter++;
- }
- for (int i = 0; i < counter; i++) {
- dictionary[i] = dic.get(i);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return counter;
- }
- @Override
- public void setDictionary(final String[] words) {
- // TODO Auto-generated method stub
- dictionary = words;
- /*String s="";
- for(int i=0;i<dictionary.length;i++) {
- s +=dictionary[i];
- }
- throw new RuntimeException(s);*/
- }
- @Override
- public String selectRandomSecretWord() {
- // TODO Auto-generated method stub
- if (dictionary.length == 0) {
- return null;
- } else {
- Random random = new Random();
- int randomInt = random.nextInt(dictionary.length);
- sword = dictionary[randomInt];
- if (sword.isEmpty() || sword.contains(" ")) {
- throw new RuntimeException("");
- } else {
- int i = 0;
- dashed.setLength(sword.length());
- while (i < sword.length()) {
- dashed.setCharAt(i, '-');
- i++;
- }
- tries = dashed.toString();
- dashesleft = tries.length();
- }
- }
- return sword;
- }
- @Override
- public String guess(final Character c) {
- // TODO Auto-generated method stub
- String ch=String.valueOf(c);
- throw new RuntimeException(ch);
- /*Boolean found1 = false;
- Boolean found2 = false;
- if (sword == null) {
- throw new RuntimeException("");
- } else if (remain < 0) {
- throw new RuntimeException("");
- } else if (remain > 0 && dashesleft > 0) {
- if (c == null) {
- return tries;
- } else {
- int i = 0;
- while (i < sword.length()) {
- if (Character.toLowerCase(c) == Character.toLowerCase(sword.charAt(i))) {
- dashed.setCharAt(i, sword.charAt(i));
- found1 = true;
- }
- i++;
- }
- i = 0;
- while (i < tries.length()) {
- if (Character.toLowerCase(c) == Character.toLowerCase(tries.charAt(i))) {
- found2 = true;
- }
- i++;
- }
- if (!found1 && !found2) {
- remain--;
- }
- dashesleft = 0;
- tries = dashed.toString();
- i = 0;
- while (i < tries.length()) {
- if (tries.charAt(i) == '-') {
- dashesleft++;
- }
- i++;
- }
- }
- }
- if (remain == 0 || dashesleft == 0) {
- return null;
- }
- return tries;*/
- }
- @Override
- public void setMaxWrongGuesses(final Integer max) {
- // TODO Auto-generated method stub
- throw new RuntimeException(max.toString());
- /*if (max == null) {
- remain = 1;
- } else {
- remain = max;
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement