1
我創建了一個新的Google Web應用程序項目(使用eclipse),並注意到代碼在.server包中運行。我有一個我在java中編寫的老虎機遊戲,並試圖將其實現到GWT中,但語法看起來完全不同。 (例如,我注意到\ n不工作)需要在GWT中運行Java代碼
這裏是我想要實現的代碼 - 我該怎麼做?
// clear console
static void clearConsole()
{
for (int i=0; i<25; i++)
{
System.out.println("\n");
}
}
// actual slot machine algorithm
static void slots()
{
Scanner input = new Scanner (System.in);
char newGame = ' ';
int chips = 100;
int bet = 0;
do{
System.out.println("Try your luck on the SLOT MACHINE." +
"\nLine up (=^^=) or))<>((symbols to win big!\n\n" +
"You currently have " + chips + " chips.\nHow much do you want to bet? ");
//check for accidental char input
try{
bet = input.nextInt();
}
catch(Exception e){
input.nextLine();
System.out.println("NOT A VALID NUMBER\nHow much do you want to bet? ");
bet = input.nextInt();
}
if (bet<=chips && bet>0){
// to add some realism, slot machine will not execute until 'enter' pressed
// then console cleared
input.nextLine();
System.out.println("\nPress 'enter' to start the slot machine.");
input.nextLine();
clearConsole();
String[] machine = {"(=^^=)", "(=^^=)", "))<>((", " XXXX ", " XXXX ", " :^{) ",
" :^{) ", " (>_<)", " (>_<)", " [-O-]", " [-O-]"};
Random rand = new Random();
int slot1 = rand.nextInt(11);
int slot2 = rand.nextInt(11);
int slot3 = rand.nextInt(11);
int slot4 = rand.nextInt(11);
int slot5 = rand.nextInt(11);
int slot6 = rand.nextInt(11);
int slot7 = rand.nextInt(11);
int slot8 = rand.nextInt(11);
int slot9 = rand.nextInt(11);
System.out.println("-----------------------");
System.out.printf("%-7s %-7s %-7s %n%n", machine[slot1], machine[slot2], machine[slot3]);
System.out.printf("%-7s %-7s %-7s %n%n", machine[slot4], machine[slot5], machine[slot6]);
System.out.printf("%-7s %-7s %-7s %n", machine[slot7], machine[slot8], machine[slot9]);
System.out.println("-----------------------\n\n\n\n");
// 3 wild cards
if (slot4 == 2 && slot5 == 2 && slot6 == 2){
bet = bet*100;
chips = chips + bet;
System.out.println("JACKPOT! ");
}
//3 cats (inclusive of wild card)
else if ( slot4 <3 && slot5 <3 && slot6 <3){
bet = bet*5;
chips = chips + bet;
System.out.println("YOU WIN! ");
}
// 3 of any other symbol (inclusive of wild card)
else if(((slot4==2 || slot4==3 || slot4==4) && (slot5==2 || slot5==3 ||
slot5==4) && (slot6==2 || slot6==3 || slot6==4))
|| ((slot4==2 || slot4==5 || slot4==6) && (slot5==2 || slot5==5 ||
slot5==6) && (slot6==2 || slot6==5 || slot6==6))
|| ((slot4==2 || slot4==7 || slot4==8) && (slot5==2 || slot5==7 ||
slot5==8) && (slot6==2 || slot6==7 || slot6==8))
|| ((slot4==2 || slot4==9 || slot4==10) && (slot5==2 || slot5==9 ||
slot5==10) && (slot6==2 || slot6==9 || slot6==10))){
bet = bet*3;
chips = chips + bet;
System.out.println("YOU WIN! ");
}
// 2 cats
else if (slot4<2 && slot5<2 || slot4<2 && slot6<2 || slot5<2 && slot6<2){
bet = bet*2;
chips = chips + bet;
System.out.println("YOU WIN! ");
}
// 1 cat
else if (slot4 < 2 || slot5 < 2 || slot6 < 2){
chips = chips + bet;
System.out.println("YOU WIN! ");
}
// nothing
else{
chips = chips - bet;
System.out.print("You Lose... ");
}
// display current amount of chips
System.out.println("You have " + chips + " chips.");
}else{
System.out.println("You do not have enough chips to make that bet!");
}
if (chips > 0){
System.out.println("\nDo you wanna play this game again? (y/n): ");
newGame = input.next().charAt(0);
clearConsole();
}
} while (newGame =='y' && chips > 0);
input.close();
System.out.println("\nGame Over\n\n");
}
GWT用於創建Web應用程序,您的代碼用於控制檯應用程序 - 您如何期望它在瀏覽器中工作? – 2014-12-04 08:52:27
可能從一個GWT教程開始。 http://www.gwtproject.org/doc/latest/tutorial/gettingstarted.html – geert3 2014-12-04 12:41:23