Beispiel #1
0
 private int fetchScore(net.heinke.cbingutter.game.player.Player p) {
   boolean success = false;
   java.lang.String input = null;
   while (!success) {
     System.out.print(p.getPlayerName() + " Enter score: ");
     java.io.BufferedReader br =
         new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
     try {
       input = br.readLine().trim();
       if (Validator.validateScore(input, p)) {
         success = true;
       }
     } catch (java.io.IOException e) {
       continue;
     }
   }
   if (input.equalsIgnoreCase("X")) {
     return Constants.MAX_PINS_PER_FRAME;
   } else {
     if (input.equals("/")) {
       net.heinke.cbingutter.game.frame.Frame f = p.getCurrentFrame();
       return f.pinsRemaining();
     }
   }
   return Integer.parseInt(input);
 }
Beispiel #2
0
 public static void printResultEffects(Frame f, Player p) {
   if (f.isStrike()) {
     int framesPlayed = p.getFrames().size();
     if (framesPlayed > 2
         && p.getFrames().get(framesPlayed - 2).isStrike()
         && p.getFrames().get(framesPlayed - 3).isStrike()) {
       System.out.print(turkeyArt);
     } else {
       System.out.print(strikeArt);
     }
   } else if (f.isSpare()) {
     System.out.print(spareArt);
   }
 }
Beispiel #3
0
 private static void printBottomLine(Player p, int longestPlayerName) {
   StringBuilder bottomLine = new StringBuilder();
   int padding = longestPlayerName + Constants.SECOND_ROW_PADDING;
   bottomLine.append(String.format("%" + padding + "s", ""));
   List<Frame> printFrames =
       Constants.FRAMES_PER_MATCH > p.getFrames().size()
           ? p.getFrames()
           : p.getFrames().subList(0, Constants.FRAMES_PER_MATCH - 1);
   for (Frame f : printFrames) {
     bottomLine.append(String.format("|%3s|", f.printAbsoluteScore()));
   }
   if (Constants.FRAMES_PER_MATCH <= p.getFrames().size()) {
     Frame g = p.getFrames().get(Constants.FRAMES_PER_MATCH - 1);
     if (Constants.FRAMES_PER_MATCH < p.getFrames().size())
       bottomLine.append(String.format("|%5s|", g.printAbsoluteScore()));
     else bottomLine.append(String.format("|%3s|", g.printAbsoluteScore()));
   }
   bottomLine.append("\n\n");
   System.out.print(bottomLine.toString());
 }
Beispiel #4
0
  private static String printTopLine(Player p, int longestPlayerName) {
    StringBuilder topLine = new StringBuilder();
    topLine.append(String.format("%-" + longestPlayerName + "s|", p.getPlayerName()));
    topLine.append(String.format("%-3s||   ", p.getRunningTotal()));
    List<Frame> regFrames =
        Constants.FRAMES_PER_MATCH > p.getFrames().size()
            ? p.getFrames()
            : p.getFrames().subList(0, Constants.FRAMES_PER_MATCH - 1);
    for (Frame f : regFrames) {
      topLine.append(String.format("|%3s|", f.toString()));
    }
    if (Constants.FRAMES_PER_MATCH <= p.getFrames().size()) {
      Frame g = p.getFrames().get(Constants.FRAMES_PER_MATCH - 1);
      topLine.append("|" + g.toString());

      for (Frame f : p.getFrames().subList(Constants.FRAMES_PER_MATCH, p.getFrames().size())) {
        topLine.append(" " + f.toString());
      }
      topLine.append("|");
    }

    topLine.append("\n");
    System.out.print(topLine.toString());
    return topLine.toString();
  }
Beispiel #5
0
 public static boolean validateScore(
     java.lang.String input, net.heinke.cbingutter.game.player.Player p) {
   java.lang.String pattern = "^[0-9/Xx]$";
   if (!input.matches(pattern)) {
     ScoreboardPainter.printMessage("Please enter 0-9, X or /");
     return false;
   }
   net.heinke.cbingutter.game.frame.Frame f = p.getCurrentFrame();
   if (!input.equals("/") && !input.equalsIgnoreCase("X")) {
     int score = Integer.parseInt(input);
     if (++score > Constants.MAX_PINS_PER_FRAME) {
       ScoreboardPainter.printMessage("Please enter 0-9, X or /");
       return false;
     } else {
       if (score < 0) {
         ScoreboardPainter.printMessage("Please enter a positive score");
         return false;
       }
     }
     if (f != null && !f.isCompleted()) {
       if (f.pinsRemaining() < score) {
         ScoreboardPainter.printMessage("You can't hit pins that don't exist");
         return false;
       }
     }
   } else {
     if (f != null && !f.isCompleted() && input.equalsIgnoreCase("X")) {
       ScoreboardPainter.printMessage("Please enter your actual score or /");
       return false;
     } else {
       if (input.equals("/") && (f == null || f.isCompleted())) {
         ScoreboardPainter.printMessage("Did you meant to enter X?");
         return false;
       }
     }
   }
   return true;
 }