javaFX Ultimate tic tac toe issues

Discussion in 'Java' started by netOwen, Nov 28, 2016.

  1. netOwen

    netOwen

    Joined:
    Nov 28, 2016
    Messages:
    2
    Likes Received:
    0
    hay so i have been working on this ultimate tic tac toe game i have the board showing with the 3*3 games on the board. [​IMG]
    my problem is that now the pieces will not show on the board when a square is clicked.. any help would be gratefully appreciated.
    below is the code that there may be a mistake
    class XOBoard

    this.ultimate1 = ultimate1;
    // Initialise the boards
    board = new int[3][3];
    // renders is an array that holds the 'render pieces'
    renders = new XOPiece[3][3];

    // initialise the rectangle
    back = new Rectangle();
    // add the rectangle
    getChildren().addAll(back);

    //init the board
    board = new int[3][3];
    renders = new XOPiece[3][3];
    for(int i = 0; i < 3; i++)
    for(int j = 0; j < 3; j++){
    board[j] = EMPTY;
    renders[j] = null;
    }
    //current_player = XPIECE;

    back = new Rectangle();
    back.setFill(Color.BLACK);
    h1 = new Line(); h2 = new Line();
    v1 = new Line(); v2 = new Line();
    h1.setStroke(Color.WHITE); h2.setStroke(Color.WHITE);
    v1.setStroke(Color.WHITE); v2.setStroke(Color.WHITE);

    //horazontal lines
    h1.setStartX(0); h1.setStartY(0); h1.setEndY(0);
    h2.setStartX(0); h2.setStartY(0); h2.setEndY(0);

    //vertical lines
    v1.setStartX(0); v1.setStartY(0); v1.setEndX(0);
    v2.setStartX(0); v2.setStartY(0); v2.setEndX(0);

    //translation of one cell height and two cell heights
    ch_one = new Translate(0,0);
    ch_two = new Translate(0,0);
    h1.getTransforms().add(ch_one);
    h2.getTransforms().add(ch_two);

    //translation of one cell height and two cell heights
    cw_one = new Translate(0,0);
    cw_two = new Translate(0,0);
    v1.getTransforms().add(cw_one);
    v2.getTransforms().add(cw_two);

    //add rectangle and lines to this group
    getChildren().addAll(back,h1,h2,v1,v2);



    //public method that tries to place a piece
    public void placePiece(final double x, final double y)
    {

    // translate the x, y coordinates into cell indexes
    int indexx = (int) (x / cell_width);
    int indexy = (int) (y / cell_height);

    // if the position is empty then place a piece and swap the players
    if(board[indexx][indexy] == EMPTY && ultimate1.getCurrent_player() == OPIECE)
    {

    // board is the array that holds all the pieces
    board[indexx][indexy] = XPIECE;
    // Create a new XPIECE
    renders[indexx][indexy] = new XOPiece(XPIECE);
    renders[indexx][indexy].resize(cell_width, cell_height);
    renders[indexx][indexy].relocate(indexx * cell_width,indexy * cell_height);
    // Place an X on the board at position x,y
    getChildren().add(renders[indexx][indexy]);
    this.ultimate1.setCurrent_player(OPIECE);
    }

    else if(board[indexx][indexy] == EMPTY && ultimate1.getCurrent_player() == OPIECE)
    {
    board[indexx][indexy] = OPIECE;
    renders[indexx][indexy] = new XOPiece(OPIECE);
    renders[indexx][indexy].resize(cell_width, cell_height);
    renders[indexx][indexy].relocate(indexx * cell_width,indexy * cell_height);
    getChildren().add(renders[indexx][indexy]);
    this.ultimate1.setCurrent_player(XPIECE);
    }

    here is the class xopiece

    package ultimatexos;

    //implementation of an XO Piece
    //imports required for this class
    import javafx.scene.Group;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.shape.Line;
    import javafx.scene.transform.Translate;

    // class definition for an X or O piece
    class XOPiece extends Group
    {

    // constructor for the class
    public XOPiece(int type)
    {
    // create a new translate object and take a copy of the type
    pos = new Translate();
    this.type = type;

    // create a new translate object and take a copy of the type
    //this.type = type;

    // choose which piece type we have
    if(type == 1)
    {
    // we have an X piece generate two lines and add them to
    // as render nodes add in the translate for our lines
    l1 = new Line(); l2 = new Line();
    getChildren().addAll(l1, l2);
    l1.getTransforms().add(pos);
    l2.getTransforms().add(pos);
    l1.setStroke(Color.RED);
    l2.setStroke(Color.RED);

    l1.setStartX(0); l1.setStartY(0);
    l2.setStartY(0); l2.setStartX(0);
    }
    else
    {
    // we have an O piece generate an oval and add it as a
    // render node
    e = new Ellipse();
    getChildren().addAll(e);
    e.getTransforms().add(pos);
    e.setStroke(Color.LIME);

    }
    }


    // overridden version of the resize method
    @Override
    public void resize(double width, double height)
    {
    super.resize(width,height);
    // update depending on the type
    if(type == 1)
    {
    // resize the lines
    // Lines are crossed
    l1.setEndX(width); l1.setEndY(height);
    l2.setStartX(width); l2.setEndY(height);
    }
    else
    {
    // recenter the ellipse// and update the radii
    e.setCenterX(width / 2); e.setCenterY(height / 2);
    e.setRadiusX(width / 2); e.setRadiusY(height / 2);
    }
    }

    @Override
    public void relocate(double x, double y){
    super.relocate(x, y);
    pos.setX(x); pos.setY(y);
    }




    // private fields of the class
    private Line l1, l2; // lines for drawing the X piece
    private Ellipse e; // ellipse for rendering the O piece
    private final int type; // maintain a copy of the piece type we have
    private final Translate pos;//tran the set position of this piece
    }
     

    Attached Files:

    • tic.PNG
      tic.PNG
      File size:
      8.4 KB
      Views:
      167
    netOwen, Nov 28, 2016
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.