Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 lekce109;
- /**
- *
- * @author CoolPrase
- */
- /*
- * 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.
- // */
- //
- import cz.gyarab.util.event.EventListener;
- import cz.gyarab.util.light.BasicLightMatrix;
- import cz.gyarab.util.light.InteractiveLightPanel;
- import cz.gyarab.util.light.LightColor;
- import cz.gyarab.util.light.LightPanel;
- import cz.gyarab.util.light.LightPanelFactory;
- import cz.gyarab.util.light.SwingDiamondLightPanel;
- import cz.gyarab.util.light.LightSelectEvent;
- import cz.gyarab.util.light.Matrix;
- import java.util.Arrays;
- /**
- *
- * @author kahoun
- */
- public class Sachovnice {
- public enum Barva {
- BILY(LightColor.CHESSBOARD_WHITE, +1, true, false),
- CERNY(LightColor.CHESSBOARD_BLACK, -1, false, false),
- DAMA_B(LightColor.LIGHT_GRAY, +1, true, true),
- DAMA_C(LightColor.DARK_GRAY, -1, false, true);
- private final LightColor barva;
- //true = white; false = black
- private final boolean white;
- private final int smer;
- private final boolean dama;
- public static Barva valueOf(LightColor lc) {
- for (Barva barva : values()) {
- if (barva.getBarva().equals(lc)) {
- return barva;
- }
- }
- return null;
- }
- private Barva(LightColor barva, int smer, boolean white, boolean dama) {
- this.barva = barva;
- this.smer = smer;
- this.dama = dama;
- this.white = white;
- }
- public LightColor getBarva() {
- return barva;
- }
- public int getSmer() {
- return smer;
- }
- public boolean isWhite() {
- return white;
- }
- private boolean isDama() {
- return dama;
- }
- }
- public static final int POCET_RAD = 3;
- public static void main(String[] args) {
- Sachovnice sachovnice = new Sachovnice();
- sachovnice.poloz(0, 2, Barva.BILY);
- }
- private final Matrix matrix = Matrix.createMatrix(8, 8);
- private Barva hrac = Barva.BILY;
- private int sloupec = -1;
- private int radek = -1;
- public Sachovnice() {
- matrix.showWindow();
- matrix.setBackground(LightColor.CYAN);
- // vytvarime sachovou desku
- for (int radek = 0; radek < matrix.getHeight(); radek++) {
- for (int sloupec = 0; sloupec < matrix.getWidth(); sloupec++) {
- matrix.setBackground(sloupec, radek,
- (sloupec + radek) % 2 == 0
- ? LightColor.CHESSBOARD_DARK
- : LightColor.CHESSBOARD_LIGHT);
- }
- }
- //vytvarime 12 bilych kamenu
- for (int radek = 0; radek < POCET_RAD; radek++) {
- for (int sloupec = radek % 2; sloupec < matrix.getWidth(); sloupec += 2) {
- poloz(sloupec, radek, Barva.BILY);
- }
- }
- //vytvarime 12 cernych kamenu
- for (int radek = matrix.getHeight() - 1; radek >= matrix.getHeight() - POCET_RAD; radek--) {
- for (int sloupec = radek % 2; sloupec < matrix.getWidth(); sloupec += 2) {
- poloz(sloupec, radek, Barva.CERNY);
- }
- }
- matrix.getInteractiveLightPanel().setUserSelectionMode(
- InteractiveLightPanel.UserSelectionMode.PRESS);
- matrix.getInteractiveLightPanel().setSelectionVisible(true);
- matrix.getInteractiveLightPanel().addSelectListener(new EventListener<LightSelectEvent>() {
- @Override
- public void event(LightSelectEvent event) {
- if (event.getCol() < 0) {
- return;
- }
- if (event.getRow() < 0) {
- return;
- }
- LightColor lc = matrix.getColor(event.getCol(), event.getRow());
- if (lc == null) { //pokud je policko, kam chci tahnout prazdne
- if ((sloupec < 0) || (radek < 0)) {
- matrix.getInteractiveLightPanel().unselect();
- System.out.println("mimo");
- return;
- } else {
- if ((event.getRow() - radek == hrac.getSmer())
- && (Math.abs(sloupec - event.getCol()) == 1)) { //normalni tah
- if (event.getRow() != 0 && event.getRow() != matrix.getHeight() - 1) {
- tah(sloupec, radek, event.getCol(), event.getRow(), matrix.getColor(sloupec, radek));
- } else {
- if (hrac.isWhite()) {
- tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_B.getBarva());
- } else {
- tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_C.getBarva());
- }
- }
- stridaniHracu();
- } else if (hrac.isDama()) { //tah damy
- if (Math.abs(sloupec - event.getCol()) == Math.abs(radek - event.getRow())) {
- int b = 0;
- int x = -1;
- int y = -1;
- for (int a = 1; a < Math.abs(sloupec - event.getCol()) + 1; a++) {
- if (matrix.getColor(
- ((event.getCol() - sloupec) * a) / Math.abs(event.getCol() - sloupec) + sloupec,
- ((event.getRow() - radek) * a) / Math.abs(event.getRow() - radek) + radek) != null) {
- if (b == 0) {
- x = ((event.getCol() - sloupec) * a) / Math.abs(event.getCol() - sloupec) + sloupec;
- y = ((event.getRow() - radek) * a) / Math.abs(event.getRow() - radek) + radek;
- }
- b += 1;
- if (b > 1) {
- System.out.println("neplatny tah");
- return;
- }
- }
- }
- if (x != -1 && y != -1) {
- smazat(x, y);
- }
- tah(sloupec, radek, event.getCol(), event.getRow(), hrac.getBarva());
- //check stridani
- boolean stridani = true;
- x = event.getCol();
- y = event.getRow();
- //nahoru vpravo
- while (x < matrix.getWidth() - 1 && y < matrix.getHeight() - 1) {
- if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
- if (matrix.getColor(x + 1, y + 1) == null) {
- stridani = false;
- }
- }
- x++;
- y++;
- }
- //dolu vlevo
- while (x > 0 && y > 0) {
- if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
- if (matrix.getColor(x - 1, y - 1) == null) {
- stridani = false;
- }
- }
- x--;
- y--;
- }
- x = event.getCol();
- y = event.getRow();
- //dolu vpravo
- while (x < matrix.getWidth() - 1 && y > 0) {
- if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
- if (matrix.getColor(x + 1, y - 1) == null) {
- stridani = false;
- }
- }
- x++;
- y--;
- }
- //nahoru vlevo
- while (x > 0 && y < matrix.getHeight() - 1) {
- if (matrix.getColor(x, y) != null && Barva.valueOf(matrix.getColor(x, y)).isWhite() != hrac.isWhite()) {
- if (matrix.getColor(x - 1, y + 1) == null) {
- stridani = false;
- }
- }
- x--;
- y++;
- }
- if (stridani) {
- stridaniHracu();
- }
- }
- } else { //skok
- int skokRadek = event.getRow() - 2 * hrac.getSmer();
- if (skokRadek == radek) {
- int sloup = -1;
- if (event.getCol() + 2 == sloupec) {
- sloup = event.getCol() + 1;
- } else if (event.getCol() - 2 == sloupec) {
- sloup = event.getCol() - 1;
- }
- if (sloup != -1) {
- LightColor l = matrix.getColor(sloup, skokRadek + hrac.getSmer());
- Barva tempB = Barva.valueOf(l);
- if (!hrac.isWhite() == tempB.isWhite()) {
- if (event.getRow() != 0 && event.getRow() != matrix.getHeight() - 1) {
- tah(sloupec, radek, event.getCol(), event.getRow(), matrix.getColor(sloupec, radek));
- } else {
- if (hrac.isWhite()) {
- tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_B.getBarva());
- } else {
- tah(sloupec, radek, event.getCol(), event.getRow(), Barva.DAMA_C.getBarva());
- }
- }
- smazat(sloup, skokRadek + hrac.getSmer());
- //Kontroluje zda by se melo stridat
- int x = event.getCol() - 1 >= 0 ? event.getCol() - 1 : -1;
- int x2 = event.getCol() + 1 <= matrix.getWidth() ? event.getCol() + 1 : -1;
- int y = event.getRow() + hrac.getSmer() >= matrix.getHeight() || event.getRow() + hrac.getSmer() < 0 ? -1 : event.getRow() + hrac.getSmer();
- boolean stridani = true;
- if (y != -1) {
- //doleva
- if (x != -1) {
- LightColor lc_leva = matrix.getColor(x, y);
- if (lc_leva == null || Barva.valueOf(lc_leva).isWhite() == hrac.isWhite()) {
- stridani = true;
- } else {
- int x3 = x - 1 <= matrix.getWidth() ? x - 1 : -1;
- int y2 = (y + hrac.getSmer() >= matrix.getHeight() || y + hrac.getSmer() < 0) ? -1 : y + hrac.getSmer();
- if (x3 != -1 && y2 != -1) {
- LightColor lc_next = matrix.getColor(x3, y2);
- if (lc_next == null) {
- stridani = false;
- }
- }
- }
- }
- //doprava
- if (x2 != -1) {
- LightColor lc_prava = matrix.getColor(x2, y);
- if (lc_prava == null || Barva.valueOf(lc_prava).isWhite() == hrac.isWhite()) {
- stridani = true;
- } else {
- int x3 = x2 + 1 <= matrix.getWidth() ? x2 + 1 : -1;
- int y2 = (y + hrac.getSmer() >= matrix.getHeight() || y + hrac.getSmer() < 0) ? -1 : y + hrac.getSmer();
- if (x3 != -1 && y2 != -1) {
- LightColor lc_next = matrix.getColor(x3, y2);
- if (lc_next == null) {
- stridani = false;
- }
- }
- }
- }
- }
- if (stridani) {
- stridaniHracu();
- }
- return;
- }
- }
- }
- matrix.getInteractiveLightPanel().unselect();
- System.out.println("neplatny tah");
- }
- return;
- }
- }
- Barva barva = Barva.valueOf(lc);
- if (barva != hrac) {
- if (barva.isWhite() == hrac.isWhite()) {
- hrac = barva;
- } else {
- matrix.getInteractiveLightPanel().unselect();
- System.out.println("hraje " + hrac);
- return;
- }
- }
- sloupec = event.getCol();
- radek = event.getRow();
- }
- });
- }
- private void stridaniHracu() {
- hrac = hrac.isWhite() ? Barva.CERNY : Barva.BILY;
- }
- private void poloz(int sloupec, int radek, Barva barva) {
- matrix.setColor(sloupec, radek, barva.getBarva());
- }
- public void tah(int sloupecOd, int radekOd, int sloupecKam, int radekKam, LightColor b) {
- matrix.setColor(sloupecKam, radekKam, b);
- matrix.setOff(sloupecOd, radekOd);
- }
- public void smazat(int sloupec, int radek) {
- matrix.setOff(sloupec, radek);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement