Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6.  
  7. /**
  8. * The Class AnswerBank.
  9. * Gage Law
  10. * CS160 Project 4
  11. */
  12. public class AnswerBank {
  13.  
  14.  
  15. // The answers.
  16. ArrayList<String> answers = new ArrayList<String>();
  17.  
  18.  
  19. // Instantiates a new answer bank.
  20. // filename the filename
  21.  
  22. public AnswerBank( String filename ) {
  23.  
  24.  
  25. try (FileReader reader = new FileReader( filename );
  26.  
  27. BufferedReader br = new BufferedReader(reader)) {
  28.  
  29. // read line by line
  30. String line;
  31. while ((line = br.readLine()) != null) {
  32. answers.add( line );
  33. }
  34.  
  35. } catch (IOException e) {
  36. System.err.format("IOException: %s%n", e);
  37. }
  38.  
  39. }
  40.  
  41.  
  42. //Gets the random answer.
  43. //the random answer
  44.  
  45. public String getRandomAnswer() {
  46.  
  47. // Random generator variable.
  48. Random rand = new Random();
  49. int n = rand.nextInt(answers.size());
  50. return answers.get(n);
  51. }
  52.  
  53. }
  54. package lawg_project4;
  55.  
  56. import java.util.ArrayList;
  57. import java.util.Scanner;
  58.  
  59. /
  60. * The Class GameManager.
  61. * Gage Law
  62. * CS160 Project 4
  63. */
  64.  
  65.  
  66.  
  67. public class GameManager {
  68.  
  69.  
  70. // The num puzzles solved.
  71. int numPuzzlesSolved;
  72.  
  73. // The puzzle history.
  74. ArrayList<PuzzleClass> puzzlehistory;
  75.  
  76. / The bank. */
  77. AnswerBank bank;
  78.  
  79. /** The input. */
  80. static Scanner input = new Scanner(System.in);
  81.  
  82.  
  83.  
  84.  
  85. // Instantiates a new game manager.
  86. // filename the filename
  87.  
  88. public GameManager( String filename ) {
  89.  
  90. numPuzzlesSolved =0;
  91. puzzlehistory = new ArrayList<>();
  92. bank = new AnswerBank( filename );
  93.  
  94. }
  95.  
  96.  
  97. //run
  98. public void run() {
  99.  
  100. String ch = null;
  101. double percent = 0;
  102. do {
  103. System.out.print("Would you like to play Hangman (yes/no)? ");
  104. ch = input.nextLine() ;
  105.  
  106. if(ch.equalsIgnoreCase("yes")) {
  107.  
  108. PuzzleClass puzzle = new PuzzleClass ( bank.getRandomAnswer() );
  109. puzzle.run(input);
  110. numPuzzlesSolved = puzzle.get_num_puzzled_solved() ;
  111. puzzlehistory.add( puzzle );
  112. // if user lose
  113. // then percentage of correct guesses
  114. // if its above 60 % ask again for play otherwise
  115. // close program
  116. percent = ( (double) numPuzzlesSolved / (double) puzzle.get_num_of_puzzles()) * 100.0;
  117. }
  118.  
  119. }while( percent >= 60.0 && ch.equalsIgnoreCase("yes") );
  120. }
  121.  
  122.  
  123. // The main method.
  124. // args the arguments
  125.  
  126. public static void main(String[] args) {
  127.  
  128. GameManager manager = new GameManager("phrases.txt");
  129. manager.run();
  130.  
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement