Exemplo n.º 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    // Initialize the GameSettings
    int[] boardSizeChoices = getResources().getIntArray(R.array.boardSizeChoices);
    int[] numColorsChoices = getResources().getIntArray(R.array.numColorsChoices);
    gameSettings =
        new GameSettings(
            boardSizeChoices[boardSizeChoices.length / 2],
            numColorsChoices[numColorsChoices.length / 2]);

    // Get the FloodView
    floodView = (FloodView) findViewById(R.id.floodView);

    // Initialize the paints array and pass it to the FloodView
    initPaints();
    floodView.setPaints(paints);

    ImageView settingsButton = (ImageView) findViewById(R.id.settingsButton);
    settingsButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent launchSettingsIntent = new Intent(GameActivity.this, SettingsActivity.class);
            launchSettingsIntent.putExtra("boardSize", gameSettings.getBoardSize());
            launchSettingsIntent.putExtra("numColors", gameSettings.getNumColors());
            startActivityForResult(launchSettingsIntent, UPDATE_SETTINGS);
          }
        });

    ImageView infoButton = (ImageView) findViewById(R.id.infoButton);
    infoButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent launchSettingsIntent = new Intent(GameActivity.this, InfoActivity.class);
            startActivity(launchSettingsIntent);
          }
        });

    ImageView newGameButton = (ImageView) findViewById(R.id.newGameButton);
    newGameButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            newGame();
          }
        });

    // Get the steps text view
    stepsTextView = (TextView) findViewById(R.id.stepsTextView);

    // Set up a new game
    newGame();
  }
Exemplo n.º 2
0
  private void newGame() {
    game = new Game(gameSettings.getBoardSize(), gameSettings.getNumColors());
    lastColor = game.getColor(0, 0);

    // Add color buttons
    LinearLayout buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
    buttonLayout.removeAllViews();
    Resources resources = getResources();
    for (int i = 0; i < gameSettings.getNumColors(); i++) {
      final int localI = i;
      ImageView newButton = new ImageView(this);
      newButton.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              v.startAnimation(AnimationUtils.loadAnimation(GameActivity.this, R.anim.button_anim));
              if (localI != lastColor) {
                doColor(localI);
              }
            }
          });
      newButton.setLayoutParams(
          new LinearLayout.LayoutParams(
              ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f));

      Drawable buttonDrawable = ContextCompat.getDrawable(this, R.drawable.button);
      buttonDrawable.setColorFilter(paints[i].getColor(), PorterDuff.Mode.SRC_ATOP);
      newButton.setImageDrawable(buttonDrawable);

      newButton.setPadding(5, 5, 5, 5);
      buttonLayout.addView(newButton);
    }

    stepsTextView.setText(game.getSteps() + " / " + game.getMaxSteps());
    floodView.setBoardSize(gameSettings.getBoardSize());
    floodView.drawGame(game);
  }
Exemplo n.º 3
0
  private void doColor(int color) {
    if (game.getSteps() >= game.getMaxSteps()) {
      return;
    }

    game.flood(color);
    floodView.drawGame(game);
    lastColor = color;
    stepsTextView.setText(game.getSteps() + " / " + game.getMaxSteps());

    if (game.checkWin() || game.getSteps() == game.getMaxSteps()) {
      showEndGameActivity();
    }

    return;
  }