0

I've been trying to draw a 3x3 grid in JSF so I can develop a tic tac toe game. However, this has been extremely punishing. Since this is a college project, we can't use 3rd party components, such as Primefaces. I've thought of using colspan, but so far I haven't found any good examples on how to do so.

This is my bean code:

package tictactoe;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;


@Named
@SessionScoped
public class TestBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean ocupado;
    private int loc;
    private static final ArrayList<Posicao> coluna1 = new ArrayList<Posicao>();
    private static final ArrayList<Posicao> coluna2 = new ArrayList<Posicao>();
    private static final ArrayList<Posicao> coluna3 = new ArrayList<Posicao>();

    public TestBean() {
        setColuna1();
        setColuna2();
        setColuna3();
    }

    private void setColuna3() {
        for (int i = 6; i <= 8; i++) {
            coluna3.add(getPositions().get(i));
        }
    }

    private void setColuna2() {
        for (int i = 3; i <= 5; i++) {
            coluna2.add(getPositions().get(i));
        }
    }

    private void setColuna1() {
        for (int i = 0; i <= 2; i++) {
            coluna1.add(getPositions().get(i));
        }
    }

    private static final ArrayList<Posicao> positions = new ArrayList<Posicao>(Arrays.asList(new Posicao(1, false),
            new Posicao(2, false), new Posicao(3, false), new Posicao(4, false), new Posicao(5, false),
            new Posicao(6, false), new Posicao(7, false), new Posicao(8, false), new Posicao(9, false)));

    public ArrayList<Posicao> getPositions() {
        return positions;
    }

    public ArrayList<Posicao> getColuna1() {

        return coluna1;
    }

    public ArrayList<Posicao> getColuna2() {

        return coluna2;
    }

    public ArrayList<Posicao> getColuna3() {

        return coluna3;
    }

    public boolean isOcupado() {
        return ocupado;
    }

    public void setOcupado(boolean ocupado) {
        this.ocupado = ocupado;
    }

    public int getLoc() {
        return loc;
    }

    public void setLoc(int loc) {
        this.loc = loc;
    }

}

This is the Java code for the positions:

package tictactoe;

public class Posicao {
    private boolean ocupado;
    private int loc;

    //construtor da Posicao
    public Posicao(int loc, boolean ocupado) {
        this.ocupado=ocupado;
        this.loc=loc;
    }

    //getters e setters da classe
    public boolean isOcupado() {
        return ocupado;
    }

    public void setOcupado(boolean ocupado) {
        this.ocupado = ocupado;

    }

    public int getLoc() {
        return loc;
    }

    public void setLoc(int loc) {
        this.loc = loc;
    }
}

And, lastly, this my JSF code:

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>Tabela do jogo do galo</title>
</h:head>
<body>
    <h2>Grelha de jogo</h2>
    <h:form>
    <ui:repeat  value="#{testBean.positions}" var="posicao">
        <h:panelGrid id="panel" columns="3" border="1" cellpadding="10"
            cellspacing="1">
            <h:outputLabel value="#{posicao.loc}" />
        </h:panelGrid>
        </ui:repeat>
    </h:form>
</body>
</html>

As you can see in my bean, I've even split my main position arraylist into 3 sub arrays.

Any atempts to draw a 3x3 grid have been unsuccessful, thus far. Any kind of help would be most welcome.

Thanks!

  • I ended up using ui:repeat to repeat a div with 33.33% width and 33.33% height, which in the end draws a 3x3 grid. ` ` – nunoteixeira Feb 08 '20 at 23:10
  • Forgot to add the CSS: `.tabuleiro { width: 35vw; height: 35vw; line-height: 0px; } .casa { width: 33.33%; height: 33.33%; display: inline-block; } .casa input, .casa span { display: inline-block; width: 100%; height: 100%; background-repeat: no-repeat; background-position: center; background-color: transparent; background-size: contain; }` The grid is rendered like this: [link](https://i.imgur.com/a1H37cZ.png) – nunoteixeira Feb 08 '20 at 23:55

0 Answers0