-3
I’m trying to put together a simple parse to parse commands entered by a user for a type game Text Adventure. However, I’ve never had experience with it and I don’t know if the structure and types of words I’m using are correct.
The class Parse
contains the basic structure of the sentence that will be analyzed, see:
class Parse(object):
def __init__(self, sentenca=''):
self.sentenca = sentenca
self.acoes = ['pega', 'use', 'olhe', 'ir', 'examine']
self.verbos = ['pegar', 'usar', 'olhar', 'examinar']
self.direcoes = ['norte', 'sul', 'leste','oeste']
self.acaoatual = None
self.direcaodestino = None
if not self.sentenca.strip():
raise Exception('A setenca informada nao eh uma setenca valida.')
@staticmethod
def _extrairpalavras(sentenca=''):
if not sentenca:
return None
return sentenca.lower().split(' ')
def parseAcao(self):
palavras = Parse._extrairpalavras(sentenca=self.sentenca)
if palavras:
for p in palavras:
if p in self.acoes or p in self.verbos:
self.acaoatual = p
break
def parseDirecao(self):
palavras = Parse._extrairpalavras(sentenca=self.sentenca)
if palavras:
for p in palavras:
if p in self.direcoes:
self.direcaodestino = p
break
A small example of use:
parse = Parse('pegar item')
parse.parseAcao()
parse.parseDirecao()
print(parse.acaoatual)
print(parse.direcaodestino)
Exit:
catch
None
The idea here is to use this in the future parse with two other classes that are:
Class Ambiente
:
class Ambiente(object):
def __init__(self, id=None, titulo='', descricao='', itens=[]):
self.id = id
self.titulo = titulo
self.descricao = descricao
self.itens = itens
And Item
:
class Item(object):
def __init__(self, id=None, descricao='', texto='', pegavel=False, pegado=False, usado=False):
self.id = id
self.descricao = descricao
self.texto = texto
self.pegavel = pegavel
self.pegado = pegado
self.usado = usado
In which class Ambiente
represents a room that the protagonist is in, and it will be possible to navigate from one room to another.
And the class Item
will be collectible items, usable or not that will be in certain environments, some are a complement to the story. Of course, I’ll create other classes, even a class to represent the protagonist. And this parse that I created this very limited and was as far as I could get.
That said, I have doubts related to grammar rules and the structure and way that parse should analyze the sentence that was informed by the user. Since the primary actions for the basic functioning of the game that I want and that can be performed are:
- Olhar
- Locomover
- Pegar um Item
- Usar um Item
- Examinar um item
So I can already have a basic and functional game limiting only these actions above. Below follows the questions.
Questions
- How I could build a parse basic to treat a sentence and make it treat the words and correlate them with the action that will be executed?
- Do I need to adopt some grammatical rules? If yes which?
- It is necessary to use a data structure as a tree to the parse or a simple list that’s enough?
Your questions are very broad for the site, but: R1) Whatever! There are endless ways to do this R2) You know! It is you who will define to what extent the program will be smart and how precise the Parsing needs to be. Most games of this type are quite simple and require the player to use the "complement verb" structure but only you set the limit of what your code will be capable of. R3) Again, it depends on what you want to do; I think you have to define use cases, phrases that you would like to be accepted and as far as you want to invest in this development.
– nosklo