Implementing the Poker Game in Jess Vanmoerkerke Frederik Project APLAI Aim of the project Learn the rules of the Poker game Implement the rules of.
Download
Report
Transcript Implementing the Poker Game in Jess Vanmoerkerke Frederik Project APLAI Aim of the project Learn the rules of the Poker game Implement the rules of.
Implementing the
Poker Game in Jess
Vanmoerkerke Frederik
Project APLAI
Aim of the project
Learn the rules of the Poker game
Implement the rules of the game in Jess
Practical example of the theory
Interaction between Java and Jess
Benefits of rule based systems
Structure of the program
Java Applet for Graphical User Interface
Jess for implementing the rules of the game
Assert Facts
Jess Rule Engine
(adjust knowledge base)
Java Applet User Interface
Check Facts by Getting Slot Values
Video Poker
Single player game
Try to get a Poker hand
Poker hand: pair, double pair, three of a kind, ...
Task Jess: check if we have a Poker hand
deftemplates + deffacts
(deftemplate card "The number, value and suit of the card"
(slot number) (slot value) (slot suit))
(deftemplate result "Payout index"
(slot index))
(deffacts indexnumber "Initiate the indexnumber"
(result (index 0)))
Assert the card facts
Deftemplate cardtemp = engine.findDeftemplate("card");
Fact card1 = new Fact(cardtemp);
card1.setSlotValue("number",new Value(0,RU.INTEGER));
card1.setSlotValue("value",new Value(value[0],RU.INTEGER));
card1.setSlotValue("suit",new Value(suit[0],RU.INTEGER));
engine.assertFact(card1);
Example rule
Check if we have a full house (pair and three of a kind)
(defrule check-full-house
(card (number ?n1) (value ?v1))
(card (number =(+ ?n1 1))(value ?v1))
(card (number ?n2&~?n1) (value ?v2&~?v1))
(card (number =(+ ?n2 1)) (value ?v2))
(card (number =(+ ?n2 2)) (value ?v2))
?oldindex <- (result (index ?i&:(< ?i 8)))
=>
(retract ?oldindex)
(assert (result (index 8))))
Get the result fact
Fact fact = new Fact(engine.findDeftemplate("result"));
String factName="";
Iterator it = engine.listFacts();
while (it.hasNext()&&!factName.endsWith("result"))
{
fact = (Fact) it.next();
factName = fact.getName() ;
}
vindex = fact.getSlotValue("index");
index = vindex.intValue(engine.getGlobalContext());
score += index*bet;
Draw Poker
Multiplayer game
Also trying to get a Poker hand
Winner = best Poker hand
Tasks Jess
What action the computer player should take
Which cards to hold
Finding the winner
Different modules: CARDS,ACTION,HOLD
deftemplates en deffacts
(deftemplate CARDS::card "The value and suit of the card"
(slot number) (slot value) (slot suit))
(deftemplate HOLD::hold "number card to hold"
(slot number))
(deftemplate HOLD::index "index to be sure that highest rank is holded"
(slot number))
(deftemplate CARDS::dealer "show if dealer had has his turn"
(slot state))
(deftemplate ACTION::action "action: Fold,Pass/Call,Bet/Raise"
(slot text))
(deftemplate ACTION::index "index to know what action to take and height of the cards"
(slot number) (slot height1) (slot height2))
(deffacts CARDS::indexnumber "Initiate the indexnumbers and a random number"
(HOLD::index (number 0))
(ACTION::index (number 0) (height1 0) (height2 0))
(ACTION::rand (round (* 10 (/ (random) 65536)))))
Action of computer player
Choice of action depends of the cards
Getting index from same rules as in Video Poker
Example rule
(defrule ACTION::action-raise
(CARDS::dealer (state "false"))
(or (and (ACTION::index (number ?i&:(> ?i 3)))
(ACTION::rand ?j&:(> ?j 1)) )
(and (ACTION::index (number ?i&:(> ?i 0)&:(< ?i 4)))
(ACTION::rand ?j&:(> ?j 4)) ) )
=>
(assert (ACTION::action (text Bet/Raise))))
Hold cards for computer player
Example rule when having four of a kind
(defrule HOLD::hold-four-of-a-kind
(CARDS::card (number ?n) (value ?v))
(CARDS::card (number =(+ ?n 1)) (value ?v))
(CARDS::card (number =(+ ?n 2)) (value ?v))
(CARDS::card (number =(+ ?n 3)) (value ?v))
?oldindex <- (HOLD::index (number ?i&:(< ?i 7)))
=>
(retract ?oldindex)
(assert (HOLD::index (number 7)))
(assert (HOLD::hold (number ?n)))
(assert (HOLD::hold (number (+ ?n 1))))
(assert (HOLD::hold (number (+ ?n 2))))
(assert (HOLD::hold (number (+ ?n 3)))))
Finding Winner
Same rules as in Video Poker
Additional slots: Height1, Height2
Example rule for checking two pairs
(defrule CARDS::check-two-pair
(CARDS::card (number ?n1) (value ?v1))
(CARDS::card (number =(+ ?n1 1)) (value ?v1))
(CARDS::card (number ?n2&~?n1) (value ?v2&~?v1))
(CARDS::card (number =(+ ?n2 1)) (value ?v2))
?oldindex <- (ACTION::index (number ?i&:(< ?i 2)))
=>
(retract ?oldindex)
(assert (ACTION::index (number 2) (height1 ?v1) (height2 ?v2))))
Conclusion
Very easy to implement rules in Jess
Better overview of rules than in a normal
programming language like Java
Easy communication between Java and Jess
Demo
Video Poker
Draw Poker