how am i going to repeat my whole game program? i dont know what to do. i have the codes below. and this program is already running, i have compiled it and it has no syntax and logical error, this is mastermind game. my only problem is how am i going to repeat this whole game once the player pressed the YEs button after he/she is asked if he/she wants to Play AGain.
any help will be of utmost appreciation. thanks.
import java.awt.*;
import java.lang.Math;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;
public class mastermind extends Applet implements ActionListener
{
int tri=0,put=1,set=1,t,in=0,w,n,ctr=0,c=0,x,win=0,lose=0,pasaloadcp=0,pasaloadset=0;
int [] secretCol;
int tapos[]={-1,-1,-1};
int pickCol[];
int copyy[];
Label peglbl1=new Label("");
Label peglbl2=new Label("");
Label peglbl3=new Label("");
Label peglbl4=new Label("");
Label peglbl5=new Label("");
Label peglbl6=new Label("");
Label peglbl7=new Label("");
Label peglbl8=new Label("");
Label peglbl9=new Label("");
Label peglbl10=new Label("");
Label peglbl11=new Label("");
Label peglbl12=new Label("");
Label peglbl13=new Label("");
Label peglbl14=new Label("");
Label peglbl15=new Label("");
Button colbtn1=new Button();
Button colbtn2=new Button();
Button colbtn3=new Button();
Button colbtn4=new Button();
Button colbtn5=new Button();
Button colbtn6=new Button();
Label gnolbl1=new Label("Guess #1");
Label gnolbl2=new Label("Guess #2");
Label gnolbl3=new Label("Guess #3");
Label gnolbl4=new Label("Guess #4");
Label gnolbl5=new Label("Guess #5");
Label winlbl=new Label("WIN");
Label loselbl=new Label("LOSE");
Label clulbl1=new Label("");
Label clulbl2=new Label("");
Label clulbl3=new Label("");
Label clulbl4=new Label("");
Label clulbl5=new Label("");
Label clulbl6=new Label("");
Label clulbl7=new Label("");
Label clulbl8=new Label("");
Label clulbl9=new Label("");
Label clulbl10=new Label("");
Label clulbl12=new Label("");
Label blbl2=new Label("");
Label blbl4=new Label("");
//Label slbl2=new Label("");
//Label slbl3=new Label("");
//Label slbl4=new Label("");
Label sclbl1=new Label("0");
Label sclbl2=new Label("0");
public void init()
{
pickCol=new int[3];
tapos=new int[3];
copyy=new int[3];
setLayout(new GridLayout(8,6,5,5));
colbtn1.addActionListener(this);
colbtn2.addActionListener(this);
colbtn3.addActionListener(this);
colbtn4.addActionListener(this);
colbtn5.addActionListener(this);
colbtn6.addActionListener(this);
add(gnolbl1);
add(peglbl1);
add(peglbl2);
add(peglbl3);
add(clulbl1);
add(clulbl2);
add(gnolbl2);
add(peglbl4);
add(peglbl5);
add(peglbl6);
add(clulbl3);
add(clulbl4);
add(gnolbl3);
add(peglbl7);
add(peglbl8);
add(peglbl9);
add(clulbl5);
add(clulbl6);
add(gnolbl4);
add(peglbl10);
add(peglbl11);
add(peglbl12);
add(clulbl7);
add(clulbl8);
add(gnolbl5);
add(peglbl13);
add(peglbl14);
add(peglbl15);
add(clulbl9);
add(clulbl10);
add(colbtn1);
add(colbtn2);
add(colbtn3);
add(colbtn4);
add(colbtn5);
add(colbtn6);
peglbl1.setBackground(Color.DARK_GRAY);
peglbl2.setBackground(Color.DARK_GRAY);
peglbl3.setBackground(Color.DARK_GRAY);
peglbl4.setBackground(Color.DARK_GRAY);
peglbl5.setBackground(Color.DARK_GRAY);
peglbl6.setBackground(Color.DARK_GRAY);
peglbl7.setBackground(Color.DARK_GRAY);
peglbl8.setBackground(Color.DARK_GRAY);
peglbl9.setBackground(Color.DARK_GRAY);
peglbl10.setBackground(Color.DARK_GRAY);
peglbl11.setBackground(Color.DARK_GRAY);
peglbl12.setBackground(Color.DARK_GRAY);
peglbl13.setBackground(Color.DARK_GRAY);
peglbl14.setBackground(Color.DARK_GRAY);
peglbl15.setBackground(Color.DARK_GRAY);
gnolbl1.setBackground(Color.LIGHT_GRAY);
gnolbl2.setBackground(Color.LIGHT_GRAY);
gnolbl3.setBackground(Color.LIGHT_GRAY);
gnolbl4.setBackground(Color.LIGHT_GRAY);
gnolbl5.setBackground(Color.LIGHT_GRAY);
clulbl1.setBackground(Color.LIGHT_GRAY);
clulbl2.setBackground(Color.LIGHT_GRAY);
clulbl3.setBackground(Color.LIGHT_GRAY);
clulbl4.setBackground(Color.LIGHT_GRAY);
clulbl5.setBackground(Color.LIGHT_GRAY);
clulbl6.setBackground(Color.LIGHT_GRAY);
clulbl7.setBackground(Color.LIGHT_GRAY);
clulbl8.setBackground(Color.LIGHT_GRAY);
clulbl9.setBackground(Color.LIGHT_GRAY);
clulbl10.setBackground(Color.LIGHT_GRAY);
colbtn1.setLabel("Green");
colbtn2.setLabel("Blue");
colbtn3.setLabel("Red");
colbtn4.setLabel("Yellow");
colbtn5.setLabel("Orange");
colbtn6.setLabel("Magenta");
colbtn1.setBackground(Color.GREEN);
colbtn2.setBackground(Color.BLUE);
colbtn3.setBackground(Color.RED);
colbtn4.setBackground(Color.YELLOW);
colbtn5.setBackground(Color.ORANGE);
colbtn6.setBackground(Color.MAGENTA);
add(winlbl);
add(sclbl1);
add(blbl2);
add(loselbl);
add(sclbl2);
add(blbl4);
//add(slbl2);
//add(slbl3);
//add(slbl4);
}//init
public void start()
{
double m;
int k,a;
secretCol=new int[3];
for(a=0;a<3;a++)
{
secretCol[a]=-1;
while(secretCol[a]<0)
{
m=Math.random();
k=(int) Math.floor((m*6)+1);
secretCol[a]=k;
}
}
for(a=0;a<3;a++)
{
if(a==0)
{
if(secretCol[a]==1)
slbl2.setBackground(Color.GREEN);
else if(secretCol[a]==2)
slbl2.setBackground(Color.BLUE);
else if(secretCol[a]==3)
slbl2.setBackground(Color.RED);
else if(secretCol[a]==4)
slbl2.setBackground(Color.YELLOW);
else if(secretCol[a]==5)
slbl2.setBackground(Color.ORANGE);
else if(secretCol[a]==6)
slbl2.setBackground(Color.MAGENTA);
}
else if(a==1)
{
if(secretCol[a]==1)
slbl3.setBackground(Color.GREEN);
else if(secretCol[a]==2)
slbl3.setBackground(Color.BLUE);
else if(secretCol[a]==3)
slbl3.setBackground(Color.RED);
else if(secretCol[a]==4)
slbl3.setBackground(Color.YELLOW);
else if(secretCol[a]==5)
slbl3.setBackground(Color.ORANGE);
else if(secretCol[a]==6)
slbl3.setBackground(Color.MAGENTA);
}
else if(a==2)
{
if(secretCol[a]==1)
slbl4.setBackground(Color.GREEN);
else if(secretCol[a]==2)
slbl4.setBackground(Color.BLUE);
else if(secretCol[a]==3)
slbl4.setBackground(Color.RED);
else if(secretCol[a]==4)
slbl4.setBackground(Color.YELLOW);
else if(secretCol[a]==5)
slbl4.setBackground(Color.ORANGE);
else if(secretCol[a]==6)
slbl4.setBackground(Color.MAGENTA);
}
}
}//clrBoard
public void actionPerformed(ActionEvent ae)
{
int rightcp=0,rightcol=0;
int what=0;
tri++;
if(ae.getSource()==colbtn1)
{
if(tri==1)
peglbl1.setBackground(Color.GREEN);
else if(tri==2)
peglbl2.setBackground(Color.GREEN);
else if(tri==3)
peglbl3.setBackground(Color.GREEN);
else if(tri==4)
peglbl4.setBackground(Color.GREEN);
else if(tri==5)
peglbl5.setBackground(Color.GREEN);
else if(tri==6)
peglbl6.setBackground(Color.GREEN);
else if(tri==7)
peglbl7.setBackground(Color.GREEN);
else if(tri==8)
peglbl8.setBackground(Color.GREEN);
else if(tri==9)
peglbl9.setBackground(Color.GREEN);
else if(tri==10)
peglbl10.setBackground(Color.GREEN);
else if(tri==11)
peglbl11.setBackground(Color.GREEN);
else if(tri==12)
peglbl12.setBackground(Color.GREEN);
else if(tri==13)
peglbl13.setBackground(Color.GREEN);
else if(tri==14)
peglbl14.setBackground(Color.GREEN);
else if(tri==15)
peglbl15.setBackground(Color.GREEN);
what=1;
}
else if(ae.getSource()==colbtn2)
{
if(tri==1)
peglbl1.setBackground(Color.BLUE);
else if(tri==2)
peglbl2.setBackground(Color.BLUE);
else if(tri==3)
peglbl3.setBackground(Color.BLUE);
else if(tri==4)
peglbl4.setBackground(Color.BLUE);
else if(tri==5)
peglbl5.setBackground(Color.BLUE);
else if(tri==6)
peglbl6.setBackground(Color.BLUE);
else if(tri==7)
peglbl7.setBackground(Color.BLUE);
else if(tri==8)
peglbl8.setBackground(Color.BLUE);
else if(tri==9)
peglbl9.setBackground(Color.BLUE);
else if(tri==10)
peglbl10.setBackground(Color.BLUE);
else if(tri==11)
peglbl11.setBackground(Color.BLUE);
else if(tri==12)
peglbl12.setBackground(Color.BLUE);
else if(tri==13)
peglbl13.setBackground(Color.BLUE);
else if(tri==14)
peglbl14.setBackground(Color.BLUE);
else if(tri==15)
peglbl15.setBackground(Color.BLUE);
what=2;
}
else if(ae.getSource()==colbtn3)
{
if(tri==1)
peglbl1.setBackground(Color.RED);
else if(tri==2)
peglbl2.setBackground(Color.RED);
else if(tri==3)
peglbl3.setBackground(Color.RED);
else if(tri==4)
peglbl4.setBackground(Color.RED);
else if(tri==5)
peglbl5.setBackground(Color.RED);
else if(tri==6)
peglbl6.setBackground(Color.RED);
else if(tri==7)
peglbl7.setBackground(Color.RED);
else if(tri==8)
peglbl8.setBackground(Color.RED);
else if(tri==9)
peglbl9.setBackground(Color.RED);
else if(tri==10)
peglbl10.setBackground(Color.RED);
else if(tri==11)
peglbl11.setBackground(Color.RED);
else if(tri==12)
peglbl12.setBackground(Color.RED);
else if(tri==13)
peglbl13.setBackground(Color.RED);
else if(tri==14)
peglbl14.setBackground(Color.RED);
else if(tri==15)
peglbl15.setBackground(Color.RED);
what=3;
}
else if(ae.getSource()==colbtn4)
{
if(tri==1)
peglbl1.setBackground(Color.YELLOW);
else if(tri==2)
peglbl2.setBackground(Color.YELLOW);
else if(tri==3)
peglbl3.setBackground(Color.YELLOW);
else if(tri==4)
peglbl4.setBackground(Color.YELLOW);
else if(tri==5)
peglbl5.setBackground(Color.YELLOW);
else if(tri==6)
peglbl6.setBackground(Color.YELLOW);
else if(tri==7)
peglbl7.setBackground(Color.YELLOW);
else if(tri==8)
peglbl8.setBackground(Color.YELLOW);
else if(tri==9)
peglbl9.setBackground(Color.YELLOW);
else if(tri==10)
peglbl10.setBackground(Color.YELLOW);
else if(tri==11)
peglbl11.setBackground(Color.YELLOW);
else if(tri==12)
peglbl12.setBackground(Color.YELLOW);
else if(tri==13)
peglbl13.setBackground(Color.YELLOW);
else if(tri==14)
peglbl14.setBackground(Color.YELLOW);
else if (tri==15)
peglbl15.setBackground(Color.YELLOW);
what=4;
}
else if(ae.getSource()==colbtn5)
{
if(tri==1)
peglbl1.setBackground(Color.ORANGE);
else if(tri==2)
peglbl2.setBackground(Color.ORANGE);
else if(tri==3)
peglbl3.setBackground(Color.ORANGE);
else if(tri==4)
peglbl4.setBackground(Color.ORANGE);
else if(tri==5)
peglbl5.setBackground(Color.ORANGE);
else if(tri==6)
peglbl6.setBackground(Color.ORANGE);
else if(tri==7)
peglbl7.setBackground(Color.ORANGE);
else if(tri==8)
peglbl8.setBackground(Color.ORANGE);
else if(tri==9)
peglbl9.setBackground(Color.ORANGE);
else if(tri==10)
peglbl10.setBackground(Color.ORANGE);
else if(tri==11)
peglbl11.setBackground(Color.ORANGE);
else if(tri==12)
peglbl12.setBackground(Color.ORANGE);
else if(tri==13)
peglbl13.setBackground(Color.ORANGE);
else if(tri==14)
peglbl14.setBackground(Color.ORANGE);
else if(tri==15)
peglbl15.setBackground(Color.ORANGE);
what=5;
}
else if(ae.getSource()==colbtn6)
{
if(tri==1)
peglbl1.setBackground(Color.MAGENTA);
else if(tri==2)
peglbl2.setBackground(Color.MAGENTA);
else if(tri==3)
peglbl3.setBackground(Color.MAGENTA);
else if(tri==4)
peglbl4.setBackground(Color.MAGENTA);
else if(tri==5)
peglbl5.setBackground(Color.MAGENTA);
else if(tri==6)
peglbl6.setBackground(Color.MAGENTA);
else if(tri==7)
peglbl7.setBackground(Color.MAGENTA);
else if(tri==8)
peglbl8.setBackground(Color.MAGENTA);
else if(tri==9)
peglbl9.setBackground(Color.MAGENTA);
else if(tri==10)
peglbl10.setBackground(Color.MAGENTA);
else if(tri==11)
peglbl11.setBackground(Color.MAGENTA);
else if(tri==12)
peglbl12.setBackground(Color.MAGENTA);
else if(tri==13)
peglbl13.setBackground(Color.MAGENTA);
else if(tri==14)
peglbl14.setBackground(Color.MAGENTA);
else if(tri==15)
peglbl15.setBackground(Color.MAGENTA);
what=6;
}
ctr=ctr+1;
pickCol[in++]=what;
if(ctr==3)
{
for(w=0;w<3;w++)
copyy[w]=secretCol[w];
for(w=0;w<3;w++)
{
if(pickCol[w]==secretCol[w])
{
rightcp=rightcp+1;
tapos[w]=pickCol[w];
}
}
for(w=0;w<3;w++)
for(x=0;x<3;x++)
if(pickCol[w]==tapos[w])
break;
else if(w==x)
continue;
else if(pickCol[w]==tapos[x])
continue;
else if(pickCol[w]==copyy[x])
{
rightcol=rightcol+1;
copyy[x]=-1;
break;
}
if(set==1)
{
if(rightcp==0)
clulbl1.setText("0");
else if(rightcp==1)
clulbl1.setText("1");
else if(rightcp==2)
clulbl1.setText("2");
else if(rightcp==3)
clulbl1.setText("3");
if(rightcol==0)
clulbl2.setText("0");
else if(rightcol==1)
clulbl2.setText("1");
else if(rightcol==2)
clulbl2.setText("2");
else if(rightcol==3)
clulbl2.setText("3");
pasaloadset=set;
if(rightcp==3 && set<=5)
{
win++;
sclbl1.setText(win+"");
JOptionPane.showMessageDialog(null,"Congratulations you WIN!!!","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
else if ((rightcp<3) && (set==5))
{
set=0;
lose++;
sclbl2.setText(lose+"");
JOptionPane.showMessageDialog(null,"Sorry you LOSE.","Resulta",JOptionPane.INFORMATION_MESSAGE);
}
pasaloadcp=rightcp;
rightcp=0;
rightcol=0;
in=0;
ctr=0;
tapos[0]=-1;
tapos[1]=-1;
tapos[2]=-1;
}
else if(set==2)
{
if(rightcp==0)
clulbl3.setText("0");
else if(rightcp==1)
clulbl3.setText("1");
else if(rightcp==2)
clulbl3.setText("2");
else if(rightcp==3)
clulbl3.setText("3");
if(rightcol==0)
clulbl4.setText("0");
else if(rightcol==1)
clulbl4.setText("1");
else if(rightcol==2)
clulbl4.setText("2");
else if(rightcol==3)
clulbl4.setText("3");
pasaloadset=set;
if(rightcp==3 && set<=5)
{
set=0;
win++;
sclbl1.setText(win+"");
JOptionPane.showMessageDialog(null,"Congratulations you WIN!!!","Resulta",JOptionPane.INFORMATION_MESSAGE);
}
else if ((rightcp<3) && (set==5))
{
set=0;
lose++;
sclbl2.setText(lose+"");
JOptionPane.showMessageDialog(null,"Sorry you LOSE.","Resulta",JOptionPane.INFORMATION_MESSAGE);
}
pasaloadcp=rightcp;
rightcp=0;
rightcol=0;
in=0;
ctr=0;
tapos[0]=-1;
tapos[1]=-1;
tapos[2]=-1;
}
else if(set==3)
{
if(rightcp==0)
clulbl5.setText("0");
else if(rightcp==1)
clulbl5.setText("1");
else if(rightcp==2)
clulbl5.setText("2");
else if(rightcp==3)
clulbl5.setText("3");
if(rightcol==0)
clulbl6.setText("0");
else if(rightcol==1)
clulbl6.setText("1");
else if(rightcol==2)
clulbl6.setText("2");
else if(rightcol==3)
clulbl6.setText("3");
pasaloadset=set;
if(rightcp==3 && set<=5)
{
win++;
sclbl1.setText(win+"");
JOptionPane.showMessageDialog(null,"Congratulations you WIN!!!","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
else if ((rightcp<3) && (set==5))
{
lose++;
sclbl2.setText(lose+"");
JOptionPane.showMessageDialog(null,"Sorry you LOSE.","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
pasaloadcp=rightcp;
rightcp=0;
rightcol=0;
in=0;
ctr=0;
tapos[0]=-1;
tapos[1]=-1;
tapos[2]=-1;
}
else if(set==4)
{
if(rightcp==0)
clulbl7.setText("0");
else if(rightcp==1)
clulbl7.setText("1");
else if(rightcp==2)
clulbl7.setText("2");
else if(rightcp==3)
clulbl7.setText("3");
if(rightcol==0)
clulbl8.setText("0");
else if(rightcol==1)
clulbl8.setText("1");
else if(rightcol==2)
clulbl8.setText("2");
else if(rightcol==3)
clulbl8.setText("3");
pasaloadset=set;
if(rightcp==3 && set<=5)
{
win++;
sclbl1.setText(win+"");
JOptionPane.showMessageDialog(null,"Congratulations you WIN!!!","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
else if ((rightcp<3) && (set==5))
{
lose++;
sclbl2.setText(lose+"");
JOptionPane.showMessageDialog(null,"Sorry you LOSE.","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
pasaloadcp=rightcp;
rightcp=0;
rightcol=0;
in=0;
ctr=0;
tapos[0]=-1;
tapos[1]=-1;
tapos[2]=-1;
}
else if(set==5)
{
if(rightcp==0)
clulbl9.setText("0");
else if(rightcp==1)
clulbl9.setText("1");
else if(rightcp==2)
clulbl9.setText("2");
else if(rightcp==3)
clulbl9.setText("3");
if(rightcol==0)
clulbl10.setText("0");
else if(rightcol==1)
clulbl10.setText("1");
else if(rightcol==2)
clulbl10.setText("2");
else if(rightcol==3)
clulbl10.setText("3");
pasaloadset=set;
if(rightcp==3 && set<=5)
{
win++;
sclbl1.setText(win+"");
JOptionPane.showMessageDialog(null,"Congratulations you WIN!!!","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
else if ((rightcp<3) && (set==5))
{
lose++;
sclbl2.setText(lose+"");
JOptionPane.showMessageDialog(null,"Sorry you LOSE.","Resulta",JOptionPane.INFORMATION_MESSAGE);
set=0;
}
pasaloadcp=rightcp;
rightcp=0;
rightcol=0;
in=0;
ctr=0;
tapos[0]=-1;
tapos[1]=-1;
tapos[2]=-1;
}
if(pasaloadcp==3 && pasaloadset<=5)
{
/*******************************/
int intAns =JOptionPane.showConfirmDialog
(null, "Play Again?","Confirmation", JOptionPane.YES_NO_OPTION);
if (intAns == JOptionPane.YES_OPTION)
{
;
}
else
{
JOptionPane.showMessageDialog(null,"THANK YOU","",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
/***************************/
}
else if(pasaloadcp<3 && pasaloadset==5)
{
/*******************************/
int intAns =JOptionPane.showConfirmDialog
(null, "Play Again?","Confirmation", JOptionPane.YES_NO_OPTION);
if (intAns == JOptionPane.YES_OPTION)
;
else
{
JOptionPane.showMessageDialog(null,"Thank You!!!","",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
/***************************/
}
set=set+1;
}//if
}//class
}//class