+- Southperry.net (https://www.southperry.net)
+-- Forum: Maplers Helping Maplers (https://www.southperry.net/forumdisplay.php?fid=9)
+--- Forum: Technical Help (https://www.southperry.net/forumdisplay.php?fid=84)
+--- Thread: how do you make a java applet? (/showthread.php?tid=12242)
how do you make a java applet? - Veneni - 2009-06-13
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:
code
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 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;
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);
}
}
And here is how it looks if you're wondering:
pics
solver
with less text:
mastermind game
Less text:
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.
how do you make a java applet? - Kortestanov - 2009-06-13
i still hate java, though. why not just code it in php?
how do you make a java applet? - Spaz - 2009-06-13
Links in ^ post if you really want to make it an applet, but the easiest way would probably be a form and javascript.
how do you make a java applet? - Veneni - 2009-06-14
Whoops ya I didn't really search for applets They seem a lot of work. Are there any other options?
The javascript form looks promising, I'll look a bit into that.
And I don't know php only java
how do you make a java applet? - KajitiSouls - 2009-06-14
I didn't look at your code extensively, but from the image you've provided of the GPQ solver, I'd have to say your methods could use refining. All it does is prevent you from trying combos that are known to be wrong, and it's not guaranteed to prevent failure. I'd suggest you go back to the drawing board and have the program make suggestions on what combos you should try next. You can do this by having the computer pre-compute probabilities and stuff for chance of success. It's sorta what people programming chess AIs do; they assign "points" to certain spots, and spots with the highest points are where the AIs are most likely to move.
In terms of actual GPQing, there's the "finish in 5 moves or less" method which it is recommended a computer should do, my method which I developed (that thread is hella old lmao, and I see that you've visited it), and the "try four of each item" method. Assuming perfect executiong of each method, the last one has a small chance of failure but probably gets you the fastest times out of all three, while the first one is infuriatingly hard to do with just people.
how do you make a java applet? - Kortestanov - 2009-06-14
Veneni Wrote:Whoops ya I didn't really search for applets They seem a lot of work. Are there any other options?
The javascript form looks promising, I'll look a bit into that.
And I don't know php only java
Well javascript is kind of very similar to java syntax-wise... the functions you can use are sort of limited and i dont think you can import libraries, but the main difference is that you code it into the HTML. for example:
<script language="JavaScript" type="text/javascript">
function solve()
{
//get input from the text boxes using getElementById, output it either using alert() or through another text box.
}
</script>
<input type="submit" value="Solve" onclick="solve()">
also @kajiti, i have no idea how the stage works (in fact, i never did a GPQ), but considering the little options you have you can always just brute force it if nothing else succeeds.
how do you make a java applet? - Stereo - 2009-06-14
KajitiSouls Wrote:You can do this by having the computer pre-compute probabilities and stuff for chance of success. It's sorta what people programming chess AIs do; they assign "points" to certain spots, and spots with the highest points are where the AIs are most likely to move.
I guess by chance of success, you mean chance to solve with least number of moves.
I think the GPQ possibilities is small enough that it would be faster to just compute everything ahead of time, then just run through a sequence where you click the result (1 r 1 w, 2 r 1 w, etc) and it shows you what combination to do next to solve in minimal moves.
public class MagicDamage extends Applet implements ActionListener {
public MagicDamage() {
}
public static void main(String[] args) {
MagicDamage tango = new MagicDamage();
tango.init();
}
swing and applet are pretty much necessary for the graphical part, awt is so it has a "click to calculate" button (or you can base it on modified fields I guess)
Code:
public void init() {
// Construct
blank = new JLabel(" ");
Container contentTop1 = new Container();
contentTop1.setLayout(new GridLayout(6,2));
Then there's the initialization which just builds a layout using swing components in a container.