Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mygame15board;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- Random rnd = new Random();
- Button[][] btns = new Button[4][4];
- int emptyRow, emptyCol;
- MySoundPool myPool;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- int btnId = getResources().getIdentifier("button" + r + c, "id", this.getPackageName());
- btns[r][c] = findViewById(btnId);
- btns[r][c].setOnClickListener(this);
- }
- }
- putNumbers();
- myPool = new MySoundPool(this);
- }
- public void orderTheBoard(View v) {
- int i = 1;
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- //btns[r][c].setText((4*r+c) + "");
- btns[r][c].setText(i + "");
- i = (i + 1) % 16;
- }
- }
- btns[emptyRow][emptyCol].setVisibility(View.VISIBLE);
- btns[3][3].setVisibility(View.INVISIBLE);
- emptyRow = 3;
- emptyCol = 3;
- }
- @Override
- public void onClick(View v) {
- int clickedRow = -1, clickedCol = -1;
- // find cliced row and column
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- if (btns[r][c].getId() == v.getId()) {
- clickedRow = r;
- clickedCol = c;
- }
- }
- }
- if (canMove(clickedRow, clickedCol)) {
- myPool.playClickOk();
- // set visibility
- v.setVisibility(View.INVISIBLE);
- btns[emptyRow][emptyCol].setVisibility(View.VISIBLE);
- // switch texts
- btns[emptyRow][emptyCol].setText(((Button) v).getText().toString());
- ((Button) v).setText("0");
- // update emptyRow and emptyCol
- emptyRow = clickedRow;
- emptyCol = clickedCol;
- // check if game is over
- if (gameIsOver()) {
- myPool.playGameOver();
- Toast.makeText(this, "Game is over!", Toast.LENGTH_SHORT).show();
- // finish();
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- //btns[r][c].setEnabled(false);
- btns[r][c].setOnClickListener(null);
- }
- }
- }
- } else {
- myPool.playClickError();
- }
- }
- private boolean canMove(int clickedRow, int clickedCol) {
- if (clickedRow == emptyRow && (clickedCol == emptyCol + 1 || clickedCol == emptyCol - 1))
- //if (clickedRow==emptyRow && Math.abs(clickedCol- emptyCol)==1)
- {
- return true;
- }
- if (clickedCol == emptyCol && (clickedRow == emptyRow + 1 || clickedRow == emptyRow - 1)) {
- return true;
- }
- return false;
- }
- private boolean gameIsOver() {
- int i = 1;
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- int no = Integer.parseInt(btns[r][c].getText().toString());
- if (no == i)
- i = (i + 1) % 16;
- else
- return false;
- }
- }
- return true;
- }
- public void putNumbers() {
- int i = 0;
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- //btns[r][c].setText((4*r+c) + "");
- btns[r][c].setText(i + "");
- i++;
- }
- }
- // switch numbers on buttons 20 up to times
- int switchCount = rnd.nextInt(20);
- for (i = 0; i < switchCount; i++) {
- int n1 = rnd.nextInt(16);
- int n2 = rnd.nextInt(16);
- while (n1 == n2) {
- n2 = rnd.nextInt(16);
- }
- // switch buttons
- Button b1, b2;
- b1 = btns[getRow(n1)][getCol(n1)];
- b2 = btns[getRow(n2)][getCol(n2)];
- String temp = b1.getText().toString();
- b1.setText(b2.getText().toString());
- b2.setText(temp);
- }
- // hide button with zero
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- if (btns[r][c].getText().toString().equals("0")) {
- btns[r][c].setVisibility(View.INVISIBLE);
- emptyRow = r;
- emptyCol = c;
- }
- }
- }
- }
- private int getRow(int n) {
- return n / 4;
- }
- private int getCol(int n) {
- return n % 4;
- }
- public void restart(View view) {
- btns[emptyRow][emptyCol].setVisibility(View.VISIBLE);
- putNumbers();
- for (int r = 0; r < 4; r++) {
- for (int c = 0; c < 4; c++) {
- btns[r][c].setOnClickListener(this);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement