Advertisement
Arush22

Untitled

Feb 18th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. Game play Object Creation:
  2.  
  3. package com.company;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.*;
  7. import java.lang.*;
  8. public class FinalProject {
  9.  
  10. //Initialize the score
  11. public static int score = 0;
  12.  
  13. public static void GamePlay(Scanner input) throws IOException {
  14. //Creates an array with all the words in the dictionary
  15. Scanner inputFile = new Scanner(new File("C:\\Users\\Arush Adabala\\Desktop\\Dictionary.txt"));
  16. String st = "";
  17. while (inputFile.hasNext()){
  18. st= st + " " +inputFile.next();
  19. }
  20. String s = st;
  21. String[] words = s.split("\\s+");
  22. for( int i = 0; i<words.length; i++)
  23.  
  24. {
  25. words[i] = words[i].replaceAll("[^\\w]", "");
  26. }
  27.  
  28. input = new Scanner(System.in);
  29.  
  30.  
  31. // Generate two random letters.
  32. Random rand = new Random();
  33. char firstLetter = (char) (rand.nextInt(26) + 'A');
  34. char secondLetter = (char) (rand.nextInt(26) + 'A');
  35. System.out.println("First letter = " + firstLetter + " Last letter = " + secondLetter);
  36.  
  37.  
  38. //User types in a word
  39. System.out.println("Type a word");
  40. String typedWord = input.next();
  41. String checkingWord = typedWord.toUpperCase();
  42. char letter1 = checkingWord.charAt(0);
  43. char letter2 = checkingWord.charAt(typedWord.length() - 1);
  44.  
  45. //Checks if the first and last letter match
  46. if (letter1 == firstLetter && letter2 == secondLetter) {
  47. for(int i = 0; i < 58110; i++) {
  48. //Checks if the word is in the dictionary.
  49. if (typedWord.equalsIgnoreCase(words[i])) {
  50. System.out.println("Correct");
  51. score++;
  52. }
  53. }
  54.  
  55. } else {
  56. System.out.println("Incorrect");
  57. score--;
  58. }
  59.  
  60. System.out.println("Your score is " + score);
  61.  
  62.  
  63. }
  64. }
  65.  
  66.  
  67.  
  68.  
  69. Level Object Creation:
  70.  
  71. package com.company;
  72.  
  73. public class FinalProjectLevel {
  74. private int level;
  75. public FinalProjectLevel() //Default constructor
  76. {
  77. level = 0;
  78. }
  79. public FinalProjectLevel(int level1) //Default constructor
  80. {
  81. level = level1;
  82. }
  83. public void setLevel(int level1)
  84. {
  85. level = level1;
  86. }
  87.  
  88. public int getLevel()
  89. {
  90. return level;
  91. }
  92. public String toString()
  93. {
  94. return "("+level+")";
  95. }
  96. }
  97.  
  98.  
  99. Object Client Class:
  100.  
  101. package com.company;
  102.  
  103. import java.io.IOException;
  104. import java.util.*;
  105. public class FinalProjectClient {
  106. public static void main(String[] args)throws IOException {
  107. System.out.println("Type in a word that matches the first and last letter given to you.");
  108. System.out.println("If you type in a word that doesn't match the first and letter you lose a point");
  109. System.out.println("You don't lose or gain any points for typing in a word that doesn't exist.");
  110. System.out.println("You gain a point for every word you get correct. You go up a commitment level for every game you play.");
  111. // Create a variable to increment the number of games played.
  112. int playcounter = 0;
  113. //Create level object.
  114. FinalProjectLevel level = new FinalProjectLevel();
  115.  
  116. //Output your initial level.
  117. System.out.println("Your level currently is "+level.toString());
  118.  
  119. // Create scanner
  120. Scanner input = new Scanner(System.in);
  121.  
  122. //Create variable needed to keep playing
  123. String UserContinuityStatus = "";
  124.  
  125. // Create the main Game object.
  126. FinalProject main = new FinalProject();
  127.  
  128. while (!UserContinuityStatus.equalsIgnoreCase("no")) {
  129. //Start the Game.
  130. main.GamePlay(input);
  131.  
  132. //Increment the number of games played
  133. playcounter++;
  134.  
  135. //Ask the User if he wants to keep playing.
  136. System.out.println("Do you want to keep playing (yes/no)");
  137. UserContinuityStatus = input.next();
  138. }
  139. level.setLevel(playcounter);
  140. System.out.println("Your commitment level is "+level.toString());
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement