Advertisement
Guest User

20 Questions

a guest
Mar 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. while(!temp.isLeaf()) {
  2. System.out.print(temp.root + "? ");
  3. String rootAnswer = scanner.nextLine();
  4. if (rootAnswer.equals("Yes")) {
  5. //add variables to differentiate
  6. temp = temp.getRightSubtree();
  7. System.out.print(temp.getData() + "? ");
  8. String firstRightLeaf = scanner.nextLine();
  9.  
  10. if (firstRightLeaf.equals("Yes") && temp.getRightSubtree() == null && temp.getLeftSubtree() == null) {
  11. System.out.println("I guessed it!");
  12. System.out.print("Would you like to play again? ");
  13. String yesOrno = scanner.nextLine();
  14. if (yesOrno.equals("Yes"))
  15. temp = bt;
  16. else
  17. break;
  18. }
  19.  
  20. else if (firstRightLeaf.equals("No") && temp.getRightSubtree() == null && temp.getLeftSubtree() == null) {
  21. System.out.print("What is your animal? ");
  22. String animal = scanner.nextLine();
  23. System.out.print("Provide a question that uniquely identifies your animal? ");
  24. String question = scanner.nextLine();
  25. BinaryTree<String> save = bt;
  26. temp = bt.getRightSubtree();
  27. temp.setData(question);
  28. temp.setLeftSubtree(save);
  29. BinaryTree<String> animal2 = new BinaryTree<>(animal);
  30. temp.setRightSubtree(animal2);
  31.  
  32. System.out.print("Would you like to play again? ");
  33. String yesOrno = scanner.nextLine();
  34. if (yesOrno.equals("Yes"))
  35. temp = bt;
  36. else
  37. break;
  38. }
  39.  
  40. else if(firstRightLeaf.equals("Yes") && !temp.isLeaf()) {
  41.  
  42. }
  43.  
  44. else if(firstRightLeaf.equals("No") && !temp.isLeaf()) {
  45. System.out.println(temp.getLeftSubtree().getData() + "? ");
  46. }
  47. }
  48.  
  49. else if (rootAnswer.equals("No")) {
  50. temp = bt.getLeftSubtree();
  51. System.out.print(temp.getData() + "? ");
  52. String firstLeftLeaf = scanner.nextLine();
  53. if (firstLeftLeaf.equals("Yes") && temp.isLeaf()) {
  54. System.out.println("I guessed it!");
  55. System.out.print("Would you like to play again? ");
  56. String yesOrno = scanner.nextLine();
  57. if (yesOrno.equals("Yes"))
  58. temp = bt;
  59. else
  60. break;
  61. }
  62. else if (firstLeftLeaf.equals("No") && temp.isLeaf()) {
  63. System.out.print("What is your animal? ");
  64. String animal = scanner.nextLine();
  65. System.out.print("Provide a question that uniquely identifies your animal? ");
  66. String question = scanner.nextLine();
  67. BinaryTree<String> save = bt;
  68. temp = bt.getLeftSubtree();
  69. temp.setData(question);
  70. temp.setLeftSubtree(save);
  71. BinaryTree<String> animal2 = new BinaryTree<>(animal);
  72. temp.setRightSubtree(animal2);
  73.  
  74. System.out.print("Would you like to play again? ");
  75. String yesOrno = scanner.nextLine();
  76. if (yesOrno.equals("Yes"))
  77. temp = bt;
  78. else
  79. break;
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement