aspose file tools*
The moose likes Game Development and the fly likes jess Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "jess" Watch "jess" New topic
Author

jess

baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
hey if any one have any idea of creating a text based game engine using java

BAsic idea is that create java applets and connect them please tell how to inter connect them and we have to use JESS (Java Expert System Shell) it is a plugin of java

we should use non-monotonic logic(Artificial Intelligence) in this game implementation1

So, please if any one have any idea please help mee
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I put in a request for someone to drop past...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Wow, that's a tall order. You've got questions about creating a game engine, writing applets, implementing applet-server communication, using a rule engine (Jess), and implementing AI.

I am the creator of Jess (not JESS, Jess; I'm officially required to tell you that this does not stand for "Java Expert System Shell" -- it's just a name, "Jess".) I can help you with general or specific questions about Jess (and all this other stuff too, actually, but so can lots of other people here.)

To get started, why not tell us what you know already from the list above. Have you written applets? Done network communication? Visited the Jess website (http://www.jessrules.com) and skimmed the manual? What do you know about game development? Let us know where you're at, and then we can try to help you from there.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

By private message:

i am a student and i am wishing to create a game as a mobile application using jess

as jess is a rule engine we can make help of it to create rules for non-monotonic logic

connectivity between applets is done but connecting jess with those applets has become a problem

could you please help me how to get a connection created so that depending on the present rule it creates a new rule and displays it so that user can click the option and so on...... the game continues

plzzzzzz help me


Jess is just a Java library, like any other, and the manual includes many simple and complex examples of embedding Jess in Java programs. Using Jess in an applet (if we're talking about java.applet.Applet) could be as simple as

import jess.*;
...
Rete engine = new Rete(this);
engine.batch("rules.clp")
engine.run();

If you pass "this" (the applet) to the engine constructor, as shown here, Jess is even smart enough to know to look for the file "rules.clp" on the web server.

I don't know what you mean here by "connecting" -- linking within a single process, or connecting to a remote service? And as far as "depending on the present rule it creates a new rule", that's really too vague to help with. At some point you need to learn the rule language and start writing code; then I can help you with specific questions.
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Ernest Friedman-Hill:


Jess is just a Java library, like any other, and the manual includes many simple and complex examples of embedding Jess in Java programs. Using Jess in an applet (if we're talking about java.applet.Applet) could be as simple as

import jess.*;
...
Rete engine = new Rete(this);
engine.batch("rules.clp")
engine.run();

If you pass "this" (the applet) to the engine constructor, as shown here, Jess is even smart enough to know to look for the file "rules.clp" on the web server.

I don't know what you mean here by "connecting" -- linking within a single process, or connecting to a remote service? And as far as "depending on the present rule it creates a new rule", that's really too vague to help with. At some point you need to learn the rule language and start writing code; then I can help you with specific questions.


I read the complete manual and i am familiar with many commanda but it is not being done when i am trying to add one more button to the same frame so please help me in finding my mistake in this code



(import javax.swing.*)
(import java.awt.event.*)
(import javax.swing.JFrame)
(import java.awt.*)

(defglobal ?*f* = (new JFrame "Button"))
(?*f* setLocation 500 300)

(defglobal ?*a* = (new Button "start"))
(?*a* set 30 40)
(?*f* add ?*a* )

(defglobal ?*c* = (new Button "exit"))
(?*f* add ?*c* )
(?*c* setLocation 60 40)
(?*f* pack)
(set ?*f* visible TRUE)
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
This is just a guess, bu the way in which the exit button is described is different to the way the start button is described.

One uses set and the other uses set location.

Have you tried copying the existing button and renaming and relocating?

Personally I would start with what works, and if there is a similar element, copy it, and then change its parameters and see what affect that has.
But then I dont have JESS so havent read the manual.

Gavin
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Gavin Tranter:
This is just a guess, bu the way in which the exit button is described is different to the way the start button is described.

One uses set and the other uses set location.

Have you tried copying the existing button and renaming and relocating?

Personally I would start with what works, and if there is a similar element, copy it, and then change its parameters and see what affect that has.
But then I dont have JESS so havent read the manual.

Gavin


ya i have done in getting the buttons placing

but i am unable to connect the applets made in jess

i mean to connect two applets created in jess is there any connectivity rule to do it
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Indeed, this isn't a Jess problem, but a Java GUI problem. I see lots of issues here, all of which have to do with the GUI code itself, irrespective of language.

First, as Gavin points out, Button doesn't have a "set" method, so you can't be calling it. setLocation() is, indeed, a valid method. This script would abort with an exception right there.

Secondly, you're putting AWT Buttons into a Swing JFrame; you're mixing toolkits, and you ought to be using JButton instead with your JFrame. This will work, it's just bad style and you'll have other problems down the road.

Third, using "setLocation" followed by add() to place buttons is a horrible practice; in Java, you should use a LayoutManager and let it decide where to place the components. But if you are going to use this technique, then you need to turn off the default layout manager by setting it to null; i.e.,

(?*f* setLayout nil)

otherwise, you're using the JFrame's default BorderLayout layout manager. Calling add() on the JFrame with one argument like this places the component in the CENTER location of the JFrame; calling it again replaces the previous CENTER component with a new one, which is why you'll only ever see one button here (and your carefully computed component dimensions will be ignored, anyway.)

If you're going to write GUI code in Jess rather than in Java -- and although it's certainly possible, I see no reason to do it here -- it's best to write the code in Java first, and get it working the way you like, and then translate it to Jess. That way the Java compiler, or your IDE, can point out many errors for you.
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Ernest Friedman-Hill:
Indeed, this isn't a Jess problem, but a Java GUI problem. I see lots of issues here, all of which have to do with the GUI code itself, irrespective of language.

First, as Gavin points out, Button doesn't have a "set" method, so you can't be calling it. setLocation() is, indeed, a valid method. This script would abort with an exception right there.

Secondly, you're putting AWT Buttons into a Swing JFrame; you're mixing toolkits, and you ought to be using JButton instead with your JFrame. This will work, it's just bad style and you'll have other problems down the road.

Third, using "setLocation" followed by add() to place buttons is a horrible practice; in Java, you should use a LayoutManager and let it decide where to place the components. But if you are going to use this technique, then you need to turn off the default layout manager by setting it to null; i.e.,

(?*f* setLayout nil)

otherwise, you're using the JFrame's default BorderLayout layout manager. Calling add() on the JFrame with one argument like this places the component in the CENTER location of the JFrame; calling it again replaces the previous CENTER component with a new one, which is why you'll only ever see one button here (and your carefully computed component dimensions will be ignored, anyway.)

If you're going to write GUI code in Jess rather than in Java -- and although it's certainly possible, I see no reason to do it here -- it's best to write the code in Java first, and get it working the way you like, and then translate it to Jess. That way the Java compiler, or your IDE, can point out many errors for you.


ok thank you for correcting my mistakes but i want to implement in Jess and one more thing i succeded with your suggession

and how to connect the two gui interfaces in Jess
now this is the main problem i am facing
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

How to which which two GUI interfaces?
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Ernest Friedman-Hill:
How to which which two GUI interfaces?


i created two frames in jess
one is a interface with all buttons and the other which should be loaded when i click the button in the first frame
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

This chapter of the Jess manual is devoted to building GUIs in the Jess language; there are several examples of responding to button presses and other events.
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Ernest Friedman-Hill:
This chapter of the Jess manual is devoted to building GUIs in the Jess language; there are several examples of responding to button presses and other events.


yes it is given about many of the gui interactings of the buttons but there is no example or atleast the header or the methods which are to be imported or called to invoke another applet from the present applet

so please help with the sample code sinppet
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You need to understand what you're doing first, and how you would write it in Java, before trying to write any kind of Java scripting code like this in Jess. Once you understand what you want to do in Java, and then how Jess scripting works, it's trivial to write your code in Jess. But it really sounds like your understanding of what you want to do in Java is very weak, and I think you need to work on your design. Get something working in Java, and then add Jess to the mix only after you have a better idea of what you're trying to accomplish.

Code to make a JFrame appear when a button is pressed is trivial in both Java and Jess, and in fact the page I pointed to shows exactly what to do, with detailed code examples. So somehow I think you must be looking for something else.

On the other hand, now that you've changed the description to "invoking one applet from another applet", that's a whole different ballgame. First of all, that phrase doesn't really mean anything clearly. Do you mean communicating between two applets on a single HTML page (that can be done; but it has nothing to do with Jess). Do you mean creating one applet from another (strictly, that's possible, but not really in a useful way.) I suspect what you actually mean is something about communicating between applets in two different web browsers, on two different machines -- and that's an enormous topic, and one that has nothing at all to do with Jess, or GUIs, or buttons, or Frames, or anything else you're asking about here. If your question has anything to do with this last topic, then you need to go do some reading on Java networking, and the browser security model, and all sorts of other stuff. You might start here.
[ April 15, 2008: Message edited by: Ernest Friedman-Hill ]
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Ernest Friedman-Hill:
You need to understand what you're doing first, and how you would write it in Java, before trying to write any kind of Java scripting code like this in Jess. Once you understand what you want to do in Java, and then how Jess scripting works, it's trivial to write your code in Jess. But it really sounds like your understanding of what you want to do in Java is very weak, and I think you need to work on your design. Get something working in Java, and then add Jess to the mix only after you have a better idea of what you're trying to accomplish.

Code to make a JFrame appear when a button is pressed is trivial in both Java and Jess, and in fact the page I pointed to shows exactly what to do, with detailed code examples. So somehow I think you must be looking for something else.

On the other hand, now that you've changed the description to "invoking one applet from another applet", that's a whole different ballgame. First of all, that phrase doesn't really mean anything clearly. Do you mean communicating between two applets on a single HTML page (that can be done; but it has nothing to do with Jess). Do you mean creating one applet from another (strictly, that's possible, but not really in a useful way.) I suspect what you actually mean is something about communicating between applets in two different web browsers, on two different machines -- and that's an enormous topic, and one that has nothing at all to do with Jess, or GUIs, or buttons, or Frames, or anything else you're asking about here. If your question has anything to do with this last topic, then you need to go do some reading on Java networking, and the browser security model, and all sorts of other stuff. You might start here.

[ April 15, 2008: Message edited by: Ernest Friedman-Hill ]




(import javax.swing.*)
(import java.awt.event.*)
(import javax.swing.JFrame)
(import java.awt.*)

(defglobal ?*f* = (new JFrame))
(defglobal ?*start* = (new JButton "start"))
(defglobal ?*load* = (new JButton "load"))
(defglobal ?*save* = (new JButton "save"))
(defglobal ?*exit* = (new JButton "exit"))
(defglobal ?*settings* = (new JButton "settings"))

(?*f* setSize 150 300)
(?*f* setLayout nil)
(?*start* setSize 80 20)
(?*start* setLocation 30 30)

(?*load* setSize 80 20)
(?*load* setLocation 30 70)

(?*save* setSize 80 20)
(?*save* setLocation 30 110)

(?*settings* setSize 80 20)
(?*settings* setLocation 30 150)

(?*exit* setSize 80 20)
(?*exit* setLocation 30 190)

(?*f* add ?*start* )
(?*f* add ?*load* )
(?*f* add ?*save* )
(?*f* add ?*settings* )
(?*f* add ?*exit* )


(set ?*f* visible TRUE)


this is the jess code for a button execution in a frame



(import javax.swing.*)
(import java.awt.event.*)
(import javax.swing.JFrame)
(import java.awt.*)
;; Import some commonly-used classes

;; Don't clear defglobals on (reset)
(set-reset-globals FALSE)

(defglobal ?*crlf* = "")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Question and answer templates

(deftemplate question
(slot text)
(slot type)
(multislot valid)
(slot ident))

(deftemplate answer
(slot ident)
(slot text))

(do-backward-chaining answer)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Module trigger

(defmodule trigger)

(defrule trigger::supply-answers
(declare (auto-focus TRUE))
(MAIN::need-answer (ident ?id))
(not (MAIN::answer (ident ?id)))
(not (MAIN::ask ?))
=>
(assert (MAIN::ask ?id))
(return))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; sound rules

(defrule MAIN::check-home
(declare (auto-focus TRUE))
(answer (ident sound) (text no))
(answer (ident open-door) (text no))

(answer (ident fire) (text no))
(answer (ident wind) (text no))
(answer (ident light-on) (text no))
=>
(assert (check surrounding))
(recommend-action "you are some where stuck in a place with a very calm climate"))

;;..........................................some more code to be written
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; forest domain rules

(defrule MAIN::forest
(declare (auto-focus TRUE))
(explicit (answer (ident place) (text in-city)))
=>
(recommend-action "to ask a human expert before going out of city to guide you to a better place")
(halt))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Results output

(deffunction recommend-action (?action)
"check final condition about the house"
(call JOptionPane showMessageDialog ?*frame*
(str-cat "I recommend that you " ?action)
"Recommendation"
(get-member JOptionPane INFORMATION_MESSAGE)))

(defadvice before halt (?*qfield* setText "Close window to exit"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Module ask

(defmodule ask)

(deffunction ask-user (?question ?type ?valid)
"Set up the GUI to ask a question"
(?*qfield* setText ?question)
(?*apanel* removeAll)
(if (eq ?type multi) then
(?*apanel* add ?*acombo*)
(?*apanel* add ?*acombo-ok*)
(?*acombo* removeAllItems)
(foreach ?item ?valid
(?*acombo* addItem ?item))
else
(?*apanel* add ?*afield*)
(?*apanel* add ?*afield-ok*)
(?*afield* setText ""))
(?*apanel* validate)
(?*apanel* repaint))

(deffunction is-of-type (?answer ?type ?valid)
"Check that the answer has the right form"
(if (eq ?type multi) then
(foreach ?item ?valid
(if (eq (sym-cat ?answer) (sym-cat ?item)) then
(return TRUE)))
(return FALSE))

(if (eq ?type number) then
(return (is-a-number ?answer)))

;; plain text
(return (> (str-length ?answer) 0)))

(deffunction is-a-number (?value)
(try
(integer ?value)
(return TRUE)
catch
(return FALSE)))

(defrule ask::ask-question-by-id
"Given the identifier of a question, ask it"
(declare (auto-focus TRUE))
(MAIN::question (ident ?id) (text ?text) (valid $?valid) (type ?type))
(not (MAIN::answer (ident ?id)))
(MAIN::ask ?id)
=>
(ask-user ?text ?type ?valid)
((engine) waitForActivations))

(defrule ask::collect-user-input
"Check an answer returned from the GUI, and optionally return it"
(declare (auto-focus TRUE))
(MAIN::question (ident ?id) (text ?text) (type ?type) (valid $?valid))
(not (MAIN::answer (ident ?id)))
?user <- (user-input ?input)
?ask <- (MAIN::ask ?id)
=>
(if (is-of-type ?input ?type ?valid) then
(retract ?ask ?user)
(assert (MAIN::answer (ident ?id) (text ?input)))
(return)
else
(retract ?ask ?user)
(assert (MAIN::ask ?id))))

;; Main window
(defglobal ?*frame* = (new JFrame "sample game"))
;;(?*frame* setDefaultCloseOperation (get-member JFrame EXIT_ON_CLOSE))
(?*frame* setSize 520 140)
(?*frame* setVisible TRUE)

;; Question field
(defglobal ?*qfield* = (new JTextArea 5 40))
(bind ?scroll (new JScrollPane ?*qfield*))
((?*frame* getContentPane) add ?scroll)
(?*qfield* setText "Please wait...")

;; Answer area
(defglobal ?*apanel* = (new JPanel))
(defglobal ?*afield* = (new JTextField 40))
(defglobal ?*afield-ok* = (new JButton OK))

(defglobal ?*acombo* = (new JComboBox (create$ "yes" "no")))
(defglobal ?*acombo-ok* = (new JButton OK))

(?*apanel* add ?*afield*)
(?*apanel* add ?*afield-ok*)
((?*frame* getContentPane) add ?*apanel* (get-member BorderLayout SOUTH))
(?*frame* validate)
(?*frame* repaint)

(deffunction read-input (?EVENT)
"An event handler for the user input field"
(assert (ask::user-input (sym-cat (?*afield* getText)))))

(bind ?handler (new jess.awt.ActionListener read-input (engine)))
(?*afield* addActionListener ?handler)
(?*afield-ok* addActionListener ?handler)

(deffunction combo-input (?EVENT)
"An event handler for the combo box"
(assert (ask::user-input (sym-cat (?*acombo* getSelectedItem)))))

(bind ?handler (new jess.awt.ActionListener combo-input (engine)))
(?*acombo-ok* addActionListener ?handler)

(deffacts MAIN::question-data
(question (ident place) (type multi) (valid in-city out-city strange)
(text "What kind of place is it?"))
(question (ident sound) (type multi) (valid yes no)
(text "Do you hear any different sounds?"))
(question (ident open-door) (type multi) (valid yes no)
(text "Do you hear any sound that seem different ?"))
(question (ident fire) (type multi) (valid yes no)
(text "Do you see fire on far hills?"))
(question (ident wind) (type multi) (valid yes no)
(text "Is there a severe winds blowing?"))
(question (ident light-on) (type multi) (valid yes no)
(text "Does the surroundings seem to have light?"))
(ask place))


(reset)
(run-until-halt)


this code will create a frame

so now my aim is to create a program such that when i click the button in first frame
i should come to the frame [B]sample game
[/B]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
It has been a very very long time since I did anything with swing or GUI components, but surely its a rather easy case to control the visibility of two frames with a toggle?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Much of this code is copied directly from the "Diagnostic Assistant" example from the textbook; I don't know that it's particularly applicable to playing a game. Furthermore, the code already contains a couple of examples of adding ActionListeners to components, so I'm really unclear as to why you're asking this particular question. But in any case, you'd define a function and put your frame-creating code into it; i.e.,



Then if you want to call this function when the button in ?*start* is pressed, you'd just say

baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by Ernest Friedman-Hill:
Much of this code is copied directly from the "Diagnostic Assistant" example from the textbook; I don't know that it's particularly applicable to playing a game. Furthermore, the code already contains a couple of examples of adding ActionListeners to components, so I'm really unclear as to why you're asking this particular question. But in any case, you'd define a function and put your frame-creating code into it; i.e.,



Then if you want to call this function when the button in ?*start* is pressed, you'd just say



i am not asking for the button in the same frame

for example say

i have created a frames naming f1 and f2
now the main thing is that code of frame1 is at "f1.clp" and frame2 is in "f2.clp"
now if i press a button in f1.clp then i should get the f2 frame to be executed( or poped out) and destroying frame1
baaru so
Greenhorn

Joined: Apr 02, 2008
Posts: 25
Originally posted by bharadwaz somavarapu:


i am not asking for the button in the same frame

for example say

i have created a frames naming f1 and f2
now the main thing is that code of frame1 is at "f1.clp" and frame2 is in "f2.clp"
now if i press a button in f1.clp then i should get the f2 frame to be executed( or poped out) and destroying frame1



one more thing to ask
why do i get errors when i tr to execute the files using rete object in java
i have imported all required files
and even copied the jess.jar file to lib also
even then its not getting executed whats problem with it...?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30294
    
150

Originally posted by bharadwaz somavarapu:
why do i get errors when i tr to execute the files using rete object in java

I don't know Jess, but I can tell there isn't enough information in this post for anyone to answer your question. Be sure to "Tell the Details" when you have a question. For example, post the errors you get when asking why you get the errors. See my signature for more on asking good questions.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jess