1

I would like to create a Dice game using JSF, I am using 2 dices(values from 1 to 6), 1 player. I want to add 10 points to the Score each time the sum of values of the 2 dices=7, and Increment the Counter whatever is the value of dices.

Here is the index.xhtml :

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">

    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
        <h:panelGrid columns="2" border="2" width="70%">
        <h:outputText value="Votre score est: #{dice.score}"/>
        <h:outputText value="Vous etes dans le tour: #{dice.nombreLance}"/>
        <h:outputText value="Le premier dé: #{dice.d1}"/>
        <h:outputText value="Le deuxieme dé: #{dice.d2}"/>
        </h:panelGrid>
        <h:commandButton value="Lancer" action="#{dice.calc}"/>
        </h:form>
    </h:body>
    </html>

Here is the Dice.java :

    package org.cours.dice;

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;

    @ManagedBean
    @RequestScoped

    public class Dice {
        private int d1 = (int)(Math.random()*6) + 1;
        private int d2 = (int)(Math.random()*6) + 1;;
        private int score;
        private int counter;

    public int getD1() {
        return d1;
    }

    public void setD1(int d1) {
        this.d1 = d1;
    }

    public int getD2() {
        return d2;
    }

    public void setD2(int d2) {
        this.d2 = d2;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getCounter() {
        return counter;
    } 

    public void setCounter(int counter) {              
        this.counter = counter;
    }

    public int total() {
           // Return the total showing on the two dices.
       return d1 + d2;
    }




    public void calc(){

        if (total()==7){
            counter++;
            score+=10;
        }else{
            counter++;
            }}

    public Dice() {

    }
    }

The problem here is whenever the player roll for new values, the counter doesn't increment and the score always come back to zero.

Can you help me on this ?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mourad gh
  • 23
  • 4
  • 1
    Please don't use the very generic java tag on questions like these. It leads to answers from people not knowing jsf and providing fully wrong answers. And please learn to look for the generic problem... It has nothing to do with dices, counters or scores. It is about keeping the value of a property over requests... find the generic problem in your situation. For these there are several Q/A in stackoverflow. – Kukeltje Jan 09 '18 at 00:15
  • off-topic: 'throwing the dice' should not be done when instantiating a class but when the 'calc' method is called (and it should better be 'throwDice' and the class should be 'GameOfDice' – Kukeltje Jan 09 '18 at 00:22

0 Answers0