コード例 #1
0
 public void restartGame(View v) {
   for (int i = 1; i <= 9; i++) {
     View mainView =
         findViewById(
             getResources()
                 .getIdentifier("mainView" + Integer.toString(i), "id", getPackageName()));
     if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
       mainView.setBackgroundDrawable(getResources().getDrawable(R.drawable.empty));
     } else {
       mainView.setBackground(getResources().getDrawable(R.drawable.empty));
     }
     player = 1;
     matriz = new int[3][3];
     tv.setText("Jogador 1");
     tv1.setText("");
     vencedor = false;
   }
 }
コード例 #2
0
  public void onClickPlay(View v) {
    if (vencedor) {
      return;
    }
    Drawable background;

    switch (v.getId()) {
      case R.id.mainView1:
        if (matriz[0][0] != 0) {
          return;
        }
        matriz[0][0] = player;
        break;
      case R.id.mainView2:
        if (matriz[0][1] != 0) {
          return;
        }
        matriz[0][1] = player;
        break;
      case R.id.mainView3:
        if (matriz[0][2] != 0) {
          return;
        }
        matriz[0][2] = player;
        break;
      case R.id.mainView4:
        if (matriz[1][0] != 0) {
          return;
        }
        matriz[1][0] = player;
        break;
      case R.id.mainView5:
        if (matriz[1][1] != 0) {
          return;
        }
        matriz[1][1] = player;
        break;
      case R.id.mainView6:
        if (matriz[1][2] != 0) {
          return;
        }
        matriz[1][2] = player;
        break;
      case R.id.mainView7:
        if (matriz[2][0] != 0) {
          return;
        }
        matriz[2][0] = player;
        break;
      case R.id.mainView8:
        if (matriz[2][1] != 0) {
          return;
        }
        matriz[2][1] = player;
        break;
      case R.id.mainView9:
        if (matriz[2][2] != 0) {
          return;
        }
        matriz[2][2] = player;
        break;
    }

    if (player == 1) {
      background = getResources().getDrawable(R.drawable.red);
    } else {
      background = getResources().getDrawable(R.drawable.blue);
    }

    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
      v.setBackgroundDrawable(background);
    } else {
      v.setBackground(background);
    }
    verificaCampeao();

    if (player == 2) {
      tv.setText("Jogador 1");
      if (vencedor) {
        tv1.setText("Vencedor: Jogador 2");
      }
    } else {
      tv.setText("Jogador 2");
      if (vencedor) {
        tv1.setText("Vencedor: Jogador 1");
      }
    }
    player = player == 1 ? 2 : 1;
  }