Casino Game Tutorial

Learn Pulse basics by creating a simple process.

In this tutorial, you will create a generic task that generates a random number between one and six via a python script.

You will create a process involving the previously created generic task and import an in task from the library. You will modify the generic task to add a user input variable and connect it to the if task so it can determine if you have won or lost.

Before you begin the tutorial, download the Pulse folder with the tutorial materials.

Create Dice

  1. Define the Library.
    1. From the menu bar, select File > Preferences > Library to select the pulse folder as the library.
  2. Create a generic task.
    1. From the Author ribbon, Create group, select the Generic Task tool.
      The GenericTask dialog opens and a generic task is created in the Block Diagram.
    2. In the GenericTask dialog, enter Dice for Name.
    3. For Application, select Python.
    4. In the Task tab, select Script.
    5. For Script language, select Python.
    6. For Script name, browse and select the Python script located in the pulse\casino_game\Script folder.
    7. In the Variables tab, click .
    8. Click the newly created study to populate the Preview.
    9. For Display name, enter Dice.
    10. For Variable name, enter dice.
    11. Verify the Type is set to Scalar and Value type is set to Integer.
    12. For Originate from, select Task.


      Figure 1.
    13. Close the GenericTask dialog.
  3. Link the script to Pulse using script editor.
    1. In the Block Diagram, right-click the generic task and select Edit Script from the context menu.
      The Pulse Script Editor dialog opens and displays the Python script.
    2. Before the Python script, enter the following Process API Command:
      #Import Pulse
      apidir = os.environ[‘ALTAIR_PULSE_API’]
      path = os.path.join(apidir,"python")
      sys.path.append(path)
      import pulse
      


      Figure 2.
      Note: Learn more about Process API Commands.
    3. After the Python script, enter the pulse.setVariableValue command.
    4. Enter the following inputs:
      • Name of the Pulse variable: 'dice'
      • The value it's going to take: str(rand)


      Figure 3.
      The command should read as pulse.setVariableValue ('dice', str(rand)).
    5. Click and save the updated script.
    6. Close the Pulse Script Editor dialog.
      The setVariableValue command gives the dice variable the value of the random number generated by randint.
  4. Define the output in the Block Diagram.
    1. In the Block Diagram, right-click the output port of the Dice generic task and select Edit from the context menu.
      A dialog opens.
    2. In the dialog, enable Dice (dice).
    3. Close the dialog.
    4. In the Block Diagram, right-click the input port of the Dice generic task and select Delete from the context menu.
  5. Execute a task.
    1. From the Author ribbon, Execute group, click the Run tool.
    Note: The variable value changes each time the task is run.
  6. Export a task to library.
    1. In the Block Diagram, right-click the generic task and select Export to Library from the context menu.
      The Save File dialog opens.
    2. Select Task folder.
    3. In the Save File dialog, save the task as dice.

Create Game

  1. From the Tasks folder inside the Library, drag-and-drop the Dice generic task into the work area.
  2. From the Tasks folder inside the Library, drag-and-drop the Equals If task into the Block Diagram.
    Note: The If task evaluates if user input a and user input b are equal. This can be viewed in the logical condition.
  3. Connect tasks.
    1. Optional: If necessary, adjust the locations of the tasks in the Block Diagram so they can easily be connected.
    2. In the Block Diagram, connect the output port of Dice to the input port of Equals.


      Figure 4.
    3. Right-click the connection and select Edit from the context menu.
      The Link dialog opens.
    4. In the Link dialog, Output column, select Dice (dice) for a (a) input.
      Note: The Value/Expression should be populated with =Dice_1_dice. This means that input a is equal to the output variable dice of instance 1 of the Dice module. One must specify the instance number because multiple modules, in this case Dice, could been present in the process. This is called Local Variable Mapping.
    5. Close the Link dialog.
  4. Modify Widget.
    1. In the Block Diagram, right-click the Equals task and select Edit from the context menu.
      The If Task dialog opens.
    2. In the Variables tab of the If Task dialog, select the b variable.
    3. In the GUI Description, select Combobox for Widget.
    4. For Allowables, enter 1 2 3 4 5 6.


      Figure 5.
    5. For variable a, change the Display Name to Dice Number and for variable b, change Display Name to Lucky Number.
  5. Connect tasks.
    1. From the Tasks folder inside the Library, drag-and-drop the Win and Lose tasks into the work area.
    2. In the Block Diagram, connect the output ports of Equals to the input ports of Win and Lose.


    Figure 6.
  6. Execute process.
    1. From the Author ribbon, Execute group, click the Run tool.
      The Equals dialog opens.
    2. In the Equals dialog, select a number for Lucky Number.
    3. Click Proceed.
    The values are updated in the Variable Browser.
  7. Export the process to library.
    1. In the Block Diagram, right-click and select Export to Library from the context menu.
      The Save File dialog opens.
    2. In the Save File dialog, save the process as Casino.