2014-01-21 3 views
-1

Итак, я делаю игру с змеями и лестницами. Если игрок1 и игрок2 приземляются в одной сетке, они должны иметь дуэль. В моей игре у двух игроков должна быть игра с ножницами из бумажной бумаги. У меня возникли проблемы с получением моего метода, чтобы вернуть значение int win в мою подпрограмму Begin(). Значение выигрыша скажет метод begin, если игрок 1 или 2 выиграл битву с бумажными ножницами.Как правильно вывести код игры

Мой код:

public static String Begin // Recieves data from the main method 
    { 
// start Begin method 
    int win=0; 
    if(P1Position==P2Position||P2Position==P1Position){ 
      System.out.println("==========================================================================");      
     System.out.println (player1+" is currently on square " + P1Position); 
     System.out.println (player2+" is currently on square " + P2Position); 
     System.out.println("==========================================================================");  

     firstplay(choice1,choice2); 
     determineOutcome(choice1,choice2,win); 
     if(win==1){ 
      determineOutcome(choice1,choice2,win); 
      P2Position=P1Position-P1Roll; 
     } 
     else{ 
      determineOutcome(choice1,choice2,win); 
      P1Position=P2Position-P2Roll; 
     } 
     } 
} 
    public static void firstplay(int choice1,int choice2)throws IOException{//USER 
      // Initializes the BufferReader for user input 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      System.out.println("DUEL!!!\nThe Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot"); 
      System.out.println("1=Rock\n2=Paper\n3=Scissors\n==========="); 
      System.out.println(player1+" choose:"); 
      choice1=Integer.parseInt(br.readLine()); 
      System.out.println(player2+" choose:"); 
      choice2=Integer.parseInt(br.readLine()); 
      if(choice1==1){ 
       System.out.println(player1+" chose Rock");//If user picked ROck 
      } 
      else if(choice1==2){ 
       System.out.println(player1+" chose Paper");//If user picked Paper 
      } 
      else if(choice1==3){ 
       System.out.println(player1+" chose Scissors");//If user picked Scissors 
      } 

      if(choice2==1){ 
       System.out.println(player2+" chose Rock"); 
      } 
      else if(choice2==2){ 
       System.out.println(player2+" chose Paper"); 
      } 
      else if(choice2==3){ 
       System.out.println(player2+" chose Scissors"); 
      } 
     } 
     public static int determineOutcome(int choice1,int choice2,int win)throws IOException{ 
      // Initializes the BufferReader for user input 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      int die=0; 


    //Rock vs Papaer 

      if(choice2==1&&choice1==2){ 
       System.out.println(player1+" WON PAPER BEATS ROCK"); 
       System.out.println(player1+" keeps their spot"); 
       win=1; 

      } 
      else if(choice2==2&&choice1==1){ 
       System.out.println(player2+" WON PAPAER BEATS ROCK "); 
       System.out.println(player1+" keeps their spot"); 
       win=0; 

      } 

      //Scissors vs Paper 
      else if(choice2==2&&choice1==3){ 
       System.out.println(player1+" WON SCISSORS BEAT PAPER "); 
       System.out.println(player1+" keeps their spot"); 
       win=1; 
      } 
      else if(choice2==3&&choice1==2){ 
       System.out.println(player2+" WON SCISSORS BEAT PAPER "); 
       System.out.println(player2+" keeps their spot"); 
       win=0; 
      } 
      //Rock vs Scissors 
      else if(choice2==3&&choice1==1){ 
       System.out.println(player1+" WON ROCK BEATS SCISSORS "); 
       System.out.println(player1+" keeps their spot"); 
       win=1; 
      } 
      else if(choice2==1&&choice1==3){ 
       System.out.println(player2+" WON ROCK BEATS SCISSORS "); 
       System.out.println(player2+" keeps their spot"); 
       win=0; 
      } 
      //Ties 
      else if(choice2==1&&choice1==1){ 
       System.out.println("YOU'VE TIED. Play once again"); 

      } 
      else if(choice2==2&&choice1==2){ 
       System.out.println("YOU'VE TIED. Play once again"); 

      } 
      else if(choice2==3&&choice1==3){ 
       System.out.println("YOU'VE TIED. Play once again"); 

      } 
     return win; 


     } 

    }//end class 

Выход:

yo Rolled a 2 
po Rolled a 2 
------------------------------------------------------------------------ 
========================================================================== 
yo is currently on square 2 
po is currently on square 2 
========================================================================== 
DUEL!!! 
The Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot 
1=Rock 
2=Paper 
3=Scissors 
=========== 
yo choose: 
1 
po choose: 
3 
yo chose Rock 
po chose Scissors 
========================================================================== 
yo is currently on square 0 
po is currently on square 2 
========================================================================== 
yo press r to roll 

Ожидаемое: Выход:

yo Rolled a 2 
po Rolled a 2 
------------------------------------------------------------------------ 
========================================================================== 
yo is currently on square 2 
po is currently on square 2 
========================================================================== 
DUEL!!! 
The Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot 
1=Rock 
2=Paper 
3=Scissors 
=========== 
yo choose: 
1 
po choose: 
3 
yo chose Rock 
po chose Scissors 
yo won rock beats scissors. 
yo keeps their spot 
========================================================================== 
yo is currently on square 2 
po is currently on square 0 
========================================================================== 
yo press r to roll 

ответ

1

Вы должны сохранить значение int возвращенное методом determineOutcome.

win = determineOutcome(choice1,choice2,win); 
if(win==1){ 
    ... 
} 

Обратите внимание, что нет необходимости передавать win в качестве параметра determineOutcome(), так как вы не используете его фактическое значение. Вы можете попробовать это вместо:

public static int determineOutcome(int choice1, int choice2) throws IOException 
{ 
    // Initializes the BufferReader for user input 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    int die = 0; 
    int win = 2; // 2 == TIE 
    //Rock vs Papaer 

    if (choice2 == 1 && choice1 == 2) { 
     System.out.println(player1 + " WON PAPER BEATS ROCK"); 
     System.out.println(player1 + " keeps their spot"); 
     win = 1; 

    } else if (choice2 == 2 && choice1 == 1) { 
     System.out.println(player2 + " WON PAPAER BEATS ROCK "); 
     System.out.println(player1 + " keeps their spot"); 
     win = 0; 

    } 

    //Scissors vs Paper 
    else if (choice2 == 2 && choice1 == 3) { 
     System.out.println(player1 + " WON SCISSORS BEAT PAPER "); 
     System.out.println(player1 + " keeps their spot"); 
     win = 1; 
    } else if (choice2 == 3 && choice1 == 2) { 
     System.out.println(player2 + " WON SCISSORS BEAT PAPER "); 
     System.out.println(player2 + " keeps their spot"); 
     win = 0; 
    } 
    //Rock vs Scissors 
    else if (choice2 == 3 && choice1 == 1) { 
     System.out.println(player1 + " WON ROCK BEATS SCISSORS "); 
     System.out.println(player1 + " keeps their spot"); 
     win = 1; 
    } else if (choice2 == 1 && choice1 == 3) { 
     System.out.println(player2 + " WON ROCK BEATS SCISSORS "); 
     System.out.println(player2 + " keeps their spot"); 
     win = 0; 
    } 
    //Ties 
    else if (choice2 == 1 && choice1 == 1) { 
     System.out.println("YOU'VE TIED. Play once again"); 

    } else if (choice2 == 2 && choice1 == 2) { 
     System.out.println("YOU'VE TIED. Play once again"); 

    } else if (choice2 == 3 && choice1 == 3) { 
     System.out.println("YOU'VE TIED. Play once again"); 

    } 
    return win; 

} 

Здесь вы объявляете локальных переменныеwin, установите его (0, 1 or 2) в соответствующем условных и вернуть его в конце.

Примечание: Если условными, чтобы определить, если игрок выиграл охватывает все случаи, вы можете заменить «случаи связи» с else:

//Rock vs Scissors 
else if (choice2 == 3 && choice1 == 1) { 
    System.out.println(player1 + " WON ROCK BEATS SCISSORS "); 
    System.out.println(player1 + " keeps their spot"); 
    win = 1; 
} else if (choice2 == 1 && choice1 == 3) { 
    System.out.println(player2 + " WON ROCK BEATS SCISSORS "); 
    System.out.println(player2 + " keeps their spot"); 
    win = 0; 
} 
//Ties 
else { 
    System.out.println("YOU'VE TIED. Play once again"); 
} 
+0

К сожалению он не работает. – user3188122

+0

Можете ли вы уточнить? Вы получаете ошибки? Результат неожиданен? Я бы посоветовал вам научиться отлаживать ваши программы, использовать отладчик или просто использовать инструкции 'System.out.println' с полезной информацией, например' System.out.println («победитель:« + выигрыш ») в места, которые вы считаете удобными. – Christian

+0

Все еще не работает плохой пост моего вывода – user3188122

Смежные вопросы