diff --git a/README.md b/README.md index d9ba123..138e857 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@


- A Task Performance in Mobile App that aims to develop a simple mobile game like memory game. + subject badge + version badge +

+ A Task Performance in Mobile App that aims to develop a simple mobile game like memory game.

## **Description** 🔥 @@ -10,9 +13,6 @@ last pair has been picked up. ## **Scoring** 💯 The scoring system in the game is straightforward: correctly guessing a pair awards an additional 10 points, while an incorrect guess results in a deduction of 5 points. -| Correct | Wrong | -| :---: | :---: | -| + 10 points | -5 points | ## **Screen Shots** 📷

@@ -25,7 +25,7 @@ The scoring system in the game is straightforward: correctly guessing a pair awa

## **Video** 🎥 -https://github.com/Jookie262/animal-match-up/assets/62915062/844c26a8-14ed-4787-804c-a985e8c1e07d +https://github.com/Jookie262/animal-match-up/assets/62915062/15ffa033-a637-4384-9df6-44708e31dd5c ## **Special Thanks** 💖 I would like to express my sincere appreciation to Wajahat Karim (author of [EasyFlipView](https://github.com/wajahatkarim3/EasyFlipView)) for providing me with the incredible EasyFlipView library. It has been instrumental in making my simple project a success and adding an engaging flipping animation to my application. diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9cb2c71..a2908af 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,9 @@ android:supportsRtl="true" android:theme="@style/Theme.AnimalMatchUp" tools:targetApi="31"> + diff --git a/app/src/main/java/com/example/animalmatchup/HighScoreScreen.java b/app/src/main/java/com/example/animalmatchup/HighScoreScreen.java new file mode 100644 index 0000000..1456b73 --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/HighScoreScreen.java @@ -0,0 +1,79 @@ +package com.example.animalmatchup; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import android.os.Bundle; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; + +import com.example.animalmatchup.adapter.scoreadapter.ScoreAdapter; +import com.example.animalmatchup.game.InfoBox; +import com.example.animalmatchup.game.PopulateScore; +import com.example.animalmatchup.model.ScoreModel; + +import java.util.ArrayList; + +public class HighScoreScreen extends AppCompatActivity { + ImageView backBtn, infoBtn; + TextView highest_score_txt, highest_score_name_txt; + RecyclerView score_view; + InfoBox infoBox; + PopulateScore populateScore; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_high_score_screen); + init(); + clickBackInfo(); + scoreRecyclerView(); + changeHighestScore(); + } + + public void init(){ + backBtn = findViewById(R.id.back_btn); + infoBtn = findViewById(R.id.info_btn); + infoBox = new InfoBox(); + highest_score_txt = findViewById(R.id.highest_score_txt); + highest_score_name_txt = findViewById(R.id.highest_score_name_txt); + score_view = findViewById(R.id.score_view); + populateScore = new PopulateScore(getApplicationContext()); + } + + public void clickBackInfo(){ + backBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); + + infoBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + infoBox.infoBox(HighScoreScreen.this); + } + }); + } + + public void changeHighestScore(){ + ArrayList highestScores = populateScore.getHighestScore(); + if (!highestScores.isEmpty()) { + ScoreModel highestScore = highestScores.get(0); + String highestScoreValue = String.valueOf(highestScore.getScore()); + String highestScoreName = highestScore.getName(); + highest_score_txt.setText(highestScoreValue); + highest_score_name_txt.setText(highestScoreName); + } + } + + public void scoreRecyclerView(){ + PopulateScore populateScore = new PopulateScore(getApplicationContext()); + ScoreAdapter scoreAdapter = new ScoreAdapter(populateScore.populateScore()); + score_view.setLayoutManager(new GridLayoutManager(this, 1)); + score_view.setAdapter(scoreAdapter); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/animalmatchup/WelcomeScreen.java b/app/src/main/java/com/example/animalmatchup/WelcomeScreen.java index c5bd062..3c2ca01 100644 --- a/app/src/main/java/com/example/animalmatchup/WelcomeScreen.java +++ b/app/src/main/java/com/example/animalmatchup/WelcomeScreen.java @@ -2,19 +2,24 @@ import androidx.appcompat.app.AppCompatActivity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; +import android.widget.Toast; + +import com.example.animalmatchup.game.InfoBox; public class WelcomeScreen extends AppCompatActivity { - ImageButton playButton; + ImageButton playButton, highScoreButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome_screen); playButton = findViewById(R.id.play_button); + highScoreButton = findViewById(R.id.high_score_button); playButton.setOnClickListener(new View.OnClickListener() { @Override @@ -23,5 +28,13 @@ public void onClick(View v) { startActivity(i); } }); + + highScoreButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent i = new Intent(WelcomeScreen.this, HighScoreScreen.class); + startActivity(i); + } + }); } } \ No newline at end of file diff --git a/app/src/main/java/com/example/animalmatchup/adapter/CardAdapter.java b/app/src/main/java/com/example/animalmatchup/adapter/gameadapter/CardAdapter.java similarity index 78% rename from app/src/main/java/com/example/animalmatchup/adapter/CardAdapter.java rename to app/src/main/java/com/example/animalmatchup/adapter/gameadapter/CardAdapter.java index fae5215..56c9d9c 100644 --- a/app/src/main/java/com/example/animalmatchup/adapter/CardAdapter.java +++ b/app/src/main/java/com/example/animalmatchup/adapter/gameadapter/CardAdapter.java @@ -1,4 +1,4 @@ -package com.example.animalmatchup.adapter; +package com.example.animalmatchup.adapter.gameadapter; import android.content.Context; import android.os.Handler; @@ -6,12 +6,16 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.RecyclerView; import com.example.animalmatchup.R; +import com.example.animalmatchup.WelcomeScreen; +import com.example.animalmatchup.game.InfoBox; +import com.example.animalmatchup.game.ScoreAnimation; import com.example.animalmatchup.model.CardModel; import com.example.animalmatchup.model.GameModel; import com.example.animalmatchup.play.CongratsScreen; @@ -25,21 +29,24 @@ public class CardAdapter extends RecyclerView.Adapter { private ArrayList names; GameModel gameModel; Context context; - TextView gameScore; + TextView gameScore, animScore; int totalCard; String fragment_round_num; FragmentManager fragment; + ScoreAnimation scoreAnimation; - public CardAdapter(ArrayList mData, Context context, GameModel gameModel, TextView gameScore, int totalCard, FragmentManager fragment, String fragment_round_num){ + public CardAdapter(ArrayList mData, Context context, GameModel gameModel, TextView gameScore, TextView animScore, int totalCard, FragmentManager fragment, String fragment_round_num){ this.mData = mData; this.context = context; this.gameModel = gameModel; this.gameScore = gameScore; + this.animScore = animScore; this.totalCard = totalCard; this.fragment_round_num = fragment_round_num; this.fragment = fragment; flipCards = new ArrayList<>(); names = new ArrayList<>(); + scoreAnimation = new ScoreAnimation(); } @NonNull @@ -53,9 +60,9 @@ public CardHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public void onBindViewHolder(@NonNull CardHolder holder, int position) { CardModel model = mData.get(position); holder.getBackImage().setImageResource(model.getBack_img()); - holder.getFrontImage().setImageResource(model.getImg()); + holder.getFrontImage().setImageResource(model.getFront_img()); Handler handler = new Handler(); - gameLogic(holder.getEasyFlipView(), handler, model); + gameLogic(holder.getEasyFlipView(), handler, model, scoreAnimation); } @Override @@ -63,7 +70,7 @@ public int getItemCount() { return mData.size(); } - public void gameLogic(EasyFlipView flipView, Handler handler, CardModel model){ + public void gameLogic(EasyFlipView flipView, Handler handler, CardModel model, ScoreAnimation scoreAnimation){ flipView.setOnFlipListener(new EasyFlipView.OnFlipAnimationListener() { @Override public void onViewFlipCompleted(EasyFlipView easyFlipView, EasyFlipView.FlipState newCurrentSide) { @@ -78,6 +85,7 @@ public void onViewFlipCompleted(EasyFlipView easyFlipView, EasyFlipView.FlipStat if (flipCards.size() == 2) { if(names.get(0).equals(names.get(1))){ totalCard--; + scoreAnimation.animationScore(animScore, "+10"); gameModel.setScore(+10); handler.postDelayed(new Runnable() { @@ -91,6 +99,7 @@ public void run() { } }, 200); } else { + scoreAnimation.animationScore(animScore, "-5"); gameModel.setScore(-5); handler.postDelayed(new Runnable() { @Override @@ -104,7 +113,8 @@ public void run() { }, 200); } } - gameScore.setText(String.valueOf(gameModel.getScore())); + scoreAnimation.delaySetText(gameScore, String.valueOf(gameModel.getScore())); + //gameScore.setText(String.valueOf(gameModel.getScore())); if(totalCard == 0){ handler.postDelayed(new Runnable() { @@ -113,10 +123,12 @@ public void run() { if(fragment_round_num.equals("Round 1")){ fragment.beginTransaction().replace(R.id.fragment_container, new CongratsScreen(gameModel, "Round 1")).commit(); } else if(fragment_round_num.equals("Round 2")){ + InfoBox infoBox = new InfoBox(); + infoBox.addNameScore(context, String.valueOf(gameModel.getScore())); fragment.beginTransaction().replace(R.id.fragment_container, new CongratsScreen(gameModel, "Round 2")).commit(); } } - }, 300); + }, 800); } } }); diff --git a/app/src/main/java/com/example/animalmatchup/adapter/CardHolder.java b/app/src/main/java/com/example/animalmatchup/adapter/gameadapter/CardHolder.java similarity index 94% rename from app/src/main/java/com/example/animalmatchup/adapter/CardHolder.java rename to app/src/main/java/com/example/animalmatchup/adapter/gameadapter/CardHolder.java index c4f5dff..15d9062 100644 --- a/app/src/main/java/com/example/animalmatchup/adapter/CardHolder.java +++ b/app/src/main/java/com/example/animalmatchup/adapter/gameadapter/CardHolder.java @@ -1,4 +1,4 @@ -package com.example.animalmatchup.adapter; +package com.example.animalmatchup.adapter.gameadapter; import android.view.View; import android.widget.ImageView; diff --git a/app/src/main/java/com/example/animalmatchup/adapter/scoreadapter/ScoreAdapter.java b/app/src/main/java/com/example/animalmatchup/adapter/scoreadapter/ScoreAdapter.java new file mode 100644 index 0000000..3e12d5b --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/adapter/scoreadapter/ScoreAdapter.java @@ -0,0 +1,42 @@ +package com.example.animalmatchup.adapter.scoreadapter; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.example.animalmatchup.R; +import com.example.animalmatchup.adapter.gameadapter.CardHolder; +import com.example.animalmatchup.model.ScoreModel; + +import java.util.ArrayList; + +public class ScoreAdapter extends RecyclerView.Adapter { + private ArrayList scoreModels; + + public ScoreAdapter(ArrayList scoreModels){ + this.scoreModels = scoreModels; + } + + @NonNull + @Override + public ScoreHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.score_card, parent, false); + return new ScoreHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull ScoreHolder holder, int position) { + ScoreModel scoreModel = scoreModels.get(position); + holder.getName().setText(scoreModel.getName()); + holder.getScore().setText(String.valueOf(scoreModel.getScore())); + holder.getRank().setText(String.valueOf(position + 2)); + } + + @Override + public int getItemCount() { + return scoreModels.size(); + } +} diff --git a/app/src/main/java/com/example/animalmatchup/adapter/scoreadapter/ScoreHolder.java b/app/src/main/java/com/example/animalmatchup/adapter/scoreadapter/ScoreHolder.java new file mode 100644 index 0000000..5074fa5 --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/adapter/scoreadapter/ScoreHolder.java @@ -0,0 +1,34 @@ +package com.example.animalmatchup.adapter.scoreadapter; + +import android.view.View; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.example.animalmatchup.R; + +public class ScoreHolder extends RecyclerView.ViewHolder { + private TextView rank, name, score; + + public ScoreHolder(@NonNull View itemView) { + super(itemView); + rank = itemView.findViewById(R.id.rank_txt); + name = itemView.findViewById(R.id.name_txt); + score = itemView.findViewById(R.id.score_txt); + } + + public TextView getRank() { + return rank; + } + + + public TextView getName() { + return name; + } + + public TextView getScore() { + return score; + } + +} diff --git a/app/src/main/java/com/example/animalmatchup/game/InfoBox.java b/app/src/main/java/com/example/animalmatchup/game/InfoBox.java new file mode 100644 index 0000000..d4bf6cc --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/game/InfoBox.java @@ -0,0 +1,98 @@ +package com.example.animalmatchup.game; + +import android.content.Context; +import android.content.DialogInterface; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.text.Html; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.appcompat.app.AlertDialog; + +import com.example.animalmatchup.R; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.textfield.TextInputEditText; +import com.google.android.material.textfield.TextInputLayout; + +public class InfoBox { + + private AlertDialog alertDialog; + ScoreDB scoreDB; + + public void infoBox(Context context){ + // Create the object of AlertDialog Builder class + AlertDialog.Builder builder = new AlertDialog.Builder(context); + + // Set the message show for the Alert time + builder.setMessage("A Task Performance in Mobile App that aims to develop a simple mobile game like Memory Game"); + + // Set Alert Title + builder.setTitle("Animal Match Up"); + + // Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain show + builder.setCancelable(false); + + // Set the positive button with yes name Lambda OnClickListener method is use of DialogInterface interface. + builder.setPositiveButton("Ok", (DialogInterface.OnClickListener) (dialog, which) -> { + // When the user click yes button then app will close + }); + + // Create the Alert dialog + AlertDialog alertDialog = builder.create(); + // Show the Alert Dialog box + alertDialog.show(); + } + + // Create the dialog only once + private void createDialog(Context context, String score) { + // Create a custom layout for the dialog + LayoutInflater inflater = LayoutInflater.from(context); + View dialogView = inflater.inflate(R.layout.dialog_layout, null); + TextInputLayout editText = dialogView.findViewById(R.id.enter_name_txt); + TextView alertScore = dialogView.findViewById(R.id.alert_score); + Button button = dialogView.findViewById(R.id.ok_button); + + final MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(context) + .setView(dialogView) + .setCancelable(false); + + alertScore.setText("Your Score: " + score); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String name = editText.getEditText().getText().toString(); + scoreDB = new ScoreDB(context); + + if(name.isEmpty() || name.length() > 10){ + editText.setError("Please follow the instructions"); + } else { + scoreDB.addScore(name, score); + Toast.makeText(context, "Successfully Saved", Toast.LENGTH_SHORT).show(); + alertDialog.dismiss(); + } + } + }); + + alertDialog = dialogBuilder.create(); + } + + public void addNameScore(Context context, String score) { + // Create the dialog if it doesn't exist + if (alertDialog == null) { + createDialog(context, score); + } + + // Show the dialog + alertDialog.show(); + } + + +} diff --git a/app/src/main/java/com/example/animalmatchup/model/PopulateCard.java b/app/src/main/java/com/example/animalmatchup/game/PopulateCard.java similarity index 90% rename from app/src/main/java/com/example/animalmatchup/model/PopulateCard.java rename to app/src/main/java/com/example/animalmatchup/game/PopulateCard.java index 86773d4..d18b091 100644 --- a/app/src/main/java/com/example/animalmatchup/model/PopulateCard.java +++ b/app/src/main/java/com/example/animalmatchup/game/PopulateCard.java @@ -1,6 +1,7 @@ -package com.example.animalmatchup.model; +package com.example.animalmatchup.game; import com.example.animalmatchup.R; +import com.example.animalmatchup.model.CardModel; import java.util.ArrayList; import java.util.Collections; @@ -23,7 +24,7 @@ public ArrayList shuffleDuplicateAnimals(ArrayList origina for (CardModel animal : originalList) { duplicatedList.add(animal); - duplicatedList.add(new CardModel(animal.getName(), animal.getBack_img(), animal.getImg())); + duplicatedList.add(new CardModel(animal.getName(), animal.getBack_img(), animal.getFront_img())); } Collections.shuffle(duplicatedList); diff --git a/app/src/main/java/com/example/animalmatchup/game/PopulateScore.java b/app/src/main/java/com/example/animalmatchup/game/PopulateScore.java new file mode 100644 index 0000000..2a7a0d6 --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/game/PopulateScore.java @@ -0,0 +1,52 @@ +package com.example.animalmatchup.game; + +import android.content.Context; + +import com.example.animalmatchup.model.ScoreModel; + +import java.util.ArrayList; + +public class PopulateScore { + + Context context; + ScoreDB scoreDB; + ArrayList allScores; + + public PopulateScore(Context context){ + this.context = context; + scoreDB = new ScoreDB(context); + allScores = scoreDB.viewAllScores(); + } + + public ArrayList getHighestScore(){ + if (allScores.size() > 0) { + // Get the highest score from the list + ScoreModel highestScore = allScores.get(0); + + // Create a new ArrayList to hold the highest score + ArrayList highestScoreList = new ArrayList<>(); + highestScoreList.add(highestScore); + + return highestScoreList; + } else { + // Return an empty ArrayList if there are no scores + return new ArrayList<>(); + } + } + + public ArrayList populateScore(){ + if (allScores.size() > 1) { + // Select ArrayList from the second element onwards + ArrayList selectedScores; + if (allScores.size() >= 7) { + selectedScores = new ArrayList<>(allScores.subList(1, 7)); // Select top 7 scores + } else { + selectedScores = new ArrayList<>(allScores.subList(1, allScores.size())); // Select remaining scores + } + return selectedScores; + } else { + // Return an empty ArrayList if there are no elements or only one element + return new ArrayList<>(); + } + } +} diff --git a/app/src/main/java/com/example/animalmatchup/game/ScoreAnimation.java b/app/src/main/java/com/example/animalmatchup/game/ScoreAnimation.java new file mode 100644 index 0000000..c8df53c --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/game/ScoreAnimation.java @@ -0,0 +1,48 @@ +package com.example.animalmatchup.game; + +import android.os.Handler; +import android.view.animation.AlphaAnimation; +import android.widget.TextView; + +public class ScoreAnimation { + AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ); + AlphaAnimation fadeOut = new AlphaAnimation( 1.0f , 0.0f ); + + public void animationScore(final TextView textView, String score) { + textView.setText(score); + Runnable endAction; + final int slideDistance = textView.getHeight(); // Distance to slide up + + // Fade-in animation + textView.animate() + .alpha(1f) + .setDuration(200) + .setStartDelay(0) + .withEndAction(endAction = new Runnable() { + public void run() { + // Slide-up animation + textView.animate() + .alpha(0f) + .translationYBy(-slideDistance) + .setDuration(200) + .setStartDelay(200) + .withEndAction(new Runnable() { + public void run() { + textView.setTranslationY(0); // Reset translation + } + }); + } + }); + } + + public void delaySetText(TextView textView, String text){ + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + textView.setText(text); + } + }, 500); // Delay of 3000 milliseconds (3 seconds) + } + +} diff --git a/app/src/main/java/com/example/animalmatchup/game/ScoreDB.java b/app/src/main/java/com/example/animalmatchup/game/ScoreDB.java new file mode 100644 index 0000000..ba0edb5 --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/game/ScoreDB.java @@ -0,0 +1,93 @@ +package com.example.animalmatchup.game; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import com.example.animalmatchup.model.ScoreModel; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class ScoreDB extends SQLiteOpenHelper { + private static final String DATABASE_NAME = "ScoreDB"; + private static final int DATABASE_VERSION = 1; + + private static final String TABLE_SCORES = "scores"; + private static final String COLUMN_ID = "id"; + private static final String COLUMN_NAME = "name"; + private static final String COLUMN_SCORE = "score"; + + public ScoreDB(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + @Override + public void onCreate(SQLiteDatabase db) { + String createTable = "CREATE TABLE " + TABLE_SCORES + + "(" + + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + COLUMN_NAME + " TEXT, " + + COLUMN_SCORE + " TEXT" + + ")"; + db.execSQL(createTable); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + // Drop the table if it exists + db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORES); + onCreate(db); + } + + public void addScore(String name, String score) { + SQLiteDatabase db = getWritableDatabase(); + ContentValues values = new ContentValues(); + values.put(COLUMN_NAME, name); + values.put(COLUMN_SCORE, score); + db.insert(TABLE_SCORES, null, values); + db.close(); + } + + public ArrayList viewAllScores() { + ArrayList scores = new ArrayList<>(); + SQLiteDatabase db = getReadableDatabase(); + Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_SCORES, null); + + int idIndex = cursor.getColumnIndex(COLUMN_ID); + int nameIndex = cursor.getColumnIndex(COLUMN_NAME); + int scoreIndex = cursor.getColumnIndex(COLUMN_SCORE); + + while (cursor.moveToNext()) { + if (idIndex >= 0 && nameIndex >= 0 && scoreIndex >= 0) { + int id = cursor.getInt(idIndex); + String name = cursor.getString(nameIndex); + String score = cursor.getString(scoreIndex); + ScoreModel scoreModel = new ScoreModel(id, Integer.parseInt(score), name); + scores.add(scoreModel); + } + } + + cursor.close(); + db.close(); + + // Sort the scores in descending order based on the score value + // If the scores are the same, compare the IDs in descending order + Collections.sort(scores, new Comparator() { + @Override + public int compare(ScoreModel score1, ScoreModel score2) { + int scoreComparison = Integer.compare(score2.getScore(), score1.getScore()); + if (scoreComparison == 0) { + return Integer.compare(score2.getId(), score1.getId()); + } + return scoreComparison; + } + }); + + return scores; + } + +} diff --git a/app/src/main/java/com/example/animalmatchup/model/CardModel.java b/app/src/main/java/com/example/animalmatchup/model/CardModel.java index 1d0a8c9..4dc8b3a 100644 --- a/app/src/main/java/com/example/animalmatchup/model/CardModel.java +++ b/app/src/main/java/com/example/animalmatchup/model/CardModel.java @@ -5,12 +5,12 @@ public class CardModel { private String name; private int back_img; - private int img; + private int front_img; - public CardModel(String name, int back_img, int img) { + public CardModel(String name, int back_img, int front_img) { this.name = name; this.back_img = back_img; - this.img = img; + this.front_img = front_img; } public String getName() { @@ -29,11 +29,11 @@ public void setBack_img(int back_img) { this.back_img = back_img; } - public int getImg() { - return img; + public int getFront_img() { + return front_img; } - public void setImg(int img) { - this.img = img; + public void setFront_img(int img) { + this.front_img = img; } } diff --git a/app/src/main/java/com/example/animalmatchup/model/ScoreModel.java b/app/src/main/java/com/example/animalmatchup/model/ScoreModel.java new file mode 100644 index 0000000..b00c37c --- /dev/null +++ b/app/src/main/java/com/example/animalmatchup/model/ScoreModel.java @@ -0,0 +1,43 @@ +package com.example.animalmatchup.model; + +public class ScoreModel { + + private int id; + private int score; + private String name; + + public ScoreModel(int score, String name) { + this.score = score; + this.name = name; + } + + public ScoreModel(int id, int score, String name) { + this.id = id; + this.score = score; + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getScore() { + return score; + } + + public void setScore(int score) { + this.score = score; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/app/src/main/java/com/example/animalmatchup/play/CongratsScreen.java b/app/src/main/java/com/example/animalmatchup/play/CongratsScreen.java index 1d145a0..2b52c96 100644 --- a/app/src/main/java/com/example/animalmatchup/play/CongratsScreen.java +++ b/app/src/main/java/com/example/animalmatchup/play/CongratsScreen.java @@ -17,6 +17,7 @@ import com.example.animalmatchup.PlayScreen; import com.example.animalmatchup.R; +import com.example.animalmatchup.game.InfoBox; import com.example.animalmatchup.model.GameModel; public class CongratsScreen extends Fragment { @@ -24,6 +25,7 @@ public class CongratsScreen extends Fragment { TextView finalScore; GameModel gameModel; String fragment_round_num; + InfoBox infoBox; public CongratsScreen(GameModel gameModel, String fragment_round_num){ this.gameModel = gameModel; @@ -43,6 +45,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat nextLevelBtn = view.findViewById(R.id.next_level_btn); infoBtn = view.findViewById(R.id.info_btn); finalScore = view.findViewById(R.id.final_score); + infoBox = new InfoBox(); backBtn.setOnClickListener(new View.OnClickListener() { @Override @@ -54,7 +57,7 @@ public void onClick(View v) { infoBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - infoBox(); + infoBox.infoBox(getContext()); } }); @@ -79,28 +82,4 @@ public void onClick(View v) { }); } } - - private void infoBox(){ - // Create the object of AlertDialog Builder class - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - - // Set the message show for the Alert time - builder.setMessage("A Task Performance in Mobile App that aims to develop a simple mobile game like Memory Game"); - - // Set Alert Title - builder.setTitle("Animal Match Up"); - - // Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain show - builder.setCancelable(false); - - // Set the positive button with yes name Lambda OnClickListener method is use of DialogInterface interface. - builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> { - // When the user click yes button then app will close - }); - - // Create the Alert dialog - AlertDialog alertDialog = builder.create(); - // Show the Alert Dialog box - alertDialog.show(); - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/animalmatchup/play/RoundOne.java b/app/src/main/java/com/example/animalmatchup/play/RoundOne.java index ddb3934..87a4f40 100644 --- a/app/src/main/java/com/example/animalmatchup/play/RoundOne.java +++ b/app/src/main/java/com/example/animalmatchup/play/RoundOne.java @@ -1,11 +1,9 @@ package com.example.animalmatchup.play; -import android.content.DialogInterface; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -17,16 +15,18 @@ import android.widget.TextView; import com.example.animalmatchup.R; -import com.example.animalmatchup.adapter.CardAdapter; +import com.example.animalmatchup.adapter.gameadapter.CardAdapter; +import com.example.animalmatchup.game.InfoBox; import com.example.animalmatchup.model.GameModel; -import com.example.animalmatchup.model.PopulateCard; +import com.example.animalmatchup.game.PopulateCard; public class RoundOne extends Fragment { RecyclerView recyclerView; GameModel gameModel; - TextView gameScore; + TextView gameScore, animScore; ImageView backBtn, infoBtn; + InfoBox infoBox; public RoundOne(GameModel gameModel){ this.gameModel = gameModel; @@ -44,10 +44,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat recyclerView = view.findViewById(R.id.animals_card); infoBtn = view.findViewById(R.id.info_btn); gameScore = view.findViewById(R.id.game_score); + animScore = view.findViewById(R.id.anim_score); backBtn = view.findViewById(R.id.back_btn); + infoBox = new InfoBox(); recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3)); PopulateCard populateCard = new PopulateCard(); - CardAdapter cardAdapter = new CardAdapter(populateCard.populateCard(), getContext(), gameModel, gameScore, populateCard.getTotalAnimals(), getFragmentManager(), "Round 1"); + CardAdapter cardAdapter = new CardAdapter(populateCard.populateCard(), getContext(), gameModel, gameScore, animScore, populateCard.getTotalAnimals(), getFragmentManager(), "Round 1"); recyclerView.setAdapter(cardAdapter); backBtn.setOnClickListener(new View.OnClickListener() { @@ -60,32 +62,10 @@ public void onClick(View v) { infoBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - infoBox(); + infoBox.infoBox(getContext()); } }); } - private void infoBox(){ - // Create the object of AlertDialog Builder class - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - // Set the message show for the Alert time - builder.setMessage("A Task Performance in Mobile App that aims to develop a simple mobile game like Memory Game"); - - // Set Alert Title - builder.setTitle("Animal Match Up"); - - // Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain show - builder.setCancelable(false); - - // Set the positive button with yes name Lambda OnClickListener method is use of DialogInterface interface. - builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> { - // When the user click yes button then app will close - }); - - // Create the Alert dialog - AlertDialog alertDialog = builder.create(); - // Show the Alert Dialog box - alertDialog.show(); - } } \ No newline at end of file diff --git a/app/src/main/java/com/example/animalmatchup/play/RoundTwo.java b/app/src/main/java/com/example/animalmatchup/play/RoundTwo.java index fca4921..4300bd8 100644 --- a/app/src/main/java/com/example/animalmatchup/play/RoundTwo.java +++ b/app/src/main/java/com/example/animalmatchup/play/RoundTwo.java @@ -1,11 +1,9 @@ package com.example.animalmatchup.play; -import android.content.DialogInterface; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -17,16 +15,18 @@ import android.widget.TextView; import com.example.animalmatchup.R; -import com.example.animalmatchup.adapter.CardAdapter; +import com.example.animalmatchup.adapter.gameadapter.CardAdapter; +import com.example.animalmatchup.game.InfoBox; import com.example.animalmatchup.model.GameModel; -import com.example.animalmatchup.model.PopulateCard; +import com.example.animalmatchup.game.PopulateCard; public class RoundTwo extends Fragment { RecyclerView recyclerView; GameModel gameModel; - TextView gameScore; + TextView gameScore, animScore; ImageView backBtn, infoBtn; + InfoBox infoBox; public RoundTwo(GameModel gameModel){ this.gameModel = gameModel; @@ -46,9 +46,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat gameScore = view.findViewById(R.id.game_score); infoBtn = view.findViewById(R.id.info_btn); backBtn = view.findViewById(R.id.back_btn); + animScore = view.findViewById(R.id.anim_score); + infoBox = new InfoBox(); recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3)); PopulateCard populateCard = new PopulateCard(); - CardAdapter cardAdapter = new CardAdapter(populateCard.populateCard(), getContext(), gameModel, gameScore, populateCard.getTotalAnimals(), getFragmentManager(), "Round 2"); + CardAdapter cardAdapter = new CardAdapter(populateCard.populateCard(), getContext(), gameModel, gameScore, animScore, populateCard.getTotalAnimals(), getFragmentManager(), "Round 2"); recyclerView.setAdapter(cardAdapter); gameScore.setText(String.valueOf(gameModel.getScore())); @@ -62,32 +64,8 @@ public void onClick(View v) { infoBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - infoBox(); + infoBox.infoBox(getContext()); } }); } - - private void infoBox(){ - // Create the object of AlertDialog Builder class - AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); - - // Set the message show for the Alert time - builder.setMessage("A Task Performance in Mobile App that aims to develop a simple mobile game like Memory Game"); - - // Set Alert Title - builder.setTitle("Animal Match Up"); - - // Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain show - builder.setCancelable(false); - - // Set the positive button with yes name Lambda OnClickListener method is use of DialogInterface interface. - builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> { - // When the user click yes button then app will close - }); - - // Create the Alert dialog - AlertDialog alertDialog = builder.create(); - // Show the Alert Dialog box - alertDialog.show(); - } } \ No newline at end of file diff --git a/app/src/main/res/drawable-xxhdpi/high_score_background.png b/app/src/main/res/drawable-xxhdpi/high_score_background.png new file mode 100644 index 0000000..e5fbb64 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/high_score_background.png differ diff --git a/app/src/main/res/drawable-xxhdpi/high_score_txt.png b/app/src/main/res/drawable-xxhdpi/high_score_txt.png new file mode 100644 index 0000000..f982a69 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/high_score_txt.png differ diff --git a/app/src/main/res/drawable-xxhdpi/trophy_button.png b/app/src/main/res/drawable-xxhdpi/trophy_button.png new file mode 100644 index 0000000..a8c3dd3 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/trophy_button.png differ diff --git a/app/src/main/res/layout/activity_high_score_screen.xml b/app/src/main/res/layout/activity_high_score_screen.xml new file mode 100644 index 0000000..7aacf44 --- /dev/null +++ b/app/src/main/res/layout/activity_high_score_screen.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_layout.xml b/app/src/main/res/layout/dialog_layout.xml new file mode 100644 index 0000000..c2594a4 --- /dev/null +++ b/app/src/main/res/layout/dialog_layout.xml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + +