/**
   * A recursive function that performs DFS and checks to see if a word is valid or not. Stops when
   * you're out of tiles.
   *
   * <p>TODO Performance Enhancement : Build a suffix tree and check if the node with the suffix arg
   * has any children. That will help skip loads of paths.
   *
   * @param suffix
   * @param coveredPositions
   * @param currentPosition
   */
  private static void solve(
      String suffix, ArrayList<Position> coveredPositions, Position currentPosition) {
    ArrayList<Position> newCoveredPositions = UtilityFunctions.getNewList(coveredPositions);
    newCoveredPositions.add(currentPosition);

    ArrayList<Position> adjacentPositions = currentPosition.getAdjacentPositions();
    for (Position adjacentPosition : adjacentPositions) {
      Boolean isCovered = false;
      for (Position position : newCoveredPositions) {
        if ((position.getX() == adjacentPosition.getX())
            && (position.getY() == adjacentPosition.getY())) {
          isCovered = true;
          break;
        }
      }

      if (isCovered) continue;

      String word = suffix + table[adjacentPosition.getX()][adjacentPosition.getY()];

      if (dictionary.isWordValid(word)) {
        if (!validWords.contains(word)) {
          validWords.add(word);

          ArrayList<Position> tilePositions = UtilityFunctions.getNewList(newCoveredPositions);
          tilePositions.add(adjacentPosition);

          wordTilesMapping.put(word, tilePositions);
        }
      }
      solve(word, newCoveredPositions, adjacentPosition);
    }
  }
  /**
   * Helper function to set an alarm
   *
   * @param minutesFromNow
   * @throws UiObjectNotFoundException
   */
  public void setAlarm(int minutesFromNow) throws UiObjectNotFoundException {

    UiObject setAlarm = new UiObject(new UiSelector().description("Alarms"));
    if (!setAlarm.exists()) setAlarm = new UiObject(new UiSelector().textStartsWith("Set alarm"));
    setAlarm.click();

    UtilityFunctions uti = Controller.utilities;
    // let's add an alarm
    uti.clickByDescription("Add alarm");
    // let's set the time
    // clickByText("Time");

    Calendar c = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("HHmm");
    c.add(Calendar.MINUTE, 1);
    String now = format.format(c.getTime());

    Log.i("ROOT", "alarm time: " + now);
    new UiObject(
            new UiSelector()
                .packageName(packageName)
                .className("android.widget.Button")
                .text("" + now.charAt(0)))
        .click();
    new UiObject(
            new UiSelector()
                .packageName(packageName)
                .className("android.widget.Button")
                .text("" + now.charAt(1)))
        .click();
    new UiObject(
            new UiSelector()
                .packageName(packageName)
                .className("android.widget.Button")
                .text("" + now.charAt(2)))
        .click();
    new UiObject(
            new UiSelector()
                .packageName(packageName)
                .className("android.widget.Button")
                .text("" + now.charAt(3)))
        .click();

    // clickByText("Ok");

    // few confirmations to click thru
    UiObject doneButton = new UiObject(new UiSelector().text("Done"));
    UiObject okButton = new UiObject(new UiSelector().text("OK"));
    // working around some inconsistencies in phone vs tablet UI
    if (doneButton.exists()) {
      doneButton.click();
    } else {
      okButton.click(); // let it fail if neither exists
    }

    // we're done. Let's return to home screen
    // clickByText("Done");
    uti.goToHomeScreen();
  }
Example #3
0
  /**
   * This is the core function of the project. Starts firefox webdriver, goes to the website and
   * initiates a chat. Runs in an infinite loop.
   *
   * @param isOwnerPresent
   * @throws Exception
   */
  public static void startJokeBotChat(Boolean isOwnerPresent, ArrayList<String> topics)
      throws Exception {
    WebHandler webHandler = new WebHandler(ConstantTextStrings.WEBSITE_URL);
    webHandler.startBrowser();
    while (true) {
      String fileName = "convs/" + UtilityFunctions.getCurrentTimeStamp() + ".txt";
      String newMessage = "";

      webHandler.startNewChat(topics);
      webHandler.waitForChatStart();

      try {
        webHandler.sendMessage(ConstantTextStrings.BOT_WELCOME_MESSAGE);

        webHandler.waitForNewMessage();
        newMessage = webHandler.getNewMessage();
        webHandler.sendMessage("Good. Let's get started. The first choice is direct.");

        StoryNode storyNode = null;
        StoryTree storyTree = new StoryTree();
        int answer = 0;
        storyTree.startStory();

        // This loop is the course of the whole chat.
        while (true) {

          storyNode = storyTree.getCurrentStoryNode();
          webHandler.sendMessage(storyNode.getContent());

          if (storyNode.getNodeType() != 1) {
            Boolean shouldRestart = false;
            if (storyNode.getNodeType() == 2) {
              webHandler.sendMessage("Happy Ending ! Well done :)");
            } else {
              webHandler.sendMessage("Bad Ending ! Hard luck :/");
            }
            shouldRestart = stopGameBot(webHandler);
            if (shouldRestart) {
              storyTree.startStory();
              continue;
            }
            break;
          }

          int cnt = 1;
          for (String option : storyNode.getOptions()) {
            webHandler.sendMessage(cnt + ". " + option);
            cnt++;
          }

          while (true) {
            webHandler.sendMessage("Which option do you choose ?");
            webHandler.waitForNewMessage();
            newMessage = webHandler.getNewMessage();
            newMessage = newMessage.replaceAll("[^0-9]", "").trim();
            if (newMessage.equals("")) {
              webHandler.sendMessage(
                  "Invalid option. Enter the number of the option you wish to choose.");
              continue;
            }
            answer = Integer.parseInt(newMessage);
            if (answer > storyNode.getNumberOfOptions()) {
              webHandler.sendMessage("Invalid input. Enter a valid option number.");
              continue;
            }
            break;
          }
          storyTree.advanceStory(answer - 1);
        }

        if (!webHandler.hasDisconnected()) webHandler.disconnect();

      } catch (Exception e) {
        System.out.println(e);
      }

      UtilityFunctions.writeToFile(webHandler.getTranscript(), fileName);
    }
  }