Advertisement
Guest User

hangman

a guest
Feb 21st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package eg.edu.alexu.csd.datastructure.hangman.cs01;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Random;
  9. import eg.edu.alexu.csd.datastructure.hangman.IHangman;
  10.  
  11. /**
  12. * @author abanoub ashraf zaki
  13. *
  14. *
  15. */
  16. public class Hangman implements IHangman {
  17.  
  18. private String sword;
  19. private int remain = -1;
  20. private int dashesleft = 0;
  21. private String tries;
  22. private String[] dictionary;
  23. StringBuilder dashed = new StringBuilder("");
  24. public int read() {
  25. // TODO Auto-generated method stub
  26. BufferedReader in = null;
  27. int counter = 0;
  28. ArrayList<String> dic = new ArrayList<String>();
  29. try {
  30. in = new BufferedReader(new FileReader("input.txt"));
  31. } catch (FileNotFoundException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. }
  35. String str;
  36. try {
  37. while ((str = in.readLine()) != null) {
  38. dic.add(str);
  39. counter++;
  40. }
  41. for (int i = 0; i < counter; i++) {
  42. dictionary[i] = dic.get(i);
  43. }
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. return counter;
  49. }
  50. @Override
  51. public void setDictionary(final String[] words) {
  52. // TODO Auto-generated method stub
  53. dictionary = words;
  54. /*String s="";
  55. for(int i=0;i<dictionary.length;i++) {
  56. s +=dictionary[i];
  57. }
  58. throw new RuntimeException(s);*/
  59. }
  60. @Override
  61. public String selectRandomSecretWord() {
  62. // TODO Auto-generated method stub
  63. if (dictionary.length == 0) {
  64. return null;
  65. } else {
  66. Random random = new Random();
  67. int randomInt = random.nextInt(dictionary.length);
  68. sword = dictionary[randomInt];
  69. if (sword.isEmpty() || sword.contains(" ")) {
  70. throw new RuntimeException("");
  71. } else {
  72. int i = 0;
  73. dashed.setLength(sword.length());
  74. while (i < sword.length()) {
  75. dashed.setCharAt(i, '-');
  76. i++;
  77. }
  78. tries = dashed.toString();
  79. dashesleft = tries.length();
  80. }
  81. }
  82. return sword;
  83. }
  84. @Override
  85. public String guess(final Character c) {
  86. // TODO Auto-generated method stub
  87. String ch=String.valueOf(c);
  88. throw new RuntimeException(ch);
  89. /*Boolean found1 = false;
  90. Boolean found2 = false;
  91. if (sword == null) {
  92. throw new RuntimeException("");
  93. } else if (remain < 0) {
  94. throw new RuntimeException("");
  95. } else if (remain > 0 && dashesleft > 0) {
  96. if (c == null) {
  97. return tries;
  98. } else {
  99. int i = 0;
  100. while (i < sword.length()) {
  101. if (Character.toLowerCase(c) == Character.toLowerCase(sword.charAt(i))) {
  102. dashed.setCharAt(i, sword.charAt(i));
  103. found1 = true;
  104. }
  105. i++;
  106. }
  107. i = 0;
  108. while (i < tries.length()) {
  109. if (Character.toLowerCase(c) == Character.toLowerCase(tries.charAt(i))) {
  110. found2 = true;
  111. }
  112. i++;
  113. }
  114. if (!found1 && !found2) {
  115. remain--;
  116. }
  117. dashesleft = 0;
  118. tries = dashed.toString();
  119. i = 0;
  120. while (i < tries.length()) {
  121. if (tries.charAt(i) == '-') {
  122. dashesleft++;
  123. }
  124. i++;
  125. }
  126. }
  127. }
  128. if (remain == 0 || dashesleft == 0) {
  129. return null;
  130. }
  131. return tries;*/
  132. }
  133. @Override
  134. public void setMaxWrongGuesses(final Integer max) {
  135. // TODO Auto-generated method stub
  136. throw new RuntimeException(max.toString());
  137. /*if (max == null) {
  138. remain = 1;
  139. } else {
  140. remain = max;
  141. }*/
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement