Example #1
0
 // auto save to db on pause
 @Override
 protected void onPause() {
   myDB.updatePlayer(game.p1);
   myDB.updatePlayer(game.p2);
   if (!game.ended()) new AsyncSaveGame(this).execute(game);
   super.onPause();
 }
 /*
  * Handles a click on the 'Enter' button.
  */
 public void onInputButtonClick(View view) {
   enterButton.setEnabled(false);
   enterButton.setClickable(false);
   /*
    * Processes the player input.
    */
   if (processInput()) {
     /*
      * Checks whether the round has ended.
      */
     if (game.endRound(getApplicationContext())) {
       setLettersLosingPlayer();
       /*
        * Checks whether the game has ended.
        */
       if (game.ended()) {
         handleEndGame();
       } else {
         game.startNewRound();
         wordFormed.setText("");
       }
     }
     game.changeTurn();
     setImageTurnAndHighlightPlayer();
   } else {
     Toast.makeText(
             getApplicationContext(),
             getString(R.string.ghost_game_text_invalid_input),
             Toast.LENGTH_SHORT)
         .show();
   }
   enterButton.setEnabled(true);
   enterButton.setClickable(true);
 }
Example #3
0
  @TargetApi(Build.VERSION_CODES.KITKAT)
  private void updateView() {
    // Update the current game letters
    TextView textView = (TextView) findViewById(R.id.textview1);
    textView.setText(Html.fromHtml(game.guessedLetters));
    // build up message (if any)
    String messageString = convertMessage();
    TextView message = (TextView) findViewById(R.id.message);
    message.setText(messageString);
    // two textviews indicating player names and score..
    TextView p1tv = (TextView) findViewById(R.id.p1tv);
    TextView p2tv = (TextView) findViewById(R.id.p2tv);
    p1tv.setText(game.p1.getName() + " (" + game.p1.getScore() + ")");
    p1tv.setTextSize(game.turn() ? 30 : 20);
    p2tv.setText(game.p2.getName() + " (" + game.p2.getScore() + ")");
    p2tv.setTextSize(game.turn() ? 20 : 30);
    // text input and submit button
    EditText textInput = (EditText) findViewById(R.id.editText1);
    Button submitButton = (Button) findViewById(R.id.submit);
    // rematch button
    Button rematchButton = (Button) findViewById(R.id.rematch);
    // set the image indicating language
    int uri = dictionaryEnglish ? R.drawable.flag_en : R.drawable.flag_nl;
    if (flagId != uri) { // if not set already..
      flagId = uri;
      Drawable res = ContextCompat.getDrawable(this, uri);
      ImageView imageView = (ImageView) findViewById(R.id.flag);
      imageView.setImageDrawable(res);
    }

    if (game.ended()) { // check if the game has ended and update the display accordingly
      hide_keyboard(this);
      submitButton.setVisibility(View.GONE);
      textInput.setVisibility(View.GONE);
      rematchButton.setVisibility(View.VISIBLE);
    } else { // otherwise the game is running or restarted
      submitButton.setVisibility(View.VISIBLE);
      textInput.setVisibility(View.VISIBLE);
      rematchButton.setVisibility(View.GONE);
      textInput.setText("");
    }
  }
Example #4
0
  public void onSubmit(View view) {
    EditText textInput = (EditText) findViewById(R.id.editText1);

    String letter = textInput.getText().toString().toLowerCase();
    // validate if input is filled in and not a number
    if (letter.length() != 1 || !Character.isLetter(letter.charAt(0))) {
      Toast.makeText(getApplicationContext(), R.string.toast_valid_character, Toast.LENGTH_SHORT)
          .show();
    } else {
      game.guess(letter); // counts as a guess if game is not ended already.
      if (game.ended()) { // save the player that wins because score changed
        game.getPlayerObject(game.winner()).addScore();
        // updates DB on score change;
        myDB.updatePlayer(game.getPlayerObject(game.winner()));
        // notify this activity of player change
        player1 = game.p1;
        player2 = game.p2;
      }
      // async save on every turn..
      new AsyncSaveGame(this).execute(game);
    }
    updateView();
  }