rntest.Board.java Source code

Java tutorial

Introduction

Here is the source code for rntest.Board.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rntest;

import java.io.IOException;
import java.util.ArrayList;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

/**
 *
 * @author nticknor19
 */
public class Board {
    Texture bgtex, bgtexflipped;
    ArrayList<ArrayList> pieces = new ArrayList<>();
    private int width, height;
    private float sX, sY, bX, bY, wX, wY;

    public Board(String s, int w, int h, float sx, float sy, float bx, float by, float wx, float wy)
            throws IOException {
        this.bgtex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(s));
        this.bgtexflipped = TextureLoader.getTexture("PNG",
                ResourceLoader.getResourceAsStream(s.substring(0, s.length() - 4) + "_flipped.png"));
        this.width = w;
        this.height = h;
        this.sX = sx;
        this.sY = sy;
        this.bX = bx;
        this.bY = by;
        this.wX = wx;
        this.wY = wy;

        this.clearBoard();
        pieces.get(0).set(0, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_VIRUS, false, true));
        pieces.get(0).set(1, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_LINK, false, true));
        pieces.get(0).set(2, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_VIRUS, false, true));
        pieces.get(1).set(3, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_LINK, false, true));
        pieces.get(1).set(4, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_VIRUS, false, true));
        pieces.get(0).set(5, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_LINK, false, true));
        pieces.get(0).set(6, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_VIRUS, false, true));
        pieces.get(0).set(7, new PlayingPiece(Main.SIDE_BLUE, Main.PIECE_LINK, false, true));

        pieces.get(7).set(0, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_VIRUS, false, true));
        pieces.get(7).set(1, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_LINK, false, true));
        pieces.get(7).set(2, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_VIRUS, false, true));
        pieces.get(6).set(3, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_LINK, false, true));
        pieces.get(6).set(4, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_VIRUS, false, true));
        pieces.get(7).set(5, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_LINK, false, true));
        pieces.get(7).set(6, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_VIRUS, false, true));
        pieces.get(7).set(7, new PlayingPiece(Main.SIDE_YELLOW, Main.PIECE_LINK, false, true));
        //pieces.get(4).set(5, null);

        //pieces.add(tar);
    }

    public void draw() {
        bgtex.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0, 0);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(Display.getWidth(), 0);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(Display.getWidth(), Display.getHeight());
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(0, Display.getHeight());
        GL11.glEnd();

        for (int i = 0; i < pieces.size(); i++) {
            for (int j = 0; j < pieces.get(i).size(); j++) {
                if (pieces.get(i).get(j) != null)
                    ((Piece) pieces.get(i).get(j)).draw(this.sX + (this.bX + this.wX) * j,
                            this.sY + (this.bY + this.wY) * i, this.wX, this.wY);
            }
        }

    }

    public void drawUpsideDown() {
        bgtexflipped.bind();
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0, 0);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(Display.getWidth(), 0);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(Display.getWidth(), Display.getHeight());
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(0, Display.getHeight());
        GL11.glEnd();

        for (int i = 0; i < pieces.size(); i++) {
            for (int j = 0; j < pieces.get(i).size(); j++) {
                if (pieces.get(i).get(j) != null)
                    ((Piece) pieces.get(i).get(j)).draw(this.sX + (this.bX + this.wX) * ((this.width - 1) - j),
                            this.sY + (this.bY + this.wY) * ((this.height - 1) - i), this.wX, this.wY);
            }
        }
    }

    public void click(float x, float y, boolean down) throws IOException {
        if (down) {

        } else {
            if (this.inGameBoard(x, y)) {
                int sx = getSpace(x, y)[0];
                int sy = getSpace(x, y)[1];
                if (!this.spaceIsEmpty(sx, sy))
                    this.inSpace(sx, sy).flip();
                //System.out.println(getSpace(x,y)[0]+" "+getSpace(x,y)[1]);
            } else {
                //System.out.println("false");
            }

        }
    }

    public boolean spaceIsEmpty(int x, int y) {
        return pieces.get(y).get(x) == null;
    }

    public Piece inSpace(int x, int y) throws IOException {
        return (Piece) pieces.get(y).get(x);
    }

    public int[] getSpace(float x, float y) {
        return new int[] { (int) ((x - sX) * (width) / ((this.wX + this.bX) * (width))),
                (height - 1) - (int) ((y - sY) * (height) / ((this.wY + this.bY) * (height))) };
    }

    public boolean movePiece(int x1, int y1, int x2, int y2) {
        if (pieces.get(y1).get(x1) == null)
            return false;
        pieces.get(y2).set(x2, pieces.get(y1).get(x1));
        pieces.get(y1).set(x1, null);
        return true;
    }

    public boolean swapPiece(int x1, int y1, int x2, int y2) {
        if (pieces.get(y1).get(x1) == null || pieces.get(y2).get(x2) == null)
            return false;
        Piece temp = (Piece) pieces.get(y2).get(x2);
        pieces.get(y2).set(x2, pieces.get(y1).get(x1));
        pieces.get(y1).set(x1, null);
        pieces.get(y1).set(x1, temp);
        return true;
    }

    public boolean inGameBoard(float x, float y) {
        return (x > this.sX && x < this.sX + (this.bX + this.wX) * this.width && y > this.sY
                && y < this.sY + (this.bY + this.wY) * this.height);
    }

    public void clearBoard() {
        for (int i = 0; i < this.height; i++) {
            ArrayList<Piece> tar = new ArrayList<>(8);
            for (int j = 0; j < this.width; j++) {
                tar.add(null);
            }
            pieces.add(tar);
        }
    }

    public int[] find(Piece p) {
        for (ArrayList<Piece> pa : pieces)
            if (pa.indexOf(p) != -1)
                return new int[] { pa.indexOf(p), pieces.indexOf(pa) };
        return new int[] {};
    }

}