Recreating the integer guessing game

Hi @lenexa_jayhawk , there are quite a few ways to do this in Knime, including using Python.

But based on what you wrote, it looks like you want to “better understand workflow development and understand equivalencies”, meaning you want something without Python, is that correct?

I present to you 2 ways (they are more or less the same - I’ll explain a bit further):
image

First of all, I defined the lower and upper bounds as variables (0 and 100) in case you want to change them when you are playing with the workflow, and this is done via the Variable Creator:
image

The interaction with Widgets is fully appreciated when running on the Front End, that is the web portal, which requires you to run on a Knime server. If you are running the workflow from Knime AP (back end), then it’s better to use Components if you want interactive input. I created a “What is your guess?” component for that.

This is what it looks like when you double click on it:
image

FYI, that Label “What is your guess (random between 0-100)” is dynamic and relies on the minimum and maximum values that you configure. The default value also defaults to the minimum value. Let’s say if you configure the min and max values to be 200 and 300 respectively, the label and default value will change automatically:
image
image

Now, to be able to run anything from this point (even with a Python script), you need to have one dummy record, hence the dummy row.

After that, it’s all about generating random numbers and comparing.

In Method 1, the Math Formula generates the number, the Rule Engine compares and the String Manipulation generates the correct wording for the results.

Basically in Method 2, all of the Method 1 is done via the Column Expression. Column Expression allows you to evaluate If statements and do manipulation all in the same node.

Here’s the workflow: Integer guessing game.knwf (23.3 KB)

EDIT: I added a piece of Java Snippet to popup the result, which may be a bit more fun:
image

You can attach the Java Snippet with whichever method you want to use.

This is what the popup looks like:
image

So basically to play the game:

  1. Set up your min and max (should not change frequently and currently set at 0 and 100)
  2. Take a guess via the Component
  3. Run the Java Snippet

Here’s the workflow with the popup: Integer guessing game.knwf (25.3 KB)

4 Likes