Exemplo n.º 1
0
 public void testSpareWithFrameCounting() throws Exception {
   assertEquals(0, game.scoredFrames());
   game.roll(4);
   assertEquals("4", arrayAsString(game.rollsForFrame(1)));
   game.roll(6);
   assertEquals(1, game.finishedFrames());
   assertEquals(0, game.scoredFrames()); // cant score it yet
   assertEquals("4 6", arrayAsString(game.rollsForFrame(1)));
   game.roll(5);
   assertEquals(1, game.scoredFrames()); // can score it now
   game.roll(3);
   assertEquals(2, game.finishedFrames());
   assertEquals(2, game.scoredFrames()); // score right away
   rollMany(16, 0);
   assertEquals(10, game.finishedFrames());
   assertTrue(game.gameOver());
   assertEquals(10, game.scoredFrames());
   assertEquals(23, game.score());
 }
Exemplo n.º 2
0
 /**
  * Listens to the connected game and responds to the commands it sends. Note: this function will
  * never return, it will exit the program on errors or when the connected computer exits.
  */
 @SuppressWarnings({"nls", "null"})
 public void readloop() {
   InputStream netin = null;
   try {
     netin = socket.getInputStream();
   } catch (IOException e) {
     System.out.println("Congratulation, you've found a bug!");
     System.exit(1);
   }
   while (true) {
     int c = -2;
     try {
       c = netin.read();
     } catch (EOFException e) {
       System.out.println("Unexpected end of stream.");
       System.exit(1); /* This can probably be recovered from */
     } catch (IOException e) {
       System.out.println("Connection error.");
       System.exit(1); /* Why not retry to connect? */
     }
     if (c == -1) {
       JOptionPane.showMessageDialog(
           null, "Opponent game over", "Pentris!", JOptionPane.INFORMATION_MESSAGE);
       System.exit(1);
     }
     switch (c % (1 << MSG_BITS)) {
       case MSG_GAMEOVER:
         game.gameOver(true, false);
         System.exit(0);
         break;
       case MSG_RESTART:
         game.restart(false);
         break;
       case MSG_BYE:
         JOptionPane.showMessageDialog(
             null, "Opponent game over", "Pentris!", JOptionPane.INFORMATION_MESSAGE);
         game.gameOver(true, false);
         System.exit(0);
         break;
       case MSG_NOP:
         break;
       case MSG_IAMSERVER:
         if (isServer) {
           System.out.println("Opponent claims to also be server," + " ignoring.");
         } else {
           try {
             long seed = 0;
             for (int i = 0; i < 8; i++) {
               c = netin.read();
               if (c == -1) throw new EOFException();
               seed = seed * 256 + c;
             }
             System.out.println("Got seed " + seed);
             game.setSeed(seed);
             opponentState = new Game(seed, "Opponent game");
             opponentThread = new Thread(opponentState, "Opponent visualisation thread");
             opponentState.moveFrameRight();
             opponentState.setOpponentGame(game);
             opponentState.setNoAutoMove(true);
             game.relayMovements(this);
             opponentThread.start();
             synchronized (opponentState) {
               try {
                 if (opponentState.getFrame() == null) opponentState.wait();
               } catch (InterruptedException e) {
                 System.exit(1);
               }
             }
             opponentState.unPauseGame();
           } catch (EOFException e) {
             System.out.println("Unexpected end of stream.");
             System.exit(1); // This can probably be recovered from
           } catch (IOException e) {
             System.out.println("Connection error.");
             System.exit(1); /* Why not retry to connect? */
           }
         }
         game.unPauseGame();
         break;
       case MSG_MOVEDOWN:
         opponentState.doMoveDown(false);
         break;
       case MSG_MOVERIGHT:
         opponentState.moveRight();
         break;
       case MSG_MOVELEFT:
         opponentState.moveLeft();
         break;
       case MSG_MOVEROTATE:
         opponentState.doRotate();
         break;
       case MSG_OPPONENT_MARKED_RUBBLE:
         System.out.format("Opponent marked %d lines\n", c >> MSG_BITS);
         opponentState.addBottomLines(c >> MSG_BITS);
         break;
       default:
         System.out.println("Unknown command from remote: " + c);
     }
   }
 }
Exemplo n.º 3
0
  /**
   * Creates and runs a scoreboard for a variety of games
   *
   * @param args command line arguments (none expected)
   */
  public static void main(String args[]) {
    Scanner r = new Scanner(System.in);
    int trip = 0;
    boolean err = true;
    Game game = null;
    while (err) {
      try {
        System.out.print(
            "\nSelect Sport (by number): \n"
                + "  1)   Football\n"
                + "  2)   Soccer\n"
                + "  3)   Basketball\n"
                + "  4)   Hockey\n"
                + "Enter choice: ");
        trip = r.nextInt();
        r.nextLine();
        if (trip > 4 || trip < 1) {
          throw new IndexOutOfBoundsException();
        }
        err = false;
      } catch (IndexOutOfBoundsException e) {
        System.out.println("Invalid Option, try again");
        // r.nextLine();
      } catch (InputMismatchException e) {
        System.out.println("Please enter the menu item number");
        r.nextLine();
      }
    }

    if (trip > 0) {
      System.out.println("ENTER TEAMS");
      System.out.print("Home: ");
      Team team1 = new Team(r.nextLine());
      System.out.print("Away: ");
      Team team2 = new Team(r.nextLine());
      System.out.println();

      switch (trip) {
        case 1:
          game = new Football(team1, team2);
          break;
        case 2:
          game = new Soccer(team1, team2);
          break;
        case 3:
          game = new Basketball(team1, team2);
          break;
        case 4:
          game = new Hockey(team1, team2);
          break;
      }

      while (!game.gameOver()) {
        err = true;
        int menuCount = 0;
        while (err) {
          menuCount = 1;
          // PRINT MENU
          try {

            ArrayList<ScoringMethod> scoringMethods = game.getScoringMethods();
            /* Displays dynamically generated scoreboard containing all available scoring methods for both teams */

            System.out.println(
                team1.getName()
                    + " - "
                    + team1.getScore()
                    + ", "
                    + team2.getName()
                    + " - "
                    + team2.getScore());
            System.out.println("Current " + game.getPeriodName() + ": " + game.getPeriod());

            System.out.println("Menu: ");
            for (int i = 0; i < (game.getScoringMethods().size()); i++) {
              System.out.println(
                  "  "
                      + menuCount
                      + ") "
                      + team1.getName()
                      + " "
                      + game.getScoringMethods().get(i).getName());
              menuCount++;
            }
            for (int i = 0; i < (game.getScoringMethods().size()); i++) {
              System.out.println(
                  "  "
                      + menuCount
                      + ") "
                      + team2.getName()
                      + " "
                      + game.getScoringMethods().get(i).getName());
              menuCount++;
            }
            System.out.println("  " + menuCount + ") End " + game.getPeriodName());

            System.out.print("Enter choice: ");

            trip = r.nextInt();

            if (trip > ((scoringMethods.size() * 2) + 1) || trip < 1) {
              throw new IndexOutOfBoundsException();
            }

            err = false;
          } catch (IndexOutOfBoundsException e) {
            System.out.println("Invalid Option, try again " + e);
            r.nextLine();
          } catch (InputMismatchException e) {
            System.out.println("Please enter the menu item number");
            r.nextLine();
          }
        }

        if (trip == menuCount) {
          game.endPeriod();
        } else if (trip > ((menuCount - 1) / 2)) {
          trip -= (menuCount - 1) / 2;
          team2.addScore(game.getScoringMethods().get(trip - 1).getPoints());
        } else {
          team1.addScore(game.getScoringMethods().get(trip - 1).getPoints());
        }
      }
      System.out.println("\n\nGame is over.");
      System.out.println(
          "Final Score: "
              + team1.getName()
              + " - "
              + team1.getScore()
              + ", "
              + team2.getName()
              + " - "
              + team2.getScore());
      String winner = game.getWinner();
      if (winner.equals("Nobody")) {
        System.out.println("Tie");
        // System.out.println(winner+" wins :(");
      } else {
        System.out.println(winner + " wins!");
      }
    }
    r.close();
  }
Exemplo n.º 4
0
 /**
  * Let's the player fall down dead, makes sure the runcycle stops and invokes the next method for
  * the dialog and stuff.
  */
 public void gameOver() {
   this.shouldRun = false;
   playerDeadFall();
   game.gameOver();
 }