import javax.swing.*; import java.awt.*; import java.util.Random; import java.util.ArrayList; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Scanner; import java.io.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.Scanner; public class rotation extends JFrame implements KeyListener { private int feld[][] = // Feld... duh { { -1, -1, -1, -1, -1 }, // -1=wand, 0=leeres feld, 1-3=bloecke, -2=ausgang { -1, 0, 1, 1, -1 }, { -1, 2, 0, 3, -1 }, { -1, 2, 0, 3, -1 }, { -1, -1, -2, -1, -1 } }; private int fl, ppX, len; // laengen und tmp ints public boolean cheggin, moved, canMove; Scanner scan; public static String filename; public String[] array, tmpArray; public String[][] sArray; ArrayList mBlocks = new ArrayList(); public static void main(String[] args) { if (args.length != 0) { filename = args[0]; } new rotation(); } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { System.out.println("Key: " + e.getKeyChar()); if (e.getKeyChar() == 'a') { ld(); } if (e.getKeyChar() == 'd') { rd(); } } @Override public void keyReleased(KeyEvent e) { } public void readFile() { try { scan = new Scanner(new File(filename)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void createString() { List listOfStrings = new ArrayList(); // load the data from file try { listOfStrings = Files.readAllLines(Paths.get(filename)); } catch (IOException e) { } // convert arraylist to array array = listOfStrings.toArray(new String[0]); for (int i = 0; i < array.length; i++) { array[i] = array[i].replace(' ', 'a'); } System.out.println(array[3]); } public void dimension() { tmpArray = new String[array.length]; sArray = new String[array.length][array.length]; for (int i = 0; i < array.length; i++) { scan = new Scanner(array[i]).useDelimiter("\\s*\\s*"); int j = 0; while (scan.hasNext()) { tmpArray[j] = scan.next(); // tmpArray[j] = tmpArray[j].replace("#","-1"); tmpArray[j] = tmpArray[j].replace("a", "99"); System.out.println(tmpArray[j]); j++; for (int k = 0; k < tmpArray.length; k++) { System.out.println(tmpArray[k]); } } sArray[i] = tmpArray; } System.out.println(sArray[0][0]); System.out.println(sArray[4][4]); for (int x = 0; x < sArray.length; x++) { for (int y = 0; y < sArray.length; y++) { if (sArray[y][x] == "#") { sArray[y][x] = "-1"; } } } } public void fill() { feld = new int[sArray.length][sArray.length]; for (int x = 0; x < sArray.length; x++) { for (int y = 0; y < sArray.length; y++) { feld[y][x] = Integer.parseInt(sArray[y][x]); System.out.println(feld[y][x]); if (feld[y][x] == 99) { feld[y][x] = 0; } else if (feld[y][x] >= 0) { feld[y][x]++; } System.out.println(feld[y][x]); } } printNew(); } public void printNew() { for (int x = 0; x < sArray.length; x++) { for (int y = 0; y < sArray.length; y++) { System.out.print(sArray[x][y] + " \t"); } System.out.println(); } } public rotation() { setSize(800, 800); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); // anzeigenTxt(); setBackground(new Color(0, 0, 0)); addKeyListener(this); if (filename != null) { readFile(); createString(); dimension(); fill(); } richten(); fl = feld.length; } public void rd() { rotateClockwise(); checkFall(); } public void ld() { // TODO: BS rotateClockwise(); rotateClockwise(); rotateClockwise(); checkFall(); } public void richten() { rotateClockwise(); fl = feld.length; int[][] matrixNeu = new int[feld.length][feld.length]; for (int m = 0; m < 5; m++) { for (int l = 0; l < 5; l++) { matrixNeu[m][l] = feld[fl - 1 - m][l]; } } feld = matrixNeu; repaint(); } public void checkFall() { moved = false; mBlocks.clear(); for (int y = 0; y < fl; y++) { for (int x = 0; x < fl; x++) { if (feld[x][y] > 0 && movedBlocks(x, y) == false) { mBlocks.add(feld[x][y]); fall(x, y, feld[x][y]); repaint(); // System.out.println("yeet"); } } } } public boolean movedBlocks(int px, int py) { // DONE check array-list, ob zahl schon abgefragt wurde if (mBlocks.contains(feld[px][py])) { return true; } else { return false; } } public void fall(int pX, int pY, int block) { if (horizontal(pX, pY, block) == true) { fallH(pX, pY, block); } else { fallV(pX, pY, block); } } public boolean horizontal(int pX, int pY, int block) { if (feld[pX + 1][pY] == block) { return true; } else { return false; } } public void fallH(int pX, int pY, int block) { ppX = pX; len = 0; while (feld[ppX][pY] == block) { // System.out.println("ppX"+ppX+"py"+pY+"block"+block); len++; ppX++; } canMove = true; for (int i = 0; i < len; i++) { if (feld[pX + i][pY + 1] != 0) { canMove = false; } } if (canMove == true) { for (int i = 0; i < len; i++) { feld[pX + i][pY + 1] = block; feld[pX + i][pY] = 0; wait(100); repaint(); } checkFall(); return; } } public void fallV(int pX, int pY, int block) { cheggin = true; int i = 0; int ppY = pY; while (cheggin == true) { if (feld[pX][ppY] != block) { cheggin = false; } else { ppY++; } } // DONE siegesabfrage // System.out.println("ppy:"+ppY+"px:"+pX); if (feld[pX][ppY] == -2) { delete(block); } if (feld[pX][ppY] == 0) { for (int j = ppY; j > pY; j--) { feld[pX][j] = feld[pX][j - 1]; feld[pX][j - 1] = 0; wait(100); repaint(); } checkFall(); return; } else { return; } } public void delete(int input) { int[][] matrixNeu = new int[feld.length][feld.length]; matrixNeu = feld; for (int m = 0; m < fl; m++) { for (int l = 0; l < fl; l++) { if (matrixNeu[m][l] == input) { matrixNeu[m][l] = 0; wait(50); } } } feld = matrixNeu; repaint(); } public void paint(Graphics g) { feldAnzeigen(g); } public void neuzeichnen() { repaint(); } public void anzeigen(Graphics g) { feldAnzeigen(g); } public void feldAnzeigen(Graphics g) { for (int j = 0; j < 5; j++) { for (int i = 0; i < 5; i++) { if (feld[j][i] == 0) { g.setColor(new Color(255, 255, 255)); } if (feld[j][i] == -1) { g.setColor(new Color(100, 100, 100)); } if (feld[j][i] == 1) { g.setColor(new Color(255, 0, 0)); } if (feld[j][i] == 2) { g.setColor(new Color(0, 255, 0)); } if (feld[j][i] == 3) { g.setColor(new Color(0, 0, 255)); } if (feld[j][i] == -2) { g.setColor(new Color(255, 215, 0)); } g.fillRect(100 + 42 * j, 100 + 42 * i, 40, 40); } } } public void anzeigenTxt() { for (int xpos = 0; xpos < feld.length; xpos++) { for (int ypos = 0; ypos < feld[0].length; ypos++) { System.out.println(feld[xpos][ypos] + " x:" + xpos + " y:" + ypos); } } } public void wait(int milliseconds) { try { Thread.sleep(milliseconds); } catch (Exception e) { // ignoring exception at the moment } } void rotateClockwise() { int[][] matrixNeu = new int[feld.length][feld.length]; for (int m = 0; m < 5; m++) { for (int l = 0; l < 5; l++) { matrixNeu[feld.length - 1 - m][l] = feld[l][m]; } } feld = matrixNeu; repaint(); } }