import javax.swing.JOptionPane;
import java.util.Random;
public class FermiGame
{
public static void main(String [] args)
{
JOptionPane.showMessageDialog(null,"Hello!!"+"\nWelcome to Fermi ^o^!");
JOptionPane.showMessageDialog(null,"There is only one simple rule: Guess 3 numbers in between 0-9"
+"\nand in the correct position to match with the three Secret Numbers"
+"\nYour aim is to try to get three Fermi."
+"\n If you get Nano,that's means your guess does not match any of the secret numbers."
+"\n Pico means your guess is correct,but not in the correct position.");
JOptionPane.showMessageDialog(null,"Are you ready to play?");
valid();
}
public static void valid()
{
String input;
char ch;
input=JOptionPane.showInputDialog("Enter any alphabert to start play or 'z' to quit:");
ch=input.charAt(0);
while(!Character.isLetter(ch))
{
input=JOptionPane.showInputDialog("!!You are only allow to enter alphabert!!"+
"\nPlease re-enter any alphabert to start play or 'z' to quit:");
ch=input.charAt(0);
}
if(ch=='z')
{
quit();
}
else
start();
}
public static void start()
{
char ch;
String input;
final int Num_Guess=3;
int [] guess=new int [Num_Guess];
final int Num_Random=3;
int [] random=new int [Num_Random];
Random ran=new Random();
for(int j=0; j<random.length; j++)
{
random[j]=ran.nextInt(10);
}
for(int i=0; i<guess.length; i++)
{
input=JOptionPane.showInputDialog("Enter your guess"+(i+1)+"(0-9):");
ch=input.charAt(0);
while(!Character.isDigit(ch))
{
input=JOptionPane.showInputDialog("!!This is not an integer!!"+
"\nPlease re-enter your guess"+(i+1)+"(0-9):");
ch=input.charAt(0);
}
guess[i]=Integer.parseInt(input);
while(guess[i]<0 || guess[i]>9)
{
input=JOptionPane.showInputDialog("!!Invalid number!!"+
"\nYou can only enter integer in between 0-9."+
"\nPlease re-enter your guess "+(i+1)+"(0-9):");
ch=input.charAt(0);
while(!Character.isDigit(ch))
{
input=JOptionPane.showInputDialog("!!This is not an integer!!"+
"\nPlease re-enter your guess"+(i+1)+"(0-9):");
ch=input.charAt(0);
}
guess[i]=Integer.parseInt(input);
}
}
fermi give=new fermi(guess,random);
result(guess,random);
}
public static void result(int []guess,int []random)
{
fermi value=new fermi();
for(int k=0;k<3;k++)
{
if(guess[k]==random[0])
{
JOptionPane.showMessageDialog(null,guess[k]+"=FERMI (^0^)");
}
else if(guess[k]==random[1] || guess[k]==random[2])
{
JOptionPane.showMessageDialog(null,guess[k]+"=PICO (^-^)");
}
else
JOptionPane.showMessageDialog(null,guess[k]+"=NANO (x_x)");
}
if(guess[0]==random[0] && guess[1]==random[1] && guess[2]==random[2])
value.totalWin();
else
value.totalLose();
value.totalPlay();
JOptionPane.showMessageDialog(null,value);
valid();
}
public static void quit()
{
JOptionPane.showMessageDialog(null,"Come and play again!"+
"\nBye Bye!");
}
} |