Doublets - Main


/ Published in: Java
Save to your folder(s)

Doublets Project


Copy this code and paste it in your HTML
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6. Scanner input = new Scanner(System.in); // Scanner used to read from
  7. // dictionary
  8. Doublets doublets; // Initialize the doublets
  9.  
  10. // Variable input so user selects dictionary.
  11. System.out.println("Welcome to Doublets.");
  12. System.out
  13. .println("Enter a dictionary number (10, 20, 35, 40, 50, 55, 60, 70, 80, 95): ");
  14. String dictNumber = input.nextLine();
  15. String filename = "english.cleaned.all." + dictNumber + ".txt";
  16. doublets = new Doublets(new Links(filename));
  17.  
  18. // Continue game until user specifies to quit with an 'x' key stroke.
  19. while (true) {
  20. System.out.println("Enter starting word: ");
  21. String start = input.nextLine();
  22. start = start.trim();
  23. System.out.println("Enter ending word: ");
  24. String end = input.nextLine();
  25. end = end.trim();
  26. // Check to make sure words are valid.
  27. if (!doublets.isInDictionary(start)) {
  28. System.out.println("The word " + start + " is invalid.");
  29. } else if (!doublets.isInDictionary(end)) {
  30. System.out.println("The word " + end + " is invalid.");
  31.  
  32. } else if (start.length() != end.length()) {
  33. System.out
  34. .println("The words need to be the same length. Try again!");
  35. } else if (start.equals(end)) {
  36. System.out.println("Those are the same word!");
  37. } else {
  38. // Select the chain manager
  39. ChainManager manager;
  40. while (true) {
  41. System.out
  42. .println("Enter chain manager (s: stack, q: queue, p: priority queue, x: exit): ");
  43. String m = input.nextLine();
  44. if (m.equals("s")) {
  45. manager = new StackChainManager();
  46. break;
  47. } else if (m.equals("q")) {
  48. manager = new QueueChainManager();
  49. break;
  50. } else if (m.equals("p")) {
  51. manager = new PriorityQueueChainManager(end);
  52. break;
  53. } else if (m.equals("x")) {
  54. System.out.println("Thanks for playing!");
  55. input.close();
  56. System.exit(0);
  57. } else {
  58. System.out.println("Invalid input. Try again.");
  59. }
  60. }
  61. // Find the chain
  62. Chain chain = doublets.getChain(start, end, manager);
  63. // If there is no solution
  64. if (chain == null)
  65. System.out.println("no solution");
  66. else {
  67. System.out.println(chain.toString());
  68. System.out.println("Length: " + chain.length());
  69. System.out.println("Candidates: "
  70. + manager.getNumberOfNexts());
  71. System.out.println("Max Size: " + manager.maxSize() + "\n");
  72. }
  73. }
  74.  
  75. }
  76.  
  77. }
  78.  
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.