Wednesday, October 8, 2014

Arrang16 using JSwing.

JSwing practice with old game.

Thanks to Suraj Jachak and Pooja Chavan for making me do this.

Special thanks to Gayatri Sawale for helping me when I was writing this highly inefficient code for of the game we (Gayatri and me) called Arrange16. Some of you may know it from windows7 desktop Gadgets.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

import java.util.Collections;
class  Arrange16{

    public static void main(String[] args){
    JFrame f=new JFrame();
  
 
    final JButton button1=new JButton("1");
    final JButton button2=new JButton("2");
    final JButton button3=new JButton("3");
    final JButton button4=new JButton("4");

    final JButton button5=new JButton("5");
    final JButton button6=new JButton("6");
    final JButton button7=new JButton("7");
    final JButton button8=new JButton("8");

    final JButton button9=new JButton("9");
    final JButton button10=new JButton("10");
    final JButton button11=new JButton("11");
    final JButton button12=new JButton("12");

    final JButton button13=new JButton("13");
    final JButton button14=new JButton("14");
    final JButton button15=new JButton("15");
    final JButton button16=new JButton(" ");



ArrayList<Integer> numbers = new ArrayList<Integer>();
     for(int i = 0; i < 15; i++)
     {
     numbers.add(i+1);
   }

Collections.shuffle(numbers);

    for(int j =0; j < 15; j++)
         {
        String str = Integer.toString(numbers.get(j));
            if(j==0)
        button1.setText(str);
        if(j==1)
        button2.setText(str);
        if(j==2)
        button3.setText(str);
        if(j==3)
        button4.setText(str);
        if(j==4)
        button5.setText(str);
        if(j==5)
        button6.setText(str);
        if(j==6)
        button7.setText(str);
        if(j==7)
        button8.setText(str);
        if(j==8)
        button9.setText(str);
        if(j==9)
        button10.setText(str);
        if(j==10)
        button11.setText(str);
        if(j==11)
        button12.setText(str);
        if(j==12)
        button13.setText(str);
        if(j==13)
        button14.setText(str);
        if(j==14)
        button15.setText(str);
      
    }      



    try{

       
           button1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button2.getText()==" "){
            button2.setText(button1.getText());
            button1.setText(" ");
            }

            if(button5.getText()==" "){
            button5.setText(button1.getText());
            button1.setText(" ");
            }

                    

               }
            });

    button2.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button1.getText()==" "){
            button1.setText(button2.getText());
            button2.setText(" ");
            }

            if(button3.getText()==" "){
            button3.setText(button2.getText());
            button2.setText(" ");
            }

            if(button6.getText()==" "){
            button6.setText(button2.getText());
            button2.setText(" ");
            }

                    

               }
            });

    button3.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button2.getText()==" "){
            button2.setText(button3.getText());
            button3.setText(" ");
            }

            if(button4.getText()==" "){
            button4.setText(button3.getText());
            button3.setText(" ");
            }

            if(button7.getText()==" "){
            button7.setText(button3.getText());
            button3.setText(" ");
            }

                    
               }
            });

    button4.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button3.getText()==" "){
            button3.setText(button4.getText());
            button4.setText(" ");
            }

            if(button8.getText()==" "){
            button8.setText(button4.getText());
            button4.setText(" ");
            }

                    

               }
            });

    button5.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button1.getText()==" "){
            button1.setText(button5.getText());
            button5.setText(" ");
            }

            if(button6.getText()==" "){
            button6.setText(button5.getText());
            button5.setText(" ");
            }

            if(button9.getText()==" "){
            button9.setText(button5.getText());
            button5.setText(" ");
            }

                    
               }
            });


    button6.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button2.getText()==" "){
            button2.setText(button6.getText());
            button6.setText(" ");
            }

            if(button5.getText()==" "){
            button5.setText(button6.getText());
            button6.setText(" ");
            }

            if(button7.getText()==" "){
            button7.setText(button6.getText());
            button6.setText(" ");
            }

            if(button10.getText()==" "){
            button10.setText(button6.getText());
            button6.setText(" ");
            }

                    
               }
            });

    button7.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button3.getText()==" "){
            button3.setText(button7.getText());
            button7.setText(" ");
            }

            if(button6.getText()==" "){
            button6.setText(button7.getText());
            button7.setText(" ");
            }

            if(button8.getText()==" "){
            button8.setText(button7.getText());
            button7.setText(" ");
            }

            if(button11.getText()==" "){
            button11.setText(button7.getText());
            button7.setText(" ");
            }

                    
               }
            });

    button8.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button4.getText()==" "){
            button4.setText(button8.getText());
            button8.setText(" ");
            }

            if(button7.getText()==" "){
            button7.setText(button8.getText());
            button8.setText(" ");
            }

            if(button12.getText()==" "){
            button12.setText(button8.getText());
            button8.setText(" ");
            }
                    
               }
            });

    button9.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button5.getText()==" "){
            button5.setText(button9.getText());
            button9.setText(" ");
            }

            if(button10.getText()==" "){
            button10.setText(button9.getText());
            button9.setText(" ");
            }

            if(button13.getText()==" "){
            button13.setText(button9.getText());
            button9.setText(" ");
            }
                    
               }
            });

    button10.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button9.getText()==" "){
            button9.setText(button10.getText());
            button10.setText(" ");
            }

            if(button6.getText()==" "){
            button6.setText(button10.getText());
            button10.setText(" ");
            }

            if(button14.getText()==" "){
            button14.setText(button10.getText());
            button10.setText(" ");
            }

            if(button11.getText()==" "){
            button11.setText(button10.getText());
            button10.setText(" ");
            }

                    
               }
            });

    button11.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button7.getText()==" "){
            button7.setText(button11.getText());
            button11.setText(" ");
            }

            if(button10.getText()==" "){
            button10.setText(button11.getText());
            button11.setText(" ");
            }

            if(button12.getText()==" "){
            button12.setText(button11.getText());
            button11.setText(" ");
            }

            if(button15.getText()==" "){
            button15.setText(button11.getText());
            button11.setText(" ");
            }

                    
               }
            });

    button12.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button8.getText()==" "){
            button8.setText(button12.getText());
            button12.setText(" ");
            }

            if(button11.getText()==" "){
            button11.setText(button12.getText());
            button12.setText(" ");
            }

            if(button16.getText()==" "){
            button16.setText(button12.getText());
            button12.setText(" ");
            }

                    
               }
            });

    button13.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button9.getText()==" "){
            button9.setText(button13.getText());
            button13.setText(" ");
            }

            if(button14.getText()==" "){
            button14.setText(button13.getText());
            button13.setText(" ");
            }
 
               }
            });

    button14.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button10.getText()==" "){
            button10.setText(button14.getText());
            button14.setText(" ");
            }

            if(button13.getText()==" "){
            button13.setText(button14.getText());
            button14.setText(" ");
            }

            if(button15.getText()==" "){
            button15.setText(button14.getText());
            button14.setText(" ");
            }

                    
               }
            });

    button15.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button11.getText()==" "){
            button11.setText(button15.getText());
            button15.setText(" ");
            }

            if(button14.getText()==" "){
            button14.setText(button15.getText());
            button15.setText(" ");
            }

            if(button16.getText()==" "){
            button16.setText(button15.getText());
            button15.setText(" ");
            }

                    
               }
            });

    button16.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){

            if(button12.getText()==" "){
            button12.setText(button16.getText());
            button16.setText(" ");
            }

            if(button15.getText()==" "){
            button15.setText(button16.getText());
            button16.setText(" ");
            }                    
               }
            });
        }
        catch(Exception e){
        }
        JPanel p=new JPanel(new GridLayout(4,4));

     p.add(button1);
    p.add(button2);
    p.add(button3);
    p.add(button4);

    p.add(button5);
    p.add(button6);
    p.add(button7);
    p.add(button8);

    p.add(button9);
    p.add(button10);
    p.add(button11);
    p.add(button12);

    p.add(button13);
    p.add(button14);
    p.add(button15);
    p.add(button16);

        f.add(p);
        f.setVisible(true);
        f.pack();
    }
}

Thursday, January 23, 2014

Pushbox game in java.

Source code for the Game I designed with Kaustubh Kumbhar. Feel free to modify it and use it for educational and non-profit purpose.

How to use.
use appletviewer to run the code.
keep caps on while playing. 

Violet square is Hero. (tht little moving character in games we like to call hero)
Black square is hurdle. 
Yellow square is a box and you must push all yellow boxes into black squares.
Red square is wall.

Rules:
  • Movement of Hero is done by using keys w as up, a as left, s as down, d as right.
  • Hero is restricted through walls ans hurdles.
  • Hero can push only one object at a time.
  • Remember only pushing is possible pulling is restricted 
  • When you push box in hurdles both the object and hurdles will disappear and the position acquired by hurdle before pushing will become free space.
  • Play keeping caps on.

C:\jdk\bin\javac appjpushbox.java

C:\jdk\bin\appletviewer appjpushbox.java

/*
<applet code="appjpushbox.class" height=500 width=500>
</applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import javax.swing.*;

public class appjpushbox extends Applet implements KeyListener     
{
char a[][]=new char[11][9];
char temp;
int i,j,x1,y1,upos,final1;

public void init()
{
addKeyListener(this);
//Designing the Stage 
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
a[i][j]='a';
}
}
for(i=0;i<11;i++)
{
a[i][0]='H';
a[i][8]='H';
}
for(i=0;i<9;i++)
{
a[0][i]='H';
a[10][i]='H';
}
a[1][1]='X';
a[1][3]='X';
a[1][5]='X';
a[2][7]='X';
a[3][7]='X';
a[8][7]='X';
a[9][1]='X';
a[1][6]='U';
a[2][2]='U';
a[3][5]='U';
a[5][2]='U';
a[6][6]='U';
a[8][4]='U';
a[8][6]='U';
a[3][2]='O';
a[2][3]='H';
a[2][4]='H';
a[4][1]='H';
a[4][2]='H';
a[5][5]='H';
a[5][7]='H';
a[7][2]='H';
a[7][3]='H';
a[7][4]='H';

x1=10;
y1=-425;
}

public void keyPressed(KeyEvent ke)
{
char ch=ke.getKeyChar();
System.out.println("\n\tKey Pressed :"+ch);
int key=ke.getKeyCode();
switch(ch)
{
case 'W':
upos=pos();
if(a[(upos/9)-1][upos%9]!='H')
{
if(a[(upos/9)-1][upos%9]=='U'&&a[(upos/9)-2][upos%9]=='X')
{
a[(upos/9)][upos%9]='a';
a[(upos/9)-1][upos%9]='O';
a[(upos/9)-2][upos%9]='a';
}
else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]=='H'))
break;

else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]=='U'))
break;

else if((a[(upos/9)-1][upos%9]=='U')&&(a[(upos/9)-2][upos%9]!='H'))
{
temp=a[(upos/9)-2][upos%9];
a[(upos/9)-2][upos%9]=a[(upos/9)-1][upos%9];
a[(upos/9)-1][upos%9]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)-1][upos%9];
a[(upos/9)-1][upos%9]=temp;
}
else if(a[(upos/9)-1][upos%9]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)-1][upos%9];
a[(upos/9)-1][upos%9]=temp;
}
}
final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;
case 'S' : upos=pos();
if(a[upos/9+1][upos%9]!='H')
{

if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='X'))
{
a[(upos/9)][upos%9]='a';
a[(upos/9)+1][upos%9]='O';
a[(upos/9)+2][upos%9]='a';
}

else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='H'))
break;

else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]=='U'))
break;
else if((a[upos/9+1][upos%9]=='U')&&(a[upos/9+2][upos%9]!='H'))
{
temp=a[upos/9+2][upos%9];
a[upos/9+2][upos%9]=a[upos/9+1][upos%9];
a[upos/9+1][upos%9]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)+1][upos%9];
a[(upos/9)+1][upos%9]=temp;
}
else if(a[(upos/9)+1][upos%9]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)+1][upos%9];
a[(upos/9)+1][upos%9]=temp;
}
}
final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;
case 'A' : upos=pos();
if(a[upos/9][upos%9-1]!='H')
{
if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='X'))
{
a[(upos/9)][upos%9]='a';
a[(upos/9)][upos%9-1]='O';
a[(upos/9)][upos%9-2]='a';
}

else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='H'))
break;

else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]=='U'))
break;

else if((a[upos/9][upos%9-1]=='U')&&(a[upos/9][upos%9-2]!='H'))
{
temp=a[upos/9][upos%9-1];
a[upos/9][upos%9-1]=a[upos/9][upos%9-2];
a[upos/9][upos%9-2]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9-1];
a[(upos/9)][upos%9-1]=temp;
}
else if(a[(upos/9)][upos%9-1]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9-1];
a[(upos/9)][upos%9-1]=temp;
}
}final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;
case 'D' : upos=pos();
if(a[upos/9][upos%9+1]!='H')
{

if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='X'))
{
a[(upos/9)][upos%9]='a';
a[(upos/9)][upos%9+1]='O';
a[(upos/9)][upos%9+2]='a';
}

else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='H'))
break;

else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]=='U'))
break;
else if((a[upos/9][upos%9+1]=='U')&&(a[upos/9][upos%9+2]!='H'))
{
temp=a[upos/9][upos%9+1];
a[upos/9][upos%9+1]=a[upos/9][upos%9+2];
a[upos/9][upos%9+2]=temp;

temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9+1];
a[(upos/9)][upos%9+1]=temp;
}
else if(a[(upos/9)][upos%9+1]!='X')
{
temp=a[upos/9][upos%9];
a[upos/9][upos%9]=a[(upos/9)][upos%9+1];
a[(upos/9)][upos%9+1]=temp;
}

}final1=finish();
if(final1==0)
System.out.println("Congrats Puzzle solved");
break;

}
repaint();
}

public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void paint(Graphics g)  // For displaying the array
{
x1=10;
y1=0;
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]=='H')
{
g.setColor(Color.red);
g.fillRect(x1,y1,40,40);
g.setColor(Color.black);
g.drawRect(x1+1,y1+1,38,38);
}
if(a[i][j]=='U')
{
g.setColor(Color.orange);
g.fillRect(x1+3,y1+3,34,34);
}
if(a[i][j]=='X')
{
g.setColor(Color.darkGray);
g.fillRect(x1+3,y1+3,34,34);
}
if(a[i][j]=='O')
{
g.setColor(Color.magenta);
g.fillRect(x1+3,y1+3,34,34);
}
if(a[i][j]=='a')
{
g.setColor(Color.green);
g.fillRect(x1,y1,40,40);
g.setColor(Color.black);
g.drawRect(x1+1,y1+1,38,38);
}
x1=x1+40;
}
x1=10;
y1=y1+40;
}
}
public int pos()
{
int pos;
for(i=1;i<11;i++)
{
for(j=1;j<9;j++)
{
if(a[i][j]=='O')
{
pos=9*i+j;
return(pos);
}
}
}
return -1;
}
public int finish()
{
int final2=0;
for(i=0;i<11;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]=='U')
{
final2=1;
return(final2);
}
}
}
return(final2);
}
}