@Test
  public void getThePlayersSymbol() {
    PromptSpy prompt = new PromptSpy(new StringReader(""));
    HumanPlayer player = new HumanPlayer(X, prompt);

    assertThat(player.getSymbol(), is(X));
  }
  @Test
  public void humanPlayerIsReady() {
    PromptSpy prompt = new PromptSpy(new StringReader("1\n"));
    HumanPlayer player = new HumanPlayer(X, prompt);
    Board board = new Board(X, VACANT, X, O, X, O, X, O, VACANT);

    assertThat(player.isReady(), is(true));
  }
  @Test
  public void playerProvidesPromptWithNextMove() throws IOException {
    PromptSpy prompt = new PromptSpy(new StringReader("1\n"));
    HumanPlayer player = new HumanPlayer(X, prompt);
    Board board = new Board(X, VACANT, X, O, X, O, X, O, VACANT);

    assertThat(player.chooseNextMoveFrom(board), is(1));
  }
Ejemplo n.º 4
0
  public PlayerPanel(HumanPlayer player) {
    myCards = new JLabel("My Cards");
    peopleField = new JTextArea();
    roomField = new JTextArea();
    weaponField = new JTextArea();

    // Displaying the players cards to the screen
    ArrayList<Card> humanPlayerCards = new ArrayList<Card>();
    humanPlayerCards = player.getCards();
    // String variables to keep track of more than one type of card
    String roomTxt = "";
    String personTxt = "";
    String weaponTxt = "";

    for (Card c : humanPlayerCards) {
      // Add name to the text if there is already text
      if (c.getCardType() == CardType.PERSON) {
        if (!(personTxt.equals(""))) {
          personTxt = personTxt + "\n" + (c.getName());
        } else {
          personTxt = c.getName();
        }
      } else if (c.getCardType() == CardType.ROOM) {
        if (!(roomTxt.equals(""))) {
          roomTxt = roomTxt + "\n" + (c.getName());
        } else {
          roomTxt = c.getName();
        }
      } else {
        if (!(weaponTxt.equals(""))) {
          weaponTxt = weaponTxt + "\n" + (c.getName());
        } else {
          weaponTxt = c.getName();
        }
      }
    }
    roomField.setText(roomTxt);
    roomField.setEditable(false);
    peopleField.setText(personTxt);
    peopleField.setEditable(false);
    weaponField.setText(weaponTxt);
    weaponField.setEditable(false);

    pPanel = new JPanel();
    rPanel = new JPanel();
    wPanel = new JPanel();

    pPanel.add(peopleField);
    rPanel.add(roomField);
    wPanel.add(weaponField);
    pPanel.setBorder(new TitledBorder(new EtchedBorder(), "People"));
    rPanel.setBorder(new TitledBorder(new EtchedBorder(), "Room"));
    wPanel.setBorder(new TitledBorder(new EtchedBorder(), "Weapon"));

    setLayout(new GridLayout(4, 1));
    add(myCards);
    add(pPanel);
    add(rPanel);
    add(wPanel);
  }
Ejemplo n.º 5
0
  // Constructor
  public PlayerDisplay(HumanPlayer hp) {
    myCards = new JLabel("My Cards");
    pd = new JPanel();
    rd = new JPanel();
    wd = new JPanel();
    String personCard = null;
    String roomCard = null;
    String weaponCard = null;

    for (Card c : hp.getMyCards()) {
      if (c.getCardType() == CardType.PERSON) {
        personCard = c.getName();
      } else if (c.getCardType() == CardType.ROOM) {
        roomCard = c.getName();
      } else {
        weaponCard = c.getName();
      }
    }

    pd = createDisplay("Person", personCard);
    rd = createDisplay("Room", roomCard);
    wd = createDisplay("Weapon", weaponCard);

    setLayout(new GridLayout(8, 1));
    add(myCards);
    add(pd);
    add(rd);
    add(wd);
  }
Ejemplo n.º 6
0
  public static GameAbstract create(int gameCode) throws Exception {
    GameAbstract game = null;

    switch (gameCode) {
        /** Game 4x4 */
      case CUBE4_GAME:
        game = new CubeGame(4);
        break;

        /** Game 3x3 */
      case CUBE3_GAME:
        game = new CubeGame(3);
        break;

        /** Standard */
      case STANDARD_GAME:
        game = new StandardGame(3);
        break;

        /** If type of game is not found, then throws exception */
      default:
        throw new Exception("Wrong type of game");
    }

    /** Add created game to collection */
    getCollection().add(game);

    /** @TODO refactor */
    game.getFrame().setTitle(game.getFrame().getTitle() + ' ' + getCollection().getSize());

    HumanPlayer player1 =
        PlayerCollection.getPlayer(
            (Integer) Workarea.getInstance().getConfigurationHash().get("player1"));
    player1.setIcon(Icons.get("cross-lines"));
    player1.setReady(false);
    game.setPlayer(player1);

    HumanPlayer player2 =
        PlayerCollection.getPlayer(
            (Integer) Workarea.getInstance().getConfigurationHash().get("player2"));
    player2.setIcon(Icons.get("circle"));
    game.setPlayer(player2);

    game.move();

    return game;
  }
Ejemplo n.º 7
0
  /**
   * Main method to start a new Sudoku game. When the game starts the user is asked if they would
   * like to load a premade board from our library, or have a random board made for them. The main
   * method only creates a new instance of the class that is needed. If the user request to solve
   * the game themselves then a new HumanPlayer instance is created and the methods from that class
   * are called. If the user request to have an AI solve the game then a new AIPlayer instance is
   * created and methods from that class are called.
   *
   * @param args command line arguments
   */
  public static void main(String[] args) {
    Boolean keepPlaying = true;

    // repeat while the user wishes to play
    while (keepPlaying) {
      Sudoku game = new Sudoku();

      game.currentState.printBoard();

      // keep this here. It ask the user for an AI or Human Player
      int playOrSolve = game.playOrSolve();

      // case where the user chooses to play themselves
      if (playOrSolve == 1) {
        HumanPlayer humanPlayer = new HumanPlayer(game.currentState);
        humanPlayer.play();
      }
      // case where the user chooses to have the game solved for them
      else {
        AIPlayer aiPlayer = new AIPlayer(game.currentState);
        aiPlayer.play();
      }

      // after the user has completed the board or the AI has solved
      // it, the user is given the option to play again or quit.
      System.out.println("Would you like to play again?");

      while (game.scan.hasNext()) {
        String input = game.scan.next();
        if (input.startsWith("y") || input.startsWith("Y")) {
          break;
        }
        if (input.startsWith("n") || input.startsWith("N")) {
          keepPlaying = false;
          System.exit(0);
        } else {
          System.out.println("Please answer with either yes or no.");
        }
      }
    }
  }
Ejemplo n.º 8
0
 /** Called by server on game end */
 void gameEnded(String str) {
   // In case the game was in progress, may have
   // been waiting for a user move
   if (userMoveNeeded) {
     userMoveNeeded = false;
     userMoveRequested = false;
   }
   this.outcome = str;
   boardUpdated(board, null);
   server = null;
   openAction.setEnabled(true);
   closeAction.setEnabled(true);
   enableServerActions(true);
   enableLaunchActions(false);
   killServerAction.setEnabled(false);
   statusLabel.setText("Game ended, " + outcome + ".");
   // Let the human client thread stop waiting in case it is
   if (theHumanPlayer != null) theHumanPlayer.cancelMoveRequestThread();
 }
Ejemplo n.º 9
0
 @Override
 protected void onClose(int status) {
   log.debug("onClose");
   owner.disconnected();
 }
Ejemplo n.º 10
0
 @Override
 public void onOpen(WsOutbound outbound) {
   log.debug("onOpen");
   this.outbound = outbound;
   owner.open();
 }
Ejemplo n.º 11
0
 @Override
 protected void onTextMessage(CharBuffer charBuffer) throws IOException {
   owner.received(String.valueOf(charBuffer));
 }