0

I'm trying to make a game of tic-tac-toe in JSF. I have a bi-dimensional matrix (3x3) that holds values 0, 1 or 2. So it's all 0 at the start and it switches to 1 or 2 depending of the player.

I'm trying to get the image for the button from a method in the bean, that reads the value in the matrix and gives it the appropriate image (nothing, X or O);

<h:form id="board">

<!--  id string i+j  -->

<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="00"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="01"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="02"/></h:commandButton>
<br />
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="10"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="11"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="12"/></h:commandButton>
<br />
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="20"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="21"/></h:commandButton>
<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg()}" style="width:200px;height:200px"> <f:param name="btn" value="22"/></h:commandButton>

</h:form>

and in the bean I have

public String btnImg() {

        String buttonId = getParameter("btn");

        int i = Integer.parseInt(String.valueOf(buttonId.charAt(0)));
        int j = Integer.parseInt(String.valueOf(buttonId.charAt(1)));

        int[][] posi = x.getTabuleiro();

        if (posi[i][j] == 0)
            return "resources/images/clear.png";

        else if (posi[i][j] == 1)
            return "resources/images/black_x.png";

        else
            return "resources/images/black_o.png";

    }

getTabuleiro gives me the board as it is now. How do I get the button params so that it knows which button is which?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Does this answer your question? [Passing parameter to JSF action](https://stackoverflow.com/questions/7401771/passing-parameter-to-jsf-action) – Kukeltje Feb 06 '20 at 20:04

1 Answers1

0

Use Methodarguments will be easiest way.

<h:commandButton action="#{some_action}" image="#{beanJogo.btnImg(2,2)}" style="width:200px;height:200px"></h:commandButton>

and your Java- Method like

public String btnImg(int i, int j) {

        int[][] posi = x.getTabuleiro();

        if (posi[i][j] == 0)
            return "resources/images/clear.png";

        else if (posi[i][j] == 1)
            return "resources/images/black_x.png";

        else
            return "resources/images/black_o.png";

    }
Matthias
  • 1,378
  • 10
  • 23