2014-12-04 62 views
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"); 
} 
+0

GWT用于创建Web应用程序,您的代码用于控制台应用程序 - 您如何期望它在浏览器中工作? – 2014-12-04 08:52:27

+0

可能从一个GWT教程开始。 http://www.gwtproject.org/doc/latest/tutorial/gettingstarted.html – geert3 2014-12-04 12:41:23

回答

0

你可以将原始文本(甚至不是HTML)放入具有固定空间等的元素(类似于“pre”元素)来替换System.out.println。

我建议你切换到像“进入”,“Y”/“N”...和DoubleBox为他们想要下注量的按钮。

在浏览器中可能会有一个更一般的“控制台”,但它会有点奇怪吗?

也许最好从一个GWT样本开始,并慢慢转换到你想要的?很多引导来启动和运行GWT应用程序。