Κυριακή 14 Μαρτίου 2021

Tetris for 1 or 2 players- not efficient - to show Java is Easy ! 8 hours 1000 lines code

 










/*

 * 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 dntetris;


import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GradientPaint;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.Random;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JSpinner;

import javax.swing.JTextField;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingUtilities;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;


/**

 *

 * @author jimakoskx

 */

public class DNtetris1 extends JFrame{


    static Toolkit tk=Toolkit.getDefaultToolkit();

    static Dimension screenDim=tk.getScreenSize();

    static DNtetris1 players[]=new DNtetris1[2];

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        

        

        

        players[0]=new DNtetris1(0);

        players[1]=new DNtetris1(1);

        

        players[0].oppplayer=players[1];

        players[1].oppplayer=players[0];


        players[1].shrinkToSquareize();

        players[1].setLocation(8*(screenDim.width/16),0);


        

        players[0].setBounds(players[1].getBounds());

        players[0].setLocation(1*(screenDim.width/16),0);



        showJavaEasy();

        

        


    }

    

    static void showJavaEasy(){


        JFrame jfr=new JFrame("...less than 1000 lines of code !!!");

        JPanel jp=new JPanel(){

            public void paintComponent(Graphics gr){

                super.paintComponent(gr);

                Graphics2D g=(Graphics2D)gr;

                int w=getWidth();

                int h=getHeight();

                g.setFont(new Font("",Font.BOLD,Math.min(w, h)/10));

                GradientPaint gp=new GradientPaint(0f,0f,Color.magenta,100f,20f,Color.CYAN,true);

                

                g.setPaint(gp);

                long millis=System.currentTimeMillis();

                millis%=2000;

                //1000 ->Math.PI

                //millis->x

                g.rotate((Math.PI*millis)/1000d,w/2,h/2);

                g.drawString("Java is Easy  .. N.D. & K.D. !!!", 0, h/2);

            }

        };

        jfr.getContentPane().add(jp);

        jfr.setBounds(0,0,screenDim.width,screenDim.height);

        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jfr.setVisible(true);

        

        long start=System.currentTimeMillis();

        long end=start+3000;

        int i=0;

        while(System.currentTimeMillis()<end){

            jp.repaint();

            ++i;

            players[0].sl(10);

        }

        jfr.dispose();  

    }

    

    static JPanel njp(JComponent wcomp,JComponent cpc){

        JPanel out=new JPanel(new BorderLayout());

        out.add(wcomp,BorderLayout.WEST);

        out.add(cpc,BorderLayout.CENTER);

        return out;

    }

    

    

    DNtetris1 oppplayer=null;

    

    int typeBarStraigh[][];//4x4

    int typeSquare[][];//2x2

    int typeCross[][];//3x3

    int typeZ[][];//3x3

    int typeZReversed[][];//3x3

    int typeGamma[][];//3x3

    int typeGammaReversed[][];//3x3

    

    


    IntArray incoming=new IntArray();

    IntArray incomingNext=new IntArray();

    

    

    

    JPanel jp=new JPanel(new BorderLayout());

    

    JPanel np=new JPanel(new BorderLayout());

    JPanel wp=new JPanel(new BorderLayout());

    JPanel cp=new JPanel(new BorderLayout()){

        public void paintComponent(Graphics gr){

            super.paintComponent(gr);

            paintCp(gr);

        }

    };

    JPanel ep=new JPanel(new BorderLayout());

    JPanel sp=new JPanel(new BorderLayout());

    

    

    JSpinner jspTetrisColumns=new JSpinner(new SpinnerNumberModel(10, 10, 30, 1));

    int getSelectedColumns(){return (Integer)jspTetrisColumns.getValue();}

    JSpinner jspTetrisRows=new JSpinner(new SpinnerNumberModel(20,10, 60, 1));

    int getSelectedRows(){return (Integer)jspTetrisRows.getValue();}

    JPanel jpTetrisDim=new JPanel(new BorderLayout());

    

    

    

    JSpinner jspSpeed=new JSpinner(new SpinnerNumberModel(1, 1, 10, 1));

    int getSpeed(){return (Integer)jspSpeed.getValue();}

    long millisOfFrame=1000/getSpeed();

    JPanel jpSpeed=njp(new JLabel("FPS "),jspSpeed);

    

    

    JTextField jtf=new JTextField("Arrows{W,A,S,D} , Roll{Space,Enter}");

    JButton jbStart=new JButton("Start");

    JRadioButton jrbShowVline=new JRadioButton("V-line",false);

    

    Color [] cols={Color.DARK_GRAY,Color.CYAN,Color.YELLOW,Color.MAGENTA,Color.RED,Color.GREEN,Color.ORANGE,Color.BLUE};

    IntArray array=new IntArray(getSelectedColumns()+2,getSelectedRows()+1+4);

    Random r=new Random();

    

    boolean gameIsRunning=false;

    long millisOfHour=1000*60;

    Thread thr=new Thread(){

        public void run(){

            runningGameThread();

        }

    };

    

    

    int downButton,upButton,leftButton,rightButton,spaceButton;

    void setPlayerButtons(int i01){

        if(i01%2==1){

            downButton=KeyEvent.VK_DOWN;

            upButton=KeyEvent.VK_UP;

            leftButton=KeyEvent.VK_LEFT;

            rightButton=KeyEvent.VK_RIGHT;

            spaceButton=KeyEvent.VK_ENTER;

        }

        else{

            downButton=KeyEvent.VK_S;

            upButton=KeyEvent.VK_W;

            leftButton=KeyEvent.VK_A;

            rightButton=KeyEvent.VK_D;

            spaceButton=KeyEvent.VK_SPACE;

            

        }

    }

    

    boolean downIsPressed;

    boolean upIsPressed;

    boolean leftIsPressed;

    boolean rightIsPressed;

    

    //1 line 100 points

    //2 lines 300=200+100 points

    //3 lines 500=300+200 points

    //4 lines 800=400+300 points+100 from tetris action

    

    //continisuslly tetris +1200 !!!

    

    

    

    JTextField jtfTime=new JTextField("00:00:00");

    JTextField jtfScore=new JTextField("123456");

    JTextField jtfTetris=new JTextField(" 0 ");

    JPanel jpScore=new JPanel(new BorderLayout());

    Font fontScore=new Font("",Font.BOLD,24);

    

    long gameStartedAt,gameCurrAt,gameMillis;

    long seconds,minutes,hours;

    long score;

    long tetrisAchieved;

    boolean continiusllyTetris;

    

    

    public DNtetris1(int playerId){

        

        super();

        setTitle("Player _ "+playerId);

        setBounds(screenDim.width/4,50,screenDim.width/2,screenDim.height-100);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        

        setVisible(true);

        

        setPlayerButtons(playerId);//default buttons

        

        jp.add(np,BorderLayout.NORTH);

        jp.add(wp,BorderLayout.WEST);

        jp.add(cp,BorderLayout.CENTER);

        jp.add(ep,BorderLayout.EAST);

        jp.add(sp,BorderLayout.SOUTH);

        

        //cp.setBackground(Color.black);

        

        jpTetrisDim.add(jspTetrisColumns,BorderLayout.WEST);

        jpTetrisDim.add(jspTetrisRows,BorderLayout.EAST);

        

        


        

        JPanel jpSomeUtils=new JPanel(new GridLayout(1,4));

        jpSomeUtils.add(jpSpeed);

        jpSomeUtils.add(jrbShowVline);

        jpSomeUtils.add(njp(new JLabel("cols"),jspTetrisColumns));

        jpSomeUtils.add(njp(new JLabel("rows"),jspTetrisRows));

        

        

        np.add(jpSomeUtils,BorderLayout.NORTH);

        np.add(jbStart,BorderLayout.SOUTH);

        

        

        jtfScore.setFont(fontScore);

        jtfTime.setFont(fontScore);

        jtfTetris.setFont(fontScore);

        

        jpScore.add(jtfTime,BorderLayout.WEST);

        jpScore.add(njp(new JLabel(" Score "), jtfScore),BorderLayout.CENTER);

        jpScore.add(njp(new JLabel(" tetris "), jtfTetris),BorderLayout.EAST);

        np.add(jpScore,BorderLayout.CENTER);

        

       

        

        

        

        

        

        getContentPane().add(jp,BorderLayout.CENTER);

        getContentPane().validate();

        

        

        

        

        thr.setDaemon(true);

        thr.start();

        

        

        

        jbStart.addActionListener(e->jbStart());

        cp.addMouseListener(new MouseListener() {

            @Override

            public void mouseClicked(MouseEvent e) {

            }


            @Override

            public void mousePressed(MouseEvent e) {

                cp.requestFocus();

            }


            @Override

            public void mouseReleased(MouseEvent e) {

            }


            @Override

            public void mouseEntered(MouseEvent e) {

            }


            @Override

            public void mouseExited(MouseEvent e) {

            }

        });

        

        

        cp.addKeyListener(new KeyListener() {

            @Override

            public void keyTyped(KeyEvent e) {

            }


            @Override

            public void keyPressed(KeyEvent e) {

                //System.out.println(""+e);

                if(e.getKeyCode()==downButton){

                    down();

                }

                if(e.getKeyCode()==leftButton){

                    left();

                }

                else if(e.getKeyCode()==rightButton){

                    right();

                }

                if(e.getKeyCode()==spaceButton){

                    space();

                }

                if(oppplayer!=null){

                    if(e.getKeyCode()==oppplayer.downButton){

                        oppplayer.down();

                    }

                    if(e.getKeyCode()==oppplayer.leftButton){

                        oppplayer.left();

                    }

                    else if(e.getKeyCode()==oppplayer.rightButton){

                        oppplayer.right();

                    }

                    if(e.getKeyCode()==oppplayer.spaceButton){

                        oppplayer.space();

                    }

                }

            }


            @Override

            public void keyReleased(KeyEvent e) {

                if(e.getKeyCode()==downButton){

                    downIsPressed=false;

                }

                if(oppplayer!=null){

                    if(e.getKeyCode()==oppplayer.downButton){

                        oppplayer.downIsPressed=false;

                    }

                }


            }

        });

        ChangeListener ColsAndRowsCL=new ChangeListener() {

            @Override

            public void stateChanged(ChangeEvent e) {

                tetrisArrayChanged();

            }

        };

        jspTetrisColumns.addChangeListener(ColsAndRowsCL);

        jspTetrisRows.addChangeListener(ColsAndRowsCL);

        

        jrbShowVline.addActionListener(e->cp.requestFocus());

        

        jspSpeed.addChangeListener(new ChangeListener() {

            @Override

            public void stateChanged(ChangeEvent e) {

                millisOfFrame=1000/getSpeed();

                cp.requestFocus();

            }

        });

        

    }

    

    void down(){

        if(gameIsRunning){

            downIsPressed=true;

            thr.interrupt();

            //System.out.println("Interrupted for down ");

        }

    }


    void left(){

        if(gameIsRunning){

            leftIsPressed=true;

            refreshTempRoll(incoming,false);

            --temp.col;

            //System.out.println(incoming.col+" left to "+temp.col);

            if(!collisionDetected(temp)){

                incoming.copyFrom(temp);

                cp.repaint();

            }else{tk.beep();}       

        }

    }

    void right(){

        if(gameIsRunning){

            rightIsPressed=true;

            refreshTempRoll(incoming,false);

            ++temp.col;

            //System.out.println(incoming.col+" right to"+temp.col);

            if(!collisionDetected(temp)){

                incoming.copyFrom(temp);

                cp.repaint();

            }else{tk.beep();}

        }

    }

    void space(){

        if(gameIsRunning){

            //System.out.println("space");

            refreshTempRoll(incoming,true);

            if(!collisionDetected(temp)){

                incoming.copyFrom(temp);

                cp.repaint();

            }else{tk.beep();}

        }        

    }

    

    void shrinkToSquareize(){

        sl(123);

        tetrisArrayChanged();

        System.out.println(sqw+" , "+sqh);

        if(sqw-sqh>3){ 

            while(sqw-sqh>3){//System.out.println("We are hor flat");

                setSize(getWidth()-3, getHeight());

                repaint();

                sl(23);

                tetrisArrayChanged();

                //System.out.println(sqw+" , "+sqh+" :Shrinking Width");

            }

        }

        else if(sqh-sqw>3){

            while(sqh-sqw>3){

                setSize(getWidth(), getHeight()-3);

                repaint();

                sl(23);

                tetrisArrayChanged();

                //System.out.println(sqw+" , "+sqh+" :Shrinking Height");

            }

        }

        //else{System.out.println("equals");}


    }

    

    

    void tetrisArrayChanged(){

        int newcols=getSelectedColumns()+2;

        int newrows=getSelectedRows()+1+4;


        boolean gameWASruning=gameIsRunning;

        if(gameIsRunning){

            gameIsRunning=false;

            thr.interrupt();

        }

        array.setSize(newcols,newrows);

        array.setAll(-1);

        randomiseArrayForDebug();

        cp.repaint();

        if(gameWASruning){

            jbStart();

        }

    }


    void createWalls(){

        int i=0;

        int j=array.ar[0].length-1;

        while(i<array.ar.length){

            array.ar[i][j]=0;

            ++i;

        } 

        j=4;

        while(j<array.ar[0].length){

            array.ar[0][j]=0;

            array.ar[array.ar.length-1][j]=0;

            ++j;

        } 

    }

    void randomiseArrayForDebug(){

        int fromRow=r.nextInt(array.ar[0].length);

        int i=0;

        while(i<array.ar.length){

            int j=fromRow;

            while(j<array.ar[0].length){

                if(r.nextInt(4)%3!=0){

                    array.ar[i][j]=r.nextInt(cols.length);

                }

                ++j;

            }

            ++i;

        } 

    }

    

    

    void jbStart(){

        if(gameIsRunning){

            System.out.println("Abort running");

            gameIsRunning=false;

        }

        else{

            System.out.println("Restarting");

            

            gameLost=false;

            constructed=-1;

            score=0;

            tetrisAchieved=0;

            continiusllyTetris=false;

            

            array.setAll(-1);

            createWalls();

            cp.repaint();

            


            reconstructNext();

            copyNextToCurr();

            reconstructNext();


            

            cp.requestFocus();

            gameIsRunning=true;

            gameStartedAt=System.currentTimeMillis();

            refreshScore();

        }

        thr.interrupt();

    }

    void refreshScore(){

        gameCurrAt=System.currentTimeMillis();

        gameMillis=gameCurrAt-gameStartedAt;

        gameMillis/=1000;

        seconds=gameMillis%60;

        gameMillis/=60;

        minutes=gameMillis%60;

        gameMillis/=24;

        hours=gameMillis%24;


        String hms=hours+":";  

        if(minutes<10){hms+="0"+minutes;}

        else{hms+=""+minutes;}

        if(seconds<10){hms+=":0"+seconds;}

        else{hms+=":"+seconds;}

        

        jtfTime.setText(hms);

        jtfScore.setText(" "+score);

        jtfTetris.setText(" "+tetrisAchieved);

    }

    void copyNextToCurr(){

        incoming.copyFrom(incomingNext);

        incoming.col=array.ar.length/2-2;

        incoming.row=0;

    }

    

    int constructed=0;


    void reconstructNext(){

        incomingNext.col=array.ar.length-4;

        incomingNext.row=0;

        ++constructed;

        jbStart.setText(constructed+" , ");

        incomingNext.type=r.nextInt(7);

        //incomingNext.type=6;//debug

            

        incomingNext.subtype=0;

        

        if(incomingNext.type==0){//straigh bar

            incomingNext.setSize(4,4);

            incomingNext.subtypes=2;//

        }

        else if(incomingNext.type==1){//square

            incomingNext.setSize(2,2);

            incomingNext.subtypes=0;//

        }

        else if(incomingNext.type==2){//cross

            incomingNext.setSize(3,3);

            incomingNext.subtypes=4;//

        }

        else if(incomingNext.type==3){//zet

            incomingNext.setSize(3,3);

            incomingNext.subtypes=2;//

        }

        else if(incomingNext.type==4){//zet reversed

            incomingNext.setSize(3,3);

            incomingNext.subtypes=2;//

        }

        else if(incomingNext.type==5){//gamma

            incomingNext.setSize(3,3);

            incomingNext.subtypes=4;//

        }

        else if(incomingNext.type==6){//gamma reversed

            incomingNext.setSize(3,3);

            incomingNext.subtypes=4;//

        }

        refreshTempRoll(incomingNext,false);

        incomingNext.copyFrom(temp);


    }

    

    

    

    IntArray temp=new IntArray();

    

    void refreshTempRoll(IntArray some,boolean roll){

        temp.copyBasicsFrom(some);

        temp.setAll(-1);

        int cc=temp.type+1;

        if(temp.type==0){//bar

            if(roll){

                temp.subtype++;

                temp.subtype%=temp.subtypes;

            }

            if(temp.subtype==0){//bar horizontal

                temp.ar[0][0]=cc;

                temp.ar[1][0]=cc;

                temp.ar[2][0]=cc;

                temp.ar[3][0]=cc;

            }

            else {//bar vertical

                temp.ar[0][0]=cc;

                temp.ar[0][1]=cc;

                temp.ar[0][2]=cc;

                temp.ar[0][3]=cc;

            }

        }

        else if(temp.type==1){//cross

            temp.ar[0][0]=cc;

            temp.ar[0][1]=cc;

            temp.ar[1][0]=cc;

            temp.ar[1][1]=cc;

        }

        else if(temp.type==2){//cross

            if(roll){

                temp.subtype++;

                temp.subtype%=temp.subtypes;

            }

            if(temp.subtype==0){

                temp.ar[0][0]=cc;

                temp.ar[1][0]=cc;

                temp.ar[2][0]=cc;

                temp.ar[1][1]=cc;

            }

            else if(temp.subtype==1){

                temp.ar[2][0]=cc;

                temp.ar[2][1]=cc;

                temp.ar[2][2]=cc;

                temp.ar[1][1]=cc;

            }

            else if(temp.subtype==2){

                temp.ar[0][1]=cc;

                temp.ar[1][1]=cc;

                temp.ar[2][1]=cc;

                temp.ar[1][0]=cc;

            }

            else if(temp.subtype==3){

                temp.ar[0][0]=cc;

                temp.ar[0][1]=cc;

                temp.ar[0][2]=cc;

                temp.ar[1][1]=cc;

            }

        }

        else if(temp.type==3){//zet

            if(roll){

                temp.subtype++;

                temp.subtype%=temp.subtypes;

            }

            if(temp.subtype==0){

                temp.ar[0][0]=cc;

                temp.ar[1][0]=cc;

                temp.ar[1][1]=cc;

                temp.ar[2][1]=cc;

            }

            else if(temp.subtype==1){

                temp.ar[1][0]=cc;

                temp.ar[0][1]=cc;

                temp.ar[1][1]=cc;

                temp.ar[0][2]=cc;

            }

        }

        else if(temp.type==4){//reversed zet

            if(roll){

                temp.subtype++;

                temp.subtype%=temp.subtypes;

            }

            if(temp.subtype==0){

                temp.ar[1][0]=cc;

                temp.ar[2][0]=cc;

                temp.ar[0][1]=cc;

                temp.ar[1][1]=cc;

            }

            else if(temp.subtype==1){

                temp.ar[0][0]=cc;

                temp.ar[0][1]=cc;

                temp.ar[1][1]=cc;

                temp.ar[1][2]=cc;

            }

        }

        else if(temp.type==5){//gamma

            if(roll){

                temp.subtype++;

                temp.subtype%=temp.subtypes;

            }

            if(temp.subtype==0){

                temp.ar[0][0]=cc;//***

                temp.ar[1][0]=cc;//*

                temp.ar[2][0]=cc;

                temp.ar[0][1]=cc;

            }

            else if(temp.subtype==1){

                temp.ar[0][0]=cc;//**

                temp.ar[1][0]=cc;// *

                temp.ar[1][1]=cc;// *

                temp.ar[1][2]=cc;

            }

            else if(temp.subtype==2){

                temp.ar[2][0]=cc;//  *

                temp.ar[0][1]=cc;//***

                temp.ar[1][1]=cc;

                temp.ar[2][1]=cc;

            }

            else if(temp.subtype==3){

                temp.ar[0][0]=cc;//*

                temp.ar[0][1]=cc;//*

                temp.ar[0][2]=cc;//**

                temp.ar[1][2]=cc;

            }

        }

        else if(temp.type==6){//gamma reversed

            if(roll){

                temp.subtype++;

                temp.subtype%=temp.subtypes;

            }

            if(temp.subtype==0){

                temp.ar[0][0]=cc;//***

                temp.ar[1][0]=cc;//  *

                temp.ar[2][0]=cc;

                temp.ar[2][1]=cc;

            }

            else if(temp.subtype==1){

                temp.ar[1][0]=cc;// *

                temp.ar[1][1]=cc;// *

                temp.ar[0][2]=cc;//**

                temp.ar[1][2]=cc;

            }

            else if(temp.subtype==2){

                temp.ar[0][0]=cc;//* 

                temp.ar[0][1]=cc;//***

                temp.ar[1][1]=cc;

                temp.ar[2][1]=cc;

            }

            else if(temp.subtype==3){

                temp.ar[0][0]=cc;//**

                temp.ar[1][0]=cc;//*

                temp.ar[0][1]=cc;//*

                temp.ar[0][2]=cc;

            }

        }

    }

    

    

    

    InterruptedException sl(long millis){

        try{

            Thread.sleep(millis);

        }catch(InterruptedException inter){

            return inter;

        }

        return null;

    }

    void runningGameThread(){

        while(true){

            if(gameIsRunning){

                //System.out.println("Running");

                //System.out.println(incoming.col+" "+incoming.row);

                if(increaseRow()){

                    while(downIsPressed){

                        if(!increaseRow()){

                            break;

                        }

                        cp.repaint();

                        sl(66);

                    }

                }

                cp.repaint();

                sl(millisOfFrame);

            }

            else{

                sl(1000*60);

            }

        }

        

    }

    boolean increaseRow(){

        incoming.row++;

        if(incoming.row>=array.ar[0].length-1  || collisionDetected(incoming)){//full down...must store

            --incoming.row;///decrease and store as it was

            storeIncoming();

            checkToBreak();

            refreshScore();

            return false;

        }

        else{

            //continue moving down

            return true;

        }

        

    }

    void storeIncoming(){

        System.out.println("storing");

        int i=0;

        while(i<incoming.ar.length){

            int j=0;

            while(j<incoming.ar[0].length){

                if(incoming.ar[i][j]>0){

                    array.ar[incoming.col+i][incoming.row+j]=incoming.ar[i][j];

                }

                ++j;

            }

            ++i;

        }

        

        copyNextToCurr();

        if(collisionDetected(incoming)){

            System.out.println("LOST.....");

            gameLost=true;

            cp.repaint();

            gameIsRunning=false;

            jbStart.setText("Start");

        }

        else{

            reconstructNext();

            

        }

        

    }


    boolean collisionDetected(IntArray some){

        int i=0,ti,tj;

        while(i<some.ar.length){

            int j=0;

            while(j<some.ar[0].length){

                if(some.ar[i][j]>0){

                    if(some.row+j>=array.ar[0].length-1){

                        System.out.println(i+","+j+" : Collision on Bottom");

                        return true;

                    }

                    ti=some.col+i;

                    if(ti<0 || ti>=array.ar.length){

                        System.out.println(i+","+j+" : Collision Out of Width bounds ");

                        return true;

                    }

                    tj=some.row+j;

                    if(tj<0 || tj>=array.ar[0].length){

                        System.out.println(i+","+j+" : Collision Out of Heigh bounds ");

                        return true;

                    }

                    if(array.ar[ti][tj]>=0){

                        System.out.println(i+","+j+" : Collision ");

                        return true;

                    }

                }

                ++j;

            }

            ++i;

        }

        return false;

    }

    

    

    

    

    

    void checkToBreak(){

        

        int line=array.ar[0].length-2;

        int linesFull=0;

        

        int i,iend=array.ar.length-1;

        int scoring=0,scoringPlus=0;

        while(line>3){

            i=1;

            while(i<iend){

                if(array.ar[i][line]<0){

                    break;

                }

                ++i;

            }

            if(i==iend){

                ++linesFull;

                System.out.println("Removing line : "+line);

                --i;

                while(i>0){

                    array.ar[i][line]=-1;

                    --i;

                }

                BringDownAllAbove(line);

                ++line;

                scoring+=100;

                scoring+=scoringPlus;

                scoringPlus+=100;

            }

            

            

            --line;

        }

        if(linesFull>0){

            System.out.println(linesFull+ " Lines Removed for $ "+scoring);

            score+=scoring;

            if(linesFull==4){

                ++tetrisAchieved;

                System.out.println("new Tetris Achived !");

                if(continiusllyTetris){

                    score+=1200;

                    System.out.println("+1200 from continiussly tetris !!!");

                }

                continiusllyTetris=true;

            }

            else{

                continiusllyTetris=false;

            }

        }

    }

    

    void BringDownAllAbove(int line){

        int linePrev=line-1;

        while(line>0){

            int i=array.ar.length-2;

            while(i>0){

                array.ar[i][line]=array.ar[i][linePrev];

                --i;


            }

            --line;

            --linePrev;

        }

        

    }

    

    

    

    

    

    

    

    

    

    int w,h,sqw,sqh;

    Graphics2D g;


    void paintCp(Graphics gr){

        g=(Graphics2D)gr;

        //g.drawLine(0,0,100,100);

        w=cp.getWidth();

        h=cp.getHeight();

        

        sqw=w/array.ar.length;

        sqh=h/array.ar[0].length;

        

        drawArray(array, 0, 0);

        if(gameIsRunning){

            drawArray(incoming, incoming.col*sqw, incoming.row*sqh);

            if(!gameLost){

                drawArray(incomingNext, incomingNext.col*sqw, incomingNext.row*sqh);

                

                if(jrbShowVline.isSelected()){

                    g.drawLine(incoming.col*sqw, incoming.row*sqh, incoming.col*sqw, h);

                }

            }

        }

        //if(gameLost){

            g.setFont(new Font("",Font.BOLD,Math.min(sqw,sqh)));

            g.drawString(""+constructed,sqw/3,sqh);

        //}        

        

        

    }

    boolean gameLost;

    void drawArray(IntArray some,int atw,int ath){

        

        int currAtw=atw;

        int currAth=ath;

        int i=0;

        while(i<some.ar.length){

            int j=0;

            currAth=ath;

            while(j<some.ar[0].length){

                if(some.ar[i][j]>=0){

                    g.setColor(cols[some.ar[i][j]]);

                    g.fillRect(currAtw, currAth, sqw,sqh);

                    g.setColor(Color.BLACK);

                    g.drawRect(currAtw, currAth, sqw,sqh);

                    if(gameLost){

                        g.drawLine(currAtw, currAth,currAtw+sqw, currAth+sqh);

                    }

                    

                }

                currAth+=sqh;

                if(i==0 && jrbShowVline.isSelected()){

                    g.drawString(i+" , "+j,currAtw, currAth);

                }

                ++j;

            }

            ++i;

            currAtw+=sqw;

        }

    }

    

}




















class IntArray {

    int type=0;

    int subtype=0;

    int subtypes=0;

    int row=0,col=0;

    

    int ar[][];

    public IntArray(int cols,int rows){

        setSize(cols, rows);

    }

    public IntArray(){

        this(1,1);

    }

    void setSize(int cols,int rows){

        ar=new int[cols][rows];

    }

    

    void copyBasicsFrom(IntArray other){

        type=other.type;

        subtype=other.subtype;

        subtypes=other.subtypes;

        row=other.row;

        col=other.col;

        ///???????????????????????????

        if(other.ar.length!=ar.length || other.ar[0].length!=ar[0].length){

            ar=new int[other.ar.length][other.ar[0].length];

        }

       

    }

    void copyArrayFrom(IntArray other){

        int i=0,j;

        while(i<ar.length){

            j=0;

            while(j<ar[0].length){

                ar[i][j]=other.ar[i][j];

                ++j;

            }

            ++i;

        }

    }

    void copyFrom(IntArray other){

        copyBasicsFrom(other);

        copyArrayFrom(other);

    }

    

    void setAll(int value){

        int i=0,j;

        while(i<ar.length){

            j=0;

            while(j<ar[0].length){

                ar[i][j]=value;

                ++j;

            }

            ++i;

        }

        

    }

    

}


Δεν υπάρχουν σχόλια: