Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- -----------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btnNew"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/tvCounter"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center"
- android:text="MyText"
- android:textSize="30sp"
- tools:background="@android:color/holo_blue_bright" />
- <Button
- android:id="@+id/btnSave"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save"
- android:textSize="30sp" />
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btn1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B1"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B2"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn3"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B3"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn4"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B4"
- android:textSize="40sp" />
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btn5"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B1"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn6"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B2"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn7"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B3"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn8"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B4"
- android:textSize="40sp" />
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btn9"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B1"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn10"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B2"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn11"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B3"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn12"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B4"
- android:textSize="40sp" />
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="horizontal">
- <Button
- android:id="@+id/btn13"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B1"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn14"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B2"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn15"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B3"
- android:textSize="40sp" />
- <Button
- android:id="@+id/btn16"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:text="B4"
- android:textSize="40sp" />
- </LinearLayout>
- </LinearLayout>
- MainActivity.java
- -----------------
- package com.example.mohamadpc.mygame;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- Button[] myBtn;
- TextView tvCounter;
- int emptyIndex;
- int counter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvCounter = findViewById(R.id.tvCounter);
- myBtn = new Button[16];
- for (int i = 0; i < 16; i++) {
- String btnName = "btn" + (i + 1);
- int btnId = getResources().getIdentifier(btnName, "id", getPackageName());
- myBtn[i] = findViewById(btnId);
- myBtn[i].setOnClickListener(this);
- }
- if (loadGame() == false) {
- Toast.makeText(this, "New Game", Toast.LENGTH_LONG).show();
- newGame();
- }
- findViewById(R.id.btnSave).setOnClickListener(this);
- findViewById(R.id.btnNew).setOnClickListener(this);
- }
- private void shuffle(int n) {
- Random rnd = new Random();
- for (int i = 0; i < n; i++) {
- int b1 = rnd.nextInt(16);
- int b2 = rnd.nextInt(16);
- String text = myBtn[b1].getText().toString();
- myBtn[b1].setText(myBtn[b2].getText().toString());
- myBtn[b2].setText(text);
- if (b1 == emptyIndex) {
- emptyIndex = b2;
- } else if (b2 == emptyIndex) {
- emptyIndex = b1;
- }
- }
- }
- private boolean gameIsOver() {
- for (int i = 0; i < 15; i++) {
- if (!myBtn[i].getText().toString().equals(i + 1 + "")) {
- return false;
- }
- }
- return true;
- }
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.btnSave) {
- //save the game
- saveGame();
- } else if (v.getId() == R.id.btnNew) {
- newGame();
- } else {
- int btnIndex = getButtonIndex(v.getId());
- if (canMove(btnIndex, emptyIndex)) {
- String text = myBtn[btnIndex].getText().toString();
- myBtn[btnIndex].setText("");
- myBtn[emptyIndex].setText(text);
- emptyIndex = btnIndex;
- counter += 1;
- tvCounter.setText("Moves : " + counter);
- if (gameIsOver()) {
- Toast.makeText(this, "Game Is Over", Toast.LENGTH_LONG).show();
- finish();
- }
- }
- }
- }
- private void newGame() {
- counter = 0;
- tvCounter.setText("Moves: " + counter);
- for (int i = 0; i < 16; i += 1) {
- myBtn[i].setText(i + 1 + "");
- }
- myBtn[15].setText("");
- emptyIndex = 15;
- shuffle(10);
- }
- private void saveGame() {
- Context context = getApplicationContext();
- SharedPreferences sp = context.getSharedPreferences("saved_game", Context.MODE_PRIVATE);
- // start editing
- SharedPreferences.Editor editor = sp.edit();
- // edit values to be saved
- editor.putInt("MovesCounter", counter);
- for (int i = 0; i < 16; i += 1) {
- editor.putString("Key" + i, myBtn[i].getText().toString());
- }
- // save all changes
- editor.commit();
- Toast.makeText(context, "Game has been saved!", Toast.LENGTH_LONG).show();
- }
- public boolean loadGame() {
- Context context = getApplicationContext();
- SharedPreferences sp = context.getSharedPreferences("saved_game", Context.MODE_PRIVATE);
- // get saved counter
- counter = sp.getInt("MovesCounter", -1);
- if (counter == -1) {
- return false;
- }
- tvCounter.setText(counter + "");
- for (int i = 0; i < 16; i++) {
- String savedText = sp.getString("Key" + i, "");
- myBtn[i].setText(savedText);
- if (savedText.equals("")) {
- emptyIndex = i;
- }
- }
- return true;
- }
- private boolean canMove(int btnIndex, int emptyIndex) {
- if (btnIndex == emptyIndex + 1 && btnIndex / 4 == emptyIndex / 4) {
- // move number to the left
- return true;
- } else if (btnIndex == emptyIndex - 1 && btnIndex / 4 == emptyIndex / 4) {
- // move number to the right
- return true;
- } else if (btnIndex == emptyIndex + 4) {
- // move number up
- return true;
- } else if (btnIndex == emptyIndex - 4) {
- // move number down
- return true;
- }
- return false;
- }
- private int getButtonIndex(int id) {
- for (int i = 0; i < 16; i++) {
- if (myBtn[i].getId() == id) {
- return i;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement