Hey all! I made a java code to solve stage 3 from gpq (the mastermind one), and I'd like to share it with the common mapler. Does anybody have any idea how to make it into a java applet or something?
I tested it a couple of times with my guild and now everyone thinks I'm a genious because I can solve it within seconds hehe
(so it works). It's also made it a mastermind game while I was at it. (I wasn't allowed to play MS by my parents and got really, really bored).
Here's the code:
I tested it a couple of times with my guild and now everyone thinks I'm a genious because I can solve it within seconds hehe
(so it works). It's also made it a mastermind game while I was at it. (I wasn't allowed to play MS by my parents and got really, really bored).Here's the code:
code
And here is how it looks if you're wondering:Code:
import java.util.*;
import java.lang.Math.*;
public class GPQst3 {
public int[][] tries = new int[4][7];
public int[][] results = new int[3][7];
public int numberOfTries;
public Scanner scanner = new Scanner(System.in);
int mode;
int[] mastermind = new int[4];
public static void main(String[] args) {
GPQst3 xD = new GPQst3();
}
public GPQst3(){
System.out.println("");
System.out.println("Welcome to AshaManBE's gPQ stage 3 solver deluxe!");
System.out.println("");
System.out.println("");
System.out.println("Choose game mode:");
System.out.println("Type 1 for the gPQ stage 3 solver.");
System.out.println("Type 2 for the gPQ stage 3 solver with less tekst.");
System.out.println("Type 3 for mastermind, gPQ style.");
System.out.println("Type 4 for mastermind, gPQ style with less tekst.");
mode = scanner.nextInt();
if (mode %2 == 1 ){
System.out.println("");
System.out.println("To input a combination, use the first letter of each item.");
System.out.println("Cake = C, Medal = M, Potion = P, Scroll = S");
System.out.println("You can also use 1, 2, 3 and 4.");
System.out.println("So for example the combination cake + cake + medal + potion would be CCMP, or 1123");
System.out.println("");
System.out.println("Enjoy!");
System.out.println("");
System.out.println("");
System.out.println("");
}
if (mode <3)
while (true){
reset();
while (numberOfTries != 7){
addTry();
addResults();
computePossibilities();
numberOfTries++;
}
} else
while (true){
reset();
while (numberOfTries != 7){
addTry();
giveHints();
computePossibilities();
numberOfTries++;
}
if (numberOfTries == 7){
System.out.println("Sorry! You're dead! The answer was:");
printComb(mastermind);
System.out.println("");
System.out.println("Let's try again: ");
}
}
}
public void makeComb(){
mastermind[0] = (int) (4*Math.random()+1);
mastermind[1] = (int) (4*Math.random()+1);
mastermind[2] = (int) (4*Math.random()+1);
mastermind[3] = (int) (4*Math.random()+1);
}
public void giveHints(){
int[] res = check(mastermind,numberOfTries);
System.out.println();
if (res[0]==4) {
System.out.println("You win! =D");
System.out.println("Lets go again!");
GPQst3 xD = new GPQst3();
} else {
if (mode %2 == 1 )
System.out.println("There are "+ res[0] + " on the right spot, "
+ res[1] + " are right characters, but on the wrong spot, and "
+ res[2] + " are plain wrong.");
else {
System.out.println("Good: "+ res[0]);
System.out.println("Bad: " + res[1]);
System.out.println("unknown: "+ res[2]);
}
results[0][numberOfTries] = res[0];
results[1][numberOfTries] = res[1];
results[2][numberOfTries] = res[2];
}
}
public void reset(){
numberOfTries = 0;
for (int i=0;i<7;i++){
for (int j=0;j<4;j++){
tries[j][i]=0;
if (j!=3) results[j][i]=0;
}
}
if (mode > 2 ){
makeComb();
}
}
public void addTry(){
System.out.println("Input try " + (numberOfTries+1) + ".");
if (mode %2 == 1 ){
System.out.println("Remember: Cake = C, Medal = M, Potion = P, Scroll = S");
}
if (scanner.hasNextInt()){
int combination = scanner.nextInt();
for (int j=0;j<4;j++){
int item;
item = (int) (combination/Math.pow(10,3-j))%10;
tries[j][numberOfTries]=item;
}
}
else {
String combination = scanner.next();
for (int j=0;j<4;j++){
if (combination.charAt(j) == 'C') tries[j][numberOfTries]=1;
else if (combination.charAt(j) == 'M') tries[j][numberOfTries]=2;
else if (combination.charAt(j) == 'P') tries[j][numberOfTries]=3;
else if (combination.charAt(j) == 'S') tries[j][numberOfTries]=4;
}
}
}
public void addResults(){
if (mode %2 == 1 )
System.out.println("Input number of good items:");
else System.out.println("Good:");
results[0][numberOfTries] = scanner.nextInt();
if (mode %2 == 1 )
System.out.println("Input number of bad items:");
else System.out.println("Bad:");
results[1][numberOfTries] = scanner.nextInt();
if (mode %2 == 1 )
System.out.println("Input number of unknown items:");
else System.out.println("unknown:");
results[2][numberOfTries] = scanner.nextInt();
}
public void computePossibilities(){
int[] possComb = new int[4];
int possibilities=0;
boolean possible;
for(int i=1;i<5;i++){
possComb[0]=i;
for(int j=1;j<5;j++){
possComb[1]=j;
for(int k=1;k<5;k++){
possComb[2]=k;
for(int m=1;m<5;m++){
possComb[3]=m;
possible = check(possComb);
if (possible){
if (mode<3)
printComb(possComb);
possibilities++;
}
}
}
}
}
if (mode<3) {
System.out.println("");
if (mode %2 == 1 ){
System.out.println("Results:");
System.out.println("In total "+ possibilities + " combinations are possible.");
} else
System.out.println("Possibile combinations: "+ possibilities);
} else {
if (mode %2 == 1 ){
System.out.println("With the hints I gave you, there should be "
+ possibilities + " possible combinations left.");
} else System.out.println("Possibile combinations: "+ possibilities);
System.out.println();
}
}
public boolean check(int[] possComb){
boolean result = true;
for (int j=0;j<=numberOfTries;j++){
int[] res = check(possComb,j);
if (res[0] != results[0][j] ||
res[1] != results[1][j] ||
res[2] != results[2][j])
result = false;
}
return result;
}
public int[] check(int[] possComb, int j){
int[] result = new int[3];
int good=0; int unknown=0;
int oneA=0; int oneB=0; int twoA=0; int twoB=0;
int threeA=0; int threeB=0; int fourA=0; int fourB=0;
for (int i=0; i<4; i++){
if (possComb[i]==tries[i][j]) good++;
if (tries[i][j]==1) oneA++;
else if (tries[i][j]==2) twoA++;
else if (tries[i][j]==3) threeA++;
else if (tries[i][j]==4) fourA++;
if (possComb[i]==1) oneB++;
else if (possComb[i]==2) twoB++;
else if (possComb[i]==3) threeB++;
else if (possComb[i]==4) fourB++;
}
if (oneA - oneB > 0) unknown = unknown + oneA - oneB;
if (twoA - twoB > 0) unknown = unknown + twoA - twoB;
if (threeA - threeB > 0) unknown = unknown + threeA - threeB;
if (fourA - fourB > 0) unknown = unknown + fourA - fourB;
result[0]=good;
result[1]=4-unknown-good;
result[2]=unknown;
return result;
}
public void printComb(int[] possComb){
String one; String two; String three; String four;
if (possComb[0]==1) one = "C";
else if (possComb[0]==2) one = "M";
else if (possComb[0]==3) one = "P";
else if (possComb[0]==4) one = "S";
else one = "E";
if (possComb[1]==1) two = "C";
else if (possComb[1]==2) two = "M";
else if (possComb[1]==3) two = "P";
else if (possComb[1]==4) two = "S";
else two = "E";
if (possComb[2]==1) three = "C";
else if (possComb[2]==2) three = "M";
else if (possComb[2]==3) three = "P";
else if (possComb[2]==4) three = "S";
else three = "E";
if (possComb[3]==1) four = "C";
else if (possComb[3]==2) four = "M";
else if (possComb[3]==3) four = "P";
else if (possComb[3]==4) four = "S";
else four = "E";
System.out.println(one + two + three + four);
}
}
pics
solver
![[Image: gpqst31.jpg]](http://img4.imageshack.us/img4/4243/gpqst31.jpg)
![[Image: gpqst32.jpg]](http://img14.imageshack.us/img14/992/gpqst32.jpg)
with less text:
![[Image: gpqst33.jpg]](http://img197.imageshack.us/img197/5721/gpqst33.jpg)
![[Image: gpqst31.jpg]](http://img4.imageshack.us/img4/4243/gpqst31.jpg)
![[Image: gpqst32.jpg]](http://img14.imageshack.us/img14/992/gpqst32.jpg)
with less text:
![[Image: gpqst33.jpg]](http://img197.imageshack.us/img197/5721/gpqst33.jpg)
mastermind game
![[Image: gpqst34.jpg]](http://img15.imageshack.us/img15/7062/gpqst34.jpg)
Less text:
![[Image: gpqst35.jpg]](http://img188.imageshack.us/img188/751/gpqst35.jpg)
I admit using a second screen with the solver while solving these... :zzz:
I know all the text is annoing but that's why I added option 2 and 4.![[Image: gpqst34.jpg]](http://img15.imageshack.us/img15/7062/gpqst34.jpg)
Less text:
![[Image: gpqst35.jpg]](http://img188.imageshack.us/img188/751/gpqst35.jpg)
I admit using a second screen with the solver while solving these... :zzz:

