Pergunta

I've been trying to represent a certain scenario for a game I'm building in Jess and so far I have a certain fact which will tell all the elements from each scenario. Since this game scenario will be represented in text format (using just text characters) I tought about using a two dimensional array to match a certain character to a certain position based on the info retrieved from the scenario facts.

Anyone could give me some insight on how to achieve this?

Foi útil?

Solução

Jess itself doesn't have a data structure like that. You could use a Java list-of-lists, but it would be awkward to manipulate from Jess. Fortunately, a game board is usually a sparse matrix, meaning that most positions are empty, and you therefore only need to represent the ones that are not. You can either put coordinates directly into your game object template -- i.e., x and y here:

(deftemplate game-object (slot name) (slot type) (slot x) (slot y) ...)

Or you can use a separate "position" template and match it with game objects using an id, like

(deftemplate game-object (slot name) (slot type) (slot id) ...)
(deftemplate location (slot id) (slot x) (slot y))

One important insight is that pattern matching makes loops over all objects unnecessary. For example, say you want to check if there are no other characters in the same row as your "hero" character. You can just write something like this, no looping required:

(defrule no-character-in-same-row-as-hero
    (game-object (name hero) (y ?y))
    (not (game-object (name ~hero) (y ?y)))
     =>
    // Do whatever you want, knowing that no other characters are in the same row
    )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top