Python bot bot

I am working on textual RPG in Python, but I am stuck in NPC. I need something like a tree. For instance:

  Bot: hello there.
 1. Hi, what is your name?
 2. Hi, Where do you live?
 > 2
 Player: Where do you live?
 Bot: That a creepy question.  Why do you ask?
 1. I was just curious.
 2. Never mind.
 3. Hide yo kids, hide yo wife
 > 1
 Player: I was just curious.
 Bot: Okay.
 1. Do you like cheese?
 2. Bye.
 > 1
 Player: Do you like cheese?
 Bot: Yes!  I love cheese.

Each choice you make will fall back on other options, AKA if the user answered β€œ1” to the first question, the bot would answer β€œMy name is Bob. What's yours?”

My game is designed in such a way that the level editor will not be an impossible prospect (every place you can visit is stored as a key in the dictionary, and then a tuple containing everything in it [I have a store class, an enemy class, a portal class , and soon the NPC class]). So I would like it to be created in such a way that I can store all this in a variable that my NPC class has saved (not a bunch of if statements)

Just to clarify, I am NOT asking someone to write code for me, I'm just not sure how to approach the problem.


A short description of what I need:

A tree-like structure that starts with one line with the number of lines that are "disconnected" from it

Each of these lines has more branches.

The difference from this and tuples of tuples is that there should be a line in which they are disconnected, and not immediately.

+4
source share
2 answers

This is closely related to the decision tree , but the player makes a choice, so he is not completely alone in the sense of AI. You can define the data structure that has these properties yourself, although

class TreeNode: def __init__(self,parent,children,overview_text,direction_text): #... set the variables ... # question_text is a string def print_question(self): print overview_text for child in children: print direction_text # add some accessor methods def add_child(self,child): ... 

Then your NPCs could track their status through a specific TreeNode - a call that current_question - and the whole conversation in the TreeNodes tree - invokes a dialog.

 class NPC: def __init__(self,dialogue): #... set the variables ... self.current_question = None def converse(self): #.... do some stuff.... self.current_question = users_choice; 

This way you can end the conversation as needed.

Note that TreeNode may be incorrect, because you may need dialogs to display a graph instead of a tree. For example, many RPGs will have conversation paths that can cycle through information. The data structure above cannot do this because overview_text (conceptually part of node) and direction_text (conceptually part of edge) are considered as one.

+3
source

You need a directed graph where the bot operators are nodes and the players' answers are arcs.

You may also want the arcs to have built-in intelligence - to enable / disable themselves depending on the state of the player (ie if the player does not have a blue key, then β€œunlock the blue door” is not an available option).

Sample code.

0
source

Source: https://habr.com/ru/post/1412283/


All Articles