Commit 06722762 authored by Houjie Xiong's avatar Houjie Xiong
Browse files

Final version

parent aca56400
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
package com.example.tetrisbut2048;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.material.textfield.TextInputEditText;

import java.util.ArrayList;
import java.util.Arrays;

public class GameOver extends Rule{
    public static int finalScore = 0;
    protected SharedPreferences sharedPreferences;
    protected ArrayList<String> scoreHistory;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -16,6 +25,9 @@ public class GameOver extends Rule{
        TextView show_score = (TextView) findViewById(R.id.show_score);
        show_score.setText("Your Score is: " + finalScore);

        sharedPreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
        scoreHistory = new ArrayList<>();

        Button button_toMain = (Button) findViewById(R.id.button_toMain);
        Button button_save_score = (Button) findViewById(R.id.button_save_score);

@@ -32,6 +44,7 @@ public class GameOver extends Rule{
            @Override
            public void onClick(View v) {
                soundPool.play(buttonSound, 1.0f, 1.0f, 0, 0, 1.0f);
                save_score();
                openScore();
            }
        });
@@ -40,4 +53,24 @@ public class GameOver extends Rule{
        Intent intent = new Intent(this, Score.class);
        startActivity(intent);
    }
    private void save_score() {
        TextInputEditText textInputEditText = findViewById(R.id.enter_username);
        String username = textInputEditText.getText().toString();
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String scoreHistoryString = sharedPreferences.getString("scoreHistory", "");
        if (!scoreHistoryString.isEmpty()) {
            String[] scoreHistoryArray = scoreHistoryString.split(",");
            scoreHistory.addAll(Arrays.asList(scoreHistoryArray));
        }
        scoreHistory.add(username + ": " + finalScore);
        scoreHistoryString = "";
        for (int i = 0; i < scoreHistory.size(); i++) {
            scoreHistoryString += scoreHistory.get(i);
            if (i < scoreHistory.size() - 1) {
                scoreHistoryString += ",";
            }
        }
        editor.putString("scoreHistory", scoreHistoryString);
        editor.apply();
    }
}
+26 −1
Original line number Diff line number Diff line
@@ -6,13 +6,38 @@ import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

public class Score extends Setting {

public class Score extends Setting {
    private SharedPreferences sharedPreferences;
    private ArrayList<String> scoreHistory;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        sharedPreferences = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
        scoreHistory = new ArrayList<>();
        String scoreHistoryString = sharedPreferences.getString("scoreHistory", "");
        if (!scoreHistoryString.isEmpty()) {
            String[] scoreHistoryArray = scoreHistoryString.split(",");
            scoreHistory.addAll(Arrays.asList(scoreHistoryArray));
        }
        scoreHistoryString = "";
        for (int i = 0; i < scoreHistory.size(); i++) {
            scoreHistoryString += scoreHistory.get(i);
            if (i < scoreHistory.size() - 1) {
                scoreHistoryString += "\n";
            }
        }

        TextView scoretext = (TextView) findViewById(R.id.textView3);
        scoretext.setText(scoreHistoryString);

        SoundPool soundPool = new SoundPool.Builder().build();
        int buttonSound = soundPool.load(this, R.raw.button_sound, 1);
+9 −22
Original line number Diff line number Diff line
@@ -8,10 +8,10 @@

    <TextView
        android:id="@+id/textView_score"
        android:layout_width="223dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="Top scores"
        android:text="Your Score History"
        android:textColor="@color/black"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
@@ -28,29 +28,16 @@
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rank"
        android:layout_width="15dp"
        android:layout_height="183dp"
        android:layout_marginTop="62dp"
        android:layout_marginEnd="16dp"
        android:text="1\n2\n3\n4\n5\n"
        android:textColor="@color/black"
        android:textSize="25dp"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="@+id/textView_score" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="190dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:text="@string/highestscores"
        android:layout_marginTop="16dp"
        android:text=""
        android:textColor="@color/black"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="@+id/rank"
        app:layout_constraintStart_toStartOf="@+id/textView_score"
        app:layout_constraintTop_toTopOf="@+id/textView_score"
        app:layout_constraintVertical_bias="0.95" />
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_score" />

</androidx.constraintlayout.widget.ConstraintLayout>