348 lines
6.1 KiB
Java
348 lines
6.1 KiB
Java
|
|
|
|
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.*;
|
|
|
|
|
|
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=feld.length, ppX, len; //laengen und tmp ints
|
|
public boolean cheggin, moved, canMove;
|
|
Scanner scan;
|
|
public static String filename;
|
|
|
|
|
|
ArrayList<Integer> mBlocks = new ArrayList<Integer>();
|
|
|
|
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(String filename){
|
|
try{
|
|
scan = new Scanner(new File(filename));
|
|
}
|
|
catch (FileNotFoundException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public rotation() {
|
|
|
|
setSize(800,800);
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
setLocationRelativeTo(null);
|
|
setVisible(true);
|
|
anzeigenTxt();
|
|
setBackground(new Color(0,0,0));
|
|
addKeyListener(this);
|
|
richten();
|
|
}
|
|
|
|
|
|
|
|
public void rd() {
|
|
|
|
rotateClockwise();
|
|
checkFall();
|
|
|
|
}
|
|
|
|
public void ld() {
|
|
|
|
//TODO: BS
|
|
rotateClockwise();
|
|
rotateClockwise();
|
|
rotateClockwise();
|
|
checkFall();
|
|
|
|
}
|
|
|
|
|
|
public void richten(){
|
|
|
|
rotateClockwise();
|
|
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
}
|