SameGame
Java Source:
//SameGame Source code
//on Java Applet
//Develop By Anop Boonthaveepath
//Bangkok Thailand 22 Aug 2000
//g4219393@au.ac.th , anopb@hotmail.com
//If you have any suggestion or bug report plase mail to me..... at g4219393@au.ac.th or anopb@hotmail.com
//So if you to find parter to develop program in any Developer as Java, C, C++, VB, Perl, ASP, etc
//Please mail to me,
//By the way thank you to interesting with my program..
// you must have 10 images from A and Hightlighted A to E and Highlighted E
// and you should have 2 files on AU format that about Sound when you clicked
//for this game, Rule is when you click on the connected block
//Then program will highlight connected block
//So if you click again on the highlighted area then block will disappear
//Try to get high in Score...
//Dont forget to comment about this program to me...................:)
import java.awt.*;
import java.applet.*;
public class SameGame extends Applet
{
Font f = new Font("TimesRoman",Font.BOLD,30);
Image imageA,imageB,imageC,imageD,
imageAPress,imageBPress,imageCPress,imageDPress,imageE,imageEPress,imageNewGame;
char position[][] =new char[21][10];
int score,ClickScore;
int temp,i,j,clickx,clicky,count,blinkflag,firstempty,displaycount;
int firstzeroX,lastABCDX,successX,havezeroX ,countzeroX;
int firstzeroY,lastABCDY,successY,havezeroY ,countzeroY;
double randomnumber;
String tempchar;
AudioClip ClickSound,ReduceSound;
public void init()
{
score=0;
for( int i9=0;i9<=9;i9++)
{
position[20][i9]='0';
}
firstempty=0;
tempchar="xxx";
displaycount=0;
ClickScore=0;
count=0;
setBackground(Color.white);
imageA = getImage(getCodeBase(),"imagea.gif");
imageB = getImage(getCodeBase(),"imageb.gif");
imageC = getImage(getCodeBase(),"imagec.gif");
imageD = getImage(getCodeBase(),"imaged.gif");
imageE = getImage(getCodeBase(),"imagee.gif");
imageAPress = getImage(getCodeBase(),"imageapress.gif");
imageBPress = getImage(getCodeBase(),"imagebpress.gif");
imageCPress = getImage(getCodeBase(),"imagecpress.gif");
imageDPress = getImage(getCodeBase(),"imagedpress.gif");
imageEPress = getImage(getCodeBase(),"imageepress.gif");
imageNewGame = getImage(getCodeBase(),"newgame.gif");
ClickSound = getAudioClip(getCodeBase(),"click.au");
ReduceSound = getAudioClip(getCodeBase(),"reduce.au");
for(i=0;i<=9;i++)
{
for(j=0;j<=19;j++)
{
//temp=Randomizer.getInt(1,4);
randomnumber=Math.random();
if(randomnumber>=0.00 && randomnumber<0.20)
temp=1;
if(randomnumber>=0.20 && randomnumber<0.40)
temp=2;
if(randomnumber>=0.40 && randomnumber<0.60)
temp=3;
if(randomnumber>=0.60 && randomnumber<0.80)
temp=4;
if(randomnumber>=0.80 && randomnumber<=1.00)
temp=5;
switch (temp)
{
case 1:
position[j][i]='A';
break;
case 2:
position[j][i]='B';
break;
case 3:
position[j][i]='C';
break;
case 4:
position[j][i]='D';
break;
case 5:
position[j][i]='E';
break;
}
}
}
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
g.setColor(Color.white);
for(i=0;i<=9;i++)
{
for(j=0;j<=19;j++)
{
switch (position[j][i])
{
case 'A':
g.drawImage(imageA,((j*30)+1),((i*30)+1),30,30,this);
break;
case 'B':
g.drawImage(imageB,((j*30)+1),((i*30)+1),30,30,this);
break;
case 'C':
g.drawImage(imageC,((j*30)+1),((i*30)+1),30,30,this);
break;
case 'D':
g.drawImage(imageD,((j*30)+1),((i*30)+1),30,30,this);
break;
case 'E':
g.drawImage(imageE,((j*30)+1),((i*30)+1),30,30,this);
break;
case '1':
g.drawImage(imageAPress,((j*30)+1),((i*30)+1),30,30,this);
break;
case '2':
g.drawImage(imageBPress,((j*30)+1),((i*30)+1),30,30,this);
break;
case '3':
g.drawImage(imageCPress,((j*30)+1),((i*30)+1),30,30,this);
break;
case '4':
g.drawImage(imageDPress,((j*30)+1),((i*30)+1),30,30,this);
break;
case '5':
g.drawImage(imageEPress,((j*30)+1),((i*30)+1),30,30,this);
break;
case '0':
g.fillRect((j*30)+1,(i*30)+1,30,30);
break;
}
}
}
g.setColor(Color.black);
g.fillRect(0,305,600,50);
g.drawImage(imageNewGame,5,310,100,40,this);
g.setColor(Color.white);
g.drawString("SCORE",450,330);
g.drawString(String.valueOf(score),500,330);
if(count==0)
displaycount=0;
else
displaycount=count+1;
g.drawString("THIS TOTAL",250,320);
g.drawString(String.valueOf(displaycount),400,320);
g.drawString("THIS CLICK SCORE",250,340);
g.drawString(String.valueOf(ClickScore),400,340);
if(endgame()== false)
{
g.setFont(f);
g.setColor(Color.black);
g.drawString("GAME OVER",200,200);
}
}
public boolean mouseDown(Event evt, int x, int y)
{
if(x>=5 && x<=105 && y>=310 && y<=350)
{
init();
repaint();
return true;
}
clickx=x/30;
clicky=y/30;
if((position[clickx][clicky]>='A' && position[clickx][clicky]<='E')||(position[clickx][clicky]=='0') )
{
if(blinkflag==1)
{
for(i=0;i<=9;i++)
{
for(j=0;j<=19;j++)
{
switch(position[j][i])
{
case '1':
{
position[j][i]='A';
break;
}
case '2':
{
position[j][i]='B';
break;
}
case '3':
{
position[j][i]='C';
break;
}
case '4':
{
position[j][i]='D';
break;
}
case '5':
{
position[j][i]='E';
break;
}
}
blinkflag=0;
}
}
}
if(position[clickx][clicky]!='0')
{
count=0;
findconnect(clickx,clicky,position[clickx][clicky]);
if(count>0)
{
ClickSound.play();
ClickScore = CalculateScore(count+1);
}
}
}
else
{
if(blinkflag==1)
{
ReduceSound.play();
rearrange();
score=score+ClickScore;
blinkflag=0;
}
}
repaint();
return true;
}
public void rearrange()
{
for(int i=0;i<=19;i++)
{
for(int j=0;j<=9;j++)
{
if(position[i][j]>='1' && position[i][j]<='5')
{
position[i][j]='0';
}
}
}
//For height eliminate
for(int i9=0;i9<=19;i9++)
{ havezeroX=0;
do
{
for(int j9=9;j9>=0;j9--)
{
if(position[i9][j9]=='0')
{
if(j9!=0)
{
position[i9][j9]=position[i9][j9-1];
position[i9][j9-1]='0';
}
else
{
position[i9][j9]='0';
}
//repaint();
}
}
//Find first Zero(bottom up)
for(int j8=9;j8>=0;j8--)
{
if(position[i9][j8]=='0')
{
firstzeroX=j8;
havezeroX=1;
break;
}
}
//Find last ABCD(bottom up)
for(int j7=9;j7>=0;j7--)
{
if(position[i9][j7]>='A' && position[i9][j7]<='E')
lastABCDX=j7;
}
countzeroX=0;
for(int j6=9;j6>=0;j6--)
{
if(position[i9][j6]=='0')
countzeroX++;
}
if(firstzeroX==(lastABCDX-1)||havezeroX==0||countzeroX==10)
successX=1; // dont have zero exit wihile loop
else
successX=0; //have zero go to do it again
}while(successX==0);
}
//For shift left
do
{
havezeroY=0;
for(int i5=0;i5<=19;i5++)
{
if(position[i5][9]=='0')
{
for(int i4=i5;i4<=19;i4++)
{
for(int j5=0;j5<=9;j5++)
{
position[i4][j5]=position[i4+1][j5];
position[i4+1][j5]='0';
}
}
}
}
//find the first zero(left to right)
countzeroY=0;
for(int i3=0;i3<=19;i3++)
{
if(position[i3][9]=='0')
{
firstzeroY=i3;
havezeroY=1;
break;
}
}
//find the last ABCD(left to right)
for(int i6=0;i6<=19;i6++)
{
if(position[i6][9]>='A' && position[i6][9]<='E')
{
lastABCDY=i6;
}
}
//Find that how many zero
//(if zero=20 that mean all off item eleminted already,finish game at all)
countzeroY=0;
for(int i6=9;i6>=0;i6--)
{
if(position[i6][9]=='0')
countzeroY++;
}
if(firstzeroY==(lastABCDY+1)||havezeroY==0||countzeroY==20)
successY=1; // dont have zero exit wihile loop
else
successY=0; //have zero go to do it again
}while(successY==0);
}
public int CalculateScore(int thiscount)
{
int Calculated ;
switch(thiscount)
{
case 0:
Calculated = 0;
break;
case 1:
Calculated = 0;
break;
case 2:
Calculated = 2;
break;
case 3:
Calculated = 4;
break;
case 4:
Calculated = 6;
break;
case 5:
Calculated = 10;
break;
case 6:
Calculated = 16;
break;
case 7:
Calculated = 26;
break;
case 8:
Calculated = 42;
break;
case 9:
Calculated = 68;
break;
case 10:
Calculated = 110;
break;
case 11:
Calculated = 178;
break;
case 12:
Calculated = 288;
break;
default :
Calculated = CalculateScore(thiscount-1)+CalculateScore(thiscount-2);
break;
}
return Calculated;
}
public void findconnect(int cx, int cy,char clickvalue)
{
switch(clickvalue)
{
case('A'):
{
position[cx][cy]='1';
break;
}
case('B'):
{
position[cx][cy]='2';
break;
}
case('C'):
{
position[cx][cy]='3';
break;
}
case('D'):
{
position[cx][cy]='4';
break;
}
case('E'):
{
position[cx][cy]='5';
break;
}
}
if(cx<19 && cy>=0 && cy<=9)
{
if(position[cx+1][cy]==clickvalue)
{
count++;
findconnect(cx+1,cy,clickvalue);
}
}
if( cx>0 && cy>=0 && cy<=9)
{
if(position[cx-1][cy]==clickvalue)
{
count++;
findconnect(cx-1,cy,clickvalue);
}
}
if(cx>=0 && cx<=19 && cy<9)
{
if(position[cx][cy+1]==clickvalue)
{
count++;
findconnect(cx,cy+1,clickvalue);
}
}
if(cx>=0 && cx<=19 && cy>0)
{
if(position[cx][cy-1]==clickvalue)
{
count++;
findconnect(cx,cy-1,clickvalue);
}
}
if(count==0)
{
position[cx][cy]=clickvalue;
}
else
{
blinkflag=1;
//repaint();
}
}
public boolean endgame()
{
for(int i=0;i<=19;i++)
{
for(int j=0;j<=9;j++)
{
if(position[i][j]!='0')
{
if(j!=9)
if(position[i][j+1]==position[i][j])
return true;
if(i!=19)
if(position[i+1][j]==position[i][j])
return true;
}
}
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////
}
Back to SameGame
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|