2015-09-11 2 views
0

У меня есть игра свинья в беге java, однако, кажется, что один счет игрока продолжает проходить мимо 100, хотя у меня есть несколько вещей на месте, чтобы убедиться, что этого не произойдет. игрок 2 не будет более 100, но игрок 1 будет. Что я здесь делаю неправильно?Java-игра с оценкой свиньи не считается?

import java.util.Random; 
import java.util.Scanner; 

public class PigDice { 
    static int dice1 = 0; 
    static int dice2 = 0; 
    final int maxScore = 100; 
    static int player1Score = 0; 
    static int player2Score = 0; 
    static int player1Turn = 0; 
    static int player2Turn = 0; 
    boolean game = true; 
    static String input = ""; 

    public static void main(String[] args) { 
     Random random = new Random(); 
     Scanner sc = new Scanner(System.in); 
     do { 
      while (player1Score < 100 || player2Score < 100) { 
       do { 

        System.out.println("PLAYER 1's TURN"); 
        System.out.println("Current score: " + player1Score); 
        System.out.println("Number of turns taken: " + player1Turn); 
        dice1 = random.nextInt(6) + 1; 
        System.out.println("You rolled a: " + dice1); 
        if (dice1 == 1) { 
         System.out.println("You lose a turn and no points rewarded"); 
         System.out.println("Score: " + player1Score); 
         System.out.println(""); 
         player1Turn++; 
         break; 
        } else { 
         System.out.println("Would you like to roll again, or bank your points?"); 
         System.out.println("Enter 'r' to roll again, 'b' to bank."); 
         String input = sc.nextLine(); 
         if (input.equalsIgnoreCase("b")) { 
          player1Score += dice1; 
          player1Turn++; 
          System.out.println("Current player 1 score: " + player1Score); 
          System.out.println(""); 
          break; 
         } else if (input.equalsIgnoreCase("r") && dice1 != 1 && player1Score < 100) { 
          player1Score += dice1; 
          player1Turn++; 
          System.out.println("Current player 1 score: " + player1Score); 
          System.out.println(""); 

         } 
        } 

       } while (input.equalsIgnoreCase("r") || dice1 != 1); 
       dice1 = 0; 
       if (player1Score >= 100) { 
        System.out.println("Player one wins"); 
        break; 
       } 
       break; 

      } 
      do { 
       System.out.println("PLAYER 2's TURN"); 
       System.out.println("Number of turns taken: " + player2Turn); 
       System.out.println("Current score: " + player2Score); 
       dice2 = random.nextInt(6) + 1; 
       System.out.println("You rolled a: " + dice2); 
       if (dice2 == 1) { 
        System.out.println("You lose a turn and no points rewarded"); 
        System.out.println("Score: " + player2Score); 
        player2Turn++; 
        System.out.println(""); 
        break; 
       } else { 
        System.out.println("Would you like to roll again, or bank your points?"); 
        System.out.println("Enter 'r' to roll again, 'b' to bank."); 
        String input = sc.nextLine(); 
        if (input.equalsIgnoreCase("b")) { 
         player2Score = dice2 + player2Score; 
         player2Turn++; 
         System.out.println("Current player 2 score: " + player2Score); 
         System.out.println(""); 
         break; 
        } else if (input.equalsIgnoreCase("r") && dice2 != 1 && player2Score < 100) { 
         player2Score += dice2; 
         player2Turn++; 
         System.out.println("Current player 2 score: " + player2Score); 
         System.out.println(""); 

        } 
       } 

      } while (input.equalsIgnoreCase("r") || dice2 != 1); 
      dice2 = 0; 
      if (player2Score >= 100) { 
       System.out.println("Player 2 wins"); 
       break; 
      } 

     } while (player1Score < 100 || player2Score < 100); 
    } 
} 
+0

Я могу ошибаться, но я думаю, что 'player1S ядро <100 || player2Score <100' должен быть 'player1Score <100 && player2Score <100' – MadProgrammer

+0

Нет, это не так –

+0

Вы должны использовать отладчик. Вероятно, вы также должны извлечь какой-то метод из этой стены кода. –

ответ

1

Вы добавленный ненужный while петли (один после первого do цикла), что делает break не ломается игрок 1 не ломается из основного цикла

Вот код, с петлей, снятого

public class PigDice { 

    static int dice1 = 0; 
    static int dice2 = 0; 
    static int player1Score = 0; 
    static int player2Score = 0; 
    static int player1Turn = 0; 
    static int player2Turn = 0; 
    static String input = ""; 
    final int maxScore = 100; 
    boolean game = true; 

    public static void main(String[] args) { 
    Random random = new Random(); 
    Scanner sc = new Scanner(System.in); 
    do { 
     do { 

      System.out.println("PLAYER 1's TURN"); 
      System.out.println("Current score: " + player1Score); 
      System.out.println("Number of turns taken: " + player1Turn); 
      dice1 = random.nextInt(6) + 1; 
      System.out.println("You rolled a: " + dice1); 
      if (dice1 == 1) { 
      System.out.println("You lose a turn and no points rewarded"); 
      System.out.println("Score: " + player1Score); 
      System.out.println(""); 
      player1Turn++; 
      break; 
      } else { 
      System.out.println("Would you like to roll again, or bank your points?"); 
      System.out.println("Enter 'r' to roll again, 'b' to bank."); 
      String input = sc.nextLine(); 
      if (input.equalsIgnoreCase("b")) { 
       player1Score += dice1; 
       player1Turn++; 
       System.out.println("Current player 1 score: " + player1Score); 
       System.out.println(""); 
       break; 
      } else if (input.equalsIgnoreCase("r") && dice1 != 1 
         && player1Score < 100) { 
       player1Score += dice1; 
       player1Turn++; 
       System.out.println("Current player 1 score: " + player1Score); 
       System.out.println(""); 

      } 
      } 

     } while (input.equalsIgnoreCase("r") || dice1 != 1); 
     dice1 = 0; 
     if (player1Score >= 100) { 
      System.out.println("Player one wins"); 
      break; 
     } 


     do { 
     System.out.println("PLAYER 2's TURN"); 
     System.out.println("Number of turns taken: " + player2Turn); 
     System.out.println("Current score: " + player2Score); 
     dice2 = random.nextInt(6) + 1; 
     System.out.println("You rolled a: " + dice2); 
     if (dice2 == 1) { 
      System.out.println("You lose a turn and no points rewarded"); 
      System.out.println("Score: " + player2Score); 
      player2Turn++; 
      System.out.println(""); 
      break; 
     } else { 
      System.out.println("Would you like to roll again, or bank your points?"); 
      System.out.println("Enter 'r' to roll again, 'b' to bank."); 
      String input = sc.nextLine(); 
      if (input.equalsIgnoreCase("b")) { 
      player2Score = dice2 + player2Score; 
      player2Turn++; 
      System.out.println("Current player 2 score: " + player2Score); 
      System.out.println(""); 
      break; 
      } else if (input.equalsIgnoreCase("r") && dice2 != 1 && player2Score < 100) { 
      player2Score += dice2; 
      player2Turn++; 
      System.out.println("Current player 2 score: " + player2Score); 
      System.out.println(""); 

      } 
     } 

     } while (input.equalsIgnoreCase("r") || dice2 != 1); 
     dice2 = 0; 
     if (player2Score >= 100) { 
     System.out.println("Player 2 wins"); 
     break; 
     } 

    } while (player1Score < 100 || player2Score < 100); 
    } 
} 
0

Слишком много бессмысленны петли & разрыв.

Попробуйте реорганизовать код с чем-то вроде этого:

import java.util.Random; 
import java.util.Scanner; 

/** 
* 
* @author Ankh Zet ([email protected]) 
*/ 
public class Pig { 

    public static void main(String[] args) { 
     (new Pig()).game(); 
    } 

    class Player { 

     int turnScore = 0; 
     int totalScore = 0; 

     String name; 

     public Player(String name) { 
      this.name = name; 
     } 

    } 

    Scanner keyboard = new Scanner(System.in); 
    Random diceRoll = new Random(); 

    public void game() { 

     System.out.println("Welcome to the game of Pig!\n"); 

     Player player1 = new Player("Player 1"); 
     Player player2 = new Player("Player 2"); 

     Player next = player1; 
     while (true) { 
      int score = turn(next); 

      if (score >= 100) { 
       System.out.println(next.name.toUpperCase() + " WINS!"); 
       break; 
      } 

      if (next == player1) 
       next = player2; 
      else 
       next = player1; 
     } 
    } 

    enum Action { 
     Roll, Hold; 
    } 

    int turn(Player player) { 
     System.out.println(); 
     System.out.println("It is " + player.name + "'s turn."); 

     player.turnScore = 0; 

     int dice; 
     while ((dice = roll()) != 1) { 
      System.out.println("You rolled a " + dice); 

      player.turnScore += dice; 

      System.out.println("Your turn score is " + player.turnScore); 
      System.out.println("And your total score is " + player.totalScore); 
      System.out.println("If you hold, " + player.turnScore + " points will be added to your total score."); 

      if (takeInput() != Action.Roll) 
       break; 
     } 

     if (dice == 1) { 
      player.turnScore = 0; 
      System.out.println("You rolled a 1"); 
      System.out.println("You lose your turn!"); 
     } 

     player.totalScore += player.turnScore; 
     System.out.println("Your total score is " + player.totalScore); 

     return player.totalScore; 
    } 

    int roll() { 
     return diceRoll.nextInt(6) + 1; 
    } 

    Action takeInput() { 
     String input; 
     do { 
      System.out.println("Enter 'r' to roll again, or 'h' to hold."); 
      input = keyboard.nextLine(); 
     } while (input.isEmpty()); 

     switch (input.charAt(0)) { 
     case 'h': 
      return Action.Hold; 

     default: 
      return Action.Roll; 
     } 
    } 

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