2014-01-21 3 views
-1

그래서 나는 뱀과 사다리 게임을 만들고 있습니다. player1과 player2가 동일한 그리드에 착륙한다면 반드시 결투해야합니다. 내 게임에서 두 명의 플레이어는 바위 종이 가위로 게임을해야합니다. 내 메서드 Begin() int int win 반환하는 데 문제가 있습니다. 승리 값은 플레이어 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

당신은 방법 determineOutcome에 의해 반환되는 int 값을 저장해야합니다. 당신은 그것의 실제 값을 사용하지 않기 때문에 필요하지 않습니다

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

참고 determineOutcome()에 매개 변수로 win을 전달합니다. 대신이 시도 할 수 : 여기

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 ("winner :"+ win ")과 같은 유용한 정보가있는'System.out.println' 문을 사용하는 것을 권장합니다. 편리하다고 생각하는 장소. – Christian

+0

여전히 출력이 아프다 내 출력 – user3188122

관련 문제