/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication19;
/**
*
* @author gaurav
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class JavaApplication19 extends Applet implements ActionListener,Runnable{
int counter=0;int s=0;int a=0,b=0;int l=0;
Button [][]square;
int [][]visited;
int [][]visit;
Button start,end,reset,set;
Thread t[];
public void init()
{setBackground(Color.PINK);
t=new Thread[5];
for(int k=0;k<5;k++){t[k]=new Thread(this);}
setLayout(null);
visited=new int[10][10];
visit=new int[10][10];
for(int i=0;i<10;i++){for(int j=0;j<10;j++)
{visit[i][j]=1;visited[i][j]=0;}
}
reset=new Button("RESET");
add(reset);
reset.reshape(350,570,100,30);
reset.setBackground(Color.blue);
reset.addActionListener(this); set=new Button("SET");
add(set);
set.reshape(350,20,100,30);
set.setBackground(Color.red);
set.addActionListener(this);
start=new Button("START");
add(start);
start.setBackground(Color.green);
start.reshape(100,50,50,50);
start.addActionListener(this);
end=new Button("END");
add(end);
end.reshape(650,500,50,50);
end.setBackground(Color.red);
end.addActionListener(this);
square=new Button[10][10];
for(int i=0;i<10;i++)
{for(int j=0;j<10;j++)
{square[i][j]=new Button();
square[i][j].setBackground(Color.white);
add(square[i][j]);
square[i][j].reshape(150+(i*50),50+(j*50),50,50);square[i][j].addActionListener(this);
}
}
} public void run()
{try{square[0][0].setBackground(Color.yellow);int i=0;int j=0;
visit[0][0]=0;visited[0][0]=1;Thread.sleep(300);do{if(i==0){ if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
Thread.sleep(300);i++;}else if(visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
Thread.sleep(300);j++;}else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
Thread.sleep(300);j--;}}
else if(i==9){ if(j<9&&visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
Thread.sleep(300);j++;}
else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
Thread.sleep(300);j--;}
else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
Thread.sleep(300);i--;}}
else if(j==0){if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
Thread.sleep(300);i++;}
else if(j<9&&visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
Thread.sleep(300);j++;}
else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
Thread.sleep(300);i--;}
}
else if(j==9){if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
Thread.sleep(300);i++;}
else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
Thread.sleep(300);j--;}
else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
Thread.sleep(300);i--;}
}
else if(visited[i+1][j]==0){square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i+1][j]=0;
Thread.sleep(300);i++;}
else if(j<9&&visited[i][j+1]==0){square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j+1]=0;
Thread.sleep(300);j++;}
else if(visited[i][j-1]==0&&j>0){square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j-1]=0;
Thread.sleep(300);j--;}
else if(i>0&&visited[i-1][j]==0){square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i-1][j]=0;
Thread.sleep(300);i--;}
//back tracking
/* else if(visited[0][j+1]==1&&visited[0][j-1]==1&&visited[1][j]==1){
System.out.print("ram");
if(visit[0][j+1]==0)
{ square[0][j+1].setBackground(Color.yellow);square[0][j].setBackground(Color.white);visited[0][j+1]=1;visit[0][j]=0;
Thread.sleep(300);j++;}
else if(visit[0][j-1]==0&&j>0)
{ square[0][j-1].setBackground(Color.yellow);square[0][j-1].setBackground(Color.white);visited[0][j-1]=1;visit[0][j]=0;
Thread.sleep(300);j--;}
else if(visit[1][j]==0)
{ square[1][j].setBackground(Color.yellow);square[0][j].setBackground(Color.white);visited[1][j]=1;visit[0][j]=0;
Thread.sleep(300);i++;}
}
else if(visited[9][j+1]==1&&visited[9][j-1]==1&&visited[8][j]==1){
System.out.print("ram");
if(visit[9][j+1]==0)
{ square[9][j+1].setBackground(Color.yellow);square[9][j].setBackground(Color.white);visited[9][j+1]=1;visit[9][j]=0;
Thread.sleep(300);j++;}
else if(visit[9][j-1]==0&&j>0)
{ square[9][j-1].setBackground(Color.yellow);square[9][j-1].setBackground(Color.white);visited[9][j-1]=1;visit[9][j]=0;
Thread.sleep(300);j--;}
else if(visit[i-1][j]==0)
{ square[9-1][j].setBackground(Color.yellow);square[9-1][j].setBackground(Color.white);visited[9-1][j]=1;visit[9][j]=0;
Thread.sleep(300);i--;}
}*/
else if(visited[i+1][j]==1&&visited[i][j+1]==1&&visited[i][j-1]==1&&visited[i-1][j]==1&&i<9&&i>0&&j>0&&j<9){if(i<9&&visit[i+1][j]==0){
square[i+1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i+1][j]=1;visit[i][j]=1;
Thread.sleep(300);i++;}
else if(visit[i][j+1]==0&&j<9){
square[i][j+1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j+1]=1;visit[i][j]=1;
Thread.sleep(300);j++;}
else if(visit[i][j-1]==0&&j>0){
square[i][j-1].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i][j-1]=1;visit[i][j]=1;
Thread.sleep(300);j--;}
else if(visit[i-1][j]==0&&i>0){
square[i-1][j].setBackground(Color.yellow);square[i][j].setBackground(Color.white);visited[i-1][j]=1;visit[i][j]=1;
Thread.sleep(300);i--;}}
if(i==9&&j==9){JOptionPane.showMessageDialog(null, "GAME IS OVER TO PLAY AGAIN PRESS RESET BUTTON");break;}
}while(i<=10&&j<=10);}
catch(InterruptedException e){System.out.println("gaurav");}
}
public void actionPerformed(ActionEvent ae)
{if(ae.getSource()==set)
{
s=0;l=0;
JOptionPane.showMessageDialog(null, "DESIGN THE TRACK");
}
else if(ae.getSource()==reset){s=0;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++){
square[i][j].setBackground(Color.white);visit[i][j]=1;visited[i][j]=0;
}
}
l++;start.setBackground(Color.green);end.setBackground(Color.red);
} else if(ae.getSource()==end){
end.setBackground(Color.yellow);JOptionPane.showMessageDialog(null, "GAME IS OVER TO PLAY AGAIN PRESS RESET BUTTON");t[l].stop();t[l].notify();}
else if(ae.getSource()==start){
start.setBackground(Color.yellow);
s=1;t[l].start();
return;}
else if(s<1){
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++){
if(ae.getSource()==square[i][j])
{if(square[i][j].getBackground()==Color.white) {square[i][j].setBackground(Color.black);visited[i][j]=1;visit[i][j]=1;}
else
{ square[i][j].setBackground(Color.white);visited[i][j]=0;visit[i][j]=1;
}
}}
}}
/* else if(s==1){
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++){
if(ae.getSource()==square[i][j])
{if(square[i][j].getBackground()==Color.white)
square[i][j].setBackground(Color.yellow);
else
square[i][j].setBackground(Color.white);
}
}}}*/
}
public void paint(Graphics g)
{g.drawRect(100,50,600,600);
}
}
Tuesday, 17 September 2013
Maze Game
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment