CODE
import javax.swing.*;
public class MainMP {
/**
* @author BDAndaya
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Codebreaker alpha= new Codebreaker();
alpha.setMaxguess(10);
int white=0;
int black=0;
char[] karakter = new char[100];
char[] usercodeguess = new char[4];
do{
userChoice = JOptionPane.showInputDialog(null, "Choose path:");
if(userChoice==0){
alpha.newGameMessage(alpha.showMaxguess());
//reset white and black and lastinput
alpha.setWhiteValue(0);
alpha.setBlackValue(0);
//generate the code
do{
alpha.generateCode(9);
}
while(alpha.showCode()<1000);
String code = (alpha.showCode() + " ");
int m=1;
for(int x=0;x<alpha.showMaxguess();x++){
int y=x+1;
String input=JOptionPane.showInputDialog(null, "Try #" + y + "\n\nClue: WHITE[" + alpha.showWhite() + "]; BLACK[" + alpha.showBlack() + "]" + "\n\nEnter a 4-digit code\n", lastinput);
lastinput = input;
}
//this checks the code entered by the user
for(int r=0;r<input.length();r++){
karakter[x] = input.charAt®;
if(Character.isLetter(karakter[x])){
if(!input.equals("uncle")){
JOptionPane.showMessageDialog(null, "A code should not contain letters");
lastinput="Wrong Input";
break;
}
}
//next line gets only the digits inside the string that was inputted by the user
if(Character.isDigit(karakter[r])){
//filters the entered digits, so that the program will only accept 4 digits
if(p==4){
JOptionPane.showMessageDialog(null, "You've entered more than 4 characters");
lastinput="Wrong Input";
break;
}
usercodeguess[p]=karakter[r];
p++;
//next line, the program checks the acquired codes and adds the increments/decrements the values of white/black according to the given code
for(int z=0;z<4;z++){
if(karakter[r]==code.charAt(x)){
alpha.changeWhiteValue('+',1);
for(int j=0;j<4;j++){
if(z==r){
continue;
}
else{
if(usercodeguess[r]==usercodeguess[j]){
alpha.changeWhiteValue('-',1);
}
}
}
if(r==j){
alpha.changeBlackValue('+',1);
}
}
}
}
}
//output here
if(alpha.showBlack()==4){
alpha.win(alpha.showCode(),y);
break;
}
if(m==alpha.showMaxguess()){
alpha.lose(alpha.showCode());
}
m++;
}
}
}
}while(userChoice!=2);
}
}