From 431d607e71775e27fe1668552cdd117a41fe0680 Mon Sep 17 00:00:00 2001 From: Brian Pila Date: Wed, 10 Apr 2024 06:36:54 +0800 Subject: [PATCH] Wrote Match3 in one shot HAHAHAHA --- .idea/vcs.xml | 6 + app/src/main/AndroidManifest.xml | 3 + .../MainActivity.java | 12 +- .../Match3Exercise.java | 670 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 14 + .../res/layout/activity_match3_exercise.xml | 250 +++++++ 6 files changed, 954 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/example/androidprojectcollection/Match3Exercise.java create mode 100644 app/src/main/res/layout/activity_match3_exercise.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b3a5792..3168e14 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,9 @@ android:supportsRtl="true" android:theme="@style/Theme.AndroidProjectCollection" tools:targetApi="31"> + diff --git a/app/src/main/java/com/example/androidprojectcollection/MainActivity.java b/app/src/main/java/com/example/androidprojectcollection/MainActivity.java index 222d7da..0c6a368 100644 --- a/app/src/main/java/com/example/androidprojectcollection/MainActivity.java +++ b/app/src/main/java/com/example/androidprojectcollection/MainActivity.java @@ -13,7 +13,7 @@ import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity { - Button btn1,btn2,btn3; + Button btn1,btn2,btn3,btn4; @Override protected void onCreate(Bundle savedInstanceState) { @@ -50,6 +50,16 @@ public class MainActivity extends AppCompatActivity { startActivity(intent1); } }); + btn4 = findViewById(R.id.btnCM3); + + btn4.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent startCM3 = new Intent(MainActivity.this, Match3Exercise.class); + startActivity(startCM3); + } + }); + } diff --git a/app/src/main/java/com/example/androidprojectcollection/Match3Exercise.java b/app/src/main/java/com/example/androidprojectcollection/Match3Exercise.java new file mode 100644 index 0000000..d84caab --- /dev/null +++ b/app/src/main/java/com/example/androidprojectcollection/Match3Exercise.java @@ -0,0 +1,670 @@ +package com.example.androidprojectcollection; + +import android.graphics.Color; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +import org.w3c.dom.Text; + +import java.util.Random; + +public class Match3Exercise extends AppCompatActivity { + Button[] btns = new Button[25]; + ButtonProperties[] props = new ButtonProperties[25]; + TextView tv; + + int clicks = 0,score = 0; + + int[] held_tile = new int[2]; + + Button reset; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_match3_exercise); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + reset = findViewById(R.id.BtnReset); + tv = findViewById(R.id.ScoreBoard); + + props[0] = new ButtonProperties(); + props[1] = new ButtonProperties(); + props[2] = new ButtonProperties(); + props[3] = new ButtonProperties(); + props[4] = new ButtonProperties(); + props[5] = new ButtonProperties(); + props[6] = new ButtonProperties(); + props[7] = new ButtonProperties(); + props[8] = new ButtonProperties(); + props[9] = new ButtonProperties(); + props[10] = new ButtonProperties(); + props[11] = new ButtonProperties(); + props[12] = new ButtonProperties(); + props[13] = new ButtonProperties(); + props[14] = new ButtonProperties(); + props[15] = new ButtonProperties(); + props[16] = new ButtonProperties(); + props[17] = new ButtonProperties(); + props[18] = new ButtonProperties(); + props[19] = new ButtonProperties(); + props[20] = new ButtonProperties(); + props[21] = new ButtonProperties(); + props[22] = new ButtonProperties(); + props[23] = new ButtonProperties(); + props[24] = new ButtonProperties(); + + + + //First Row + btns[0] = findViewById(R.id.btn0); + //props[0] = new ButtonProperties(true ,false,true,false); + btns[1] = findViewById(R.id.btn1); + //props[1] = new ButtonProperties(true ,false,false,false); + btns[2] = findViewById(R.id.btn2); + //props[2] = new ButtonProperties(true ,false,false,false); + btns[3] = findViewById(R.id.btn3); + //props[3] = new ButtonProperties(true ,false,false,false); + btns[4] = findViewById(R.id.btn4); + //props[4] = new ButtonProperties(true ,false,false,true); + + //Second Row + btns[5] = findViewById(R.id.btn5); + //props[5] = new ButtonProperties(false ,false,true,false); + btns[6] = findViewById(R.id.btn6); + //props[6] = new ButtonProperties(false ,false,false,false); + btns[7] = findViewById(R.id.btn7); + //props[7] = new ButtonProperties(false ,false,false,false); + btns[8] = findViewById(R.id.btn8); + //props[8] = new ButtonProperties(false ,false,false,false); + btns[9] = findViewById(R.id.btn9); + //props[9] = new ButtonProperties(false ,false,false,true); + + //Third Row + btns[10] = findViewById(R.id.btn10); + //props[10] = new ButtonProperties(false ,false,true,false); + btns[11] = findViewById(R.id.btn11); + //props[11] = new ButtonProperties(true ,false,false,false); + btns[12] = findViewById(R.id.btn12); + //props[12] = new ButtonProperties(false ,false,false,false); + btns[13] = findViewById(R.id.btn13); + //props[13] = new ButtonProperties(false ,false,false,false); + btns[14] = findViewById(R.id.btn14); + //props[14] = new ButtonProperties(false,false,false,true); + + //Fourth Row + btns[15] = findViewById(R.id.btn15); + //props[15] = new ButtonProperties(false ,false,true,false); + btns[16] = findViewById(R.id.btn16); + //props[16] = new ButtonProperties(false ,false,false,false); + btns[17] = findViewById(R.id.btn17); + //props[17] = new ButtonProperties(false ,false,false,false); + btns[18] = findViewById(R.id.btn18); + //props[18] = new ButtonProperties(false ,false,false,false); + btns[19] = findViewById(R.id.btn19); + //props[19] = new ButtonProperties(false ,false,false,true); + + //Fifth Row + btns[20] = findViewById(R.id.btn20); + //props[20] = new ButtonProperties(false ,true,true,false); + btns[21] = findViewById(R.id.btn21); + //props[21] = new ButtonProperties(false ,true,false,false); + btns[22] = findViewById(R.id.btn22); + //props[22] = new ButtonProperties(false ,true,false,false); + btns[23] = findViewById(R.id.btn23); + //props[23] = new ButtonProperties(false ,true,false,false); + btns[24] = findViewById(R.id.btn24); + //props[24] = new ButtonProperties(false ,true,false,true); + buttonLabeler(); + + btns[0].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(0); + } + }); + btns[1].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(1); + } + }); + + btns[2].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(2); + } + }); + + btns[3].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(3); + } + }); + + btns[4].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(4); + } + }); + + btns[5].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(5); + } + }); + + + btns[6].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(6); + } + }); + + btns[7].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(7); + } + }); + + btns[8].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(8); + } + }); + + btns[9].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(9); + } + }); + + btns[10].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(10); + } + }); + + btns[11].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(11); + } + }); + + btns[12].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(12); + } + }); + + btns[13].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(13); + } + }); + + btns[14].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(14); + } + }); + + btns[15].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(15); + } + }); + + btns[16].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(16); + } + }); + + btns[17].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(17); + } + }); + + btns[18].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(18); + } + }); + + btns[19].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(19); + } + }); + + btns[20].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(20); + } + }); + + btns[21].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(21); + } + }); + + btns[22].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(22); + } + }); + + btns[23].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(23); + } + }); + + btns[24].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + stuffer(24); + } + }); + + + + reset.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + buttonLabeler(); + } + }); + + } + + + + void buttonLabeler(){ + Random rd = new Random(); + for (int i = 0 ; i < 25 ; i++){ + switch (rd.nextInt(4) + 1){ + case 1:{ + btns[i].setBackgroundColor(Color.RED); + props[i].color = 1; + break; + }case 2:{ + btns[i].setBackgroundColor(Color.BLUE); + props[i].color = 2; + break; + } case 3:{ + btns[i].setBackgroundColor(Color.GREEN); + props[i].color = 3; + break; + } case 4:{ + btns[i].setBackgroundColor(Color.YELLOW); + props[i].color = 4; + break; + }default:{ + btns[i].setBackgroundColor(Color.BLACK); + props[i].color = 5; + } + } + } + } + + void stuffer(int index){ + if(clicks < 1){ + held_tile[clicks] = index; + clicks++; + }else { + held_tile[clicks] = index ; + clicks++; + swapper(held_tile[0],held_tile[1]); + clicks = 0; + } + } + + void swapper(int wan, int tuo){ + if (validity(wan, tuo)) { + int swap = props[wan].color; + props[wan].color = props[tuo].color; + props[tuo].color = swap; + colorer(wan); + colorer(tuo); + for (int i = 0 ;i < 25 ; i++){ + matchCheck(i); + } + tv.setText("Score: " + score); + } + } + + boolean validity(int wan, int tuo){ + if (wan == (tuo-1) || wan == (tuo+1) || wan == (tuo-5) || wan == (tuo+5)){ + return props[wan].color != props[tuo].color; + }else { + return false; + } + } + + + void colorer(int index){ + + switch (props[index].color){ + case 1:{ + btns[index].setBackgroundColor(Color.RED); + break; + }case 2:{ + btns[index].setBackgroundColor(Color.BLUE); + break; + } case 3:{ + btns[index].setBackgroundColor(Color.GREEN); + break; + } case 4:{ + btns[index].setBackgroundColor(Color.YELLOW); + break; + }default:{ + btns[index].setBackgroundColor(Color.BLACK); + } + } + + } + + + + + void matchCheck(int index) { + int row = index / 5; // Calculate the row of the button + int col = index % 5; // Calculate the column of the button + int color = props[index].color; // Get the color of the current button + + // Check for horizontal matches + if (col <= 2 && props[index + 1].color == color && props[index + 2].color == color) { + // Match found, update colors + props[index].color = getRandomColor(); + props[index + 1].color = getRandomColor(); + props[index + 2].color = getRandomColor(); + colorer(index); + colorer(index + 1); + colorer(index + 2); + score++; + } + + // Check for vertical matches + if (row <= 2 && props[index + 5].color == color && props[index + 10].color == color) { + // Match found, update colors + props[index].color = getRandomColor(); + props[index + 5].color = getRandomColor(); + props[index + 10].color = getRandomColor(); + colorer(index); + colorer(index + 5); + colorer(index + 10); + score++; + } + + // Check for matches in upward direction + if (row >= 2 && props[index - 5].color == color && props[index - 10].color == color) { + // Match found, update colors + props[index].color = getRandomColor(); + props[index - 5].color = getRandomColor(); + props[index - 10].color = getRandomColor(); + colorer(index); + colorer(index - 5); + colorer(index - 10); + score++; + } + } + + int getRandomColor() { + Random rd = new Random(); + return rd.nextInt(4) + 1; + } + + + + + + +} + + +class ButtonProperties{ +/* boolean isTopMost; + boolean isBottomMost; + boolean isLeftMost; + boolean isRightMost;*/ + int color; +/* + public ButtonProperties(boolean isTopMost, boolean isBottomMost, boolean isLeftMost, boolean isRightMost) { + this.isTopMost = isTopMost; + this.isBottomMost = isBottomMost; + this.isLeftMost = isLeftMost; + this.isRightMost = isRightMost; + } + +*/ + +} + + + + + + + + + + + + + + +/*basement code*/ + /* int matchCheck(int index){ + Random rd = new Random(); + switch (props[index].spewer()){ + case 1:{ //Topmost + if(props[index].color == props[index-1].color && props[index].color == props[index+1].color){ + props[index].color = rd.nextInt(4)+1; + props[index+1].color = rd.nextInt(4)+1; + props[index-1].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+1); + colorer(index-1); + }else if (props[index].color == props[index+5].color && props[index].color == props[index+10].color){ + props[index].color = rd.nextInt(4)+1; + props[index+5].color = rd.nextInt(4)+1; + props[index+10].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+5); + colorer(index+10); + }else { + return 0; + } + score++; + break; + } case 2:{ //BottomMost + if(props[index].color == props[index-1].color && props[index].color == props[index+1].color){ + props[index].color = rd.nextInt(4)+1; + props[index+1].color = rd.nextInt(4)+1; + props[index-1].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+1); + colorer(index-1); + }else if (props[index].color == props[index-5].color && props[index].color == props[index-10].color){ + props[index].color = rd.nextInt(4)+1; + props[index-5].color = rd.nextInt(4)+1; + props[index-10].color = rd.nextInt(4)+1; + colorer(index); + colorer(index-5); + colorer(index-10); + }else { + return 0; + } + score++; + break; + } case 3:{//LeftMost + if(props[index].color == props[index+1].color && props[index].color == props[index+2].color){ + props[index].color = rd.nextInt(4)+1; + props[index+1].color = rd.nextInt(4)+1; + props[index+2].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+1); + colorer(index+2); + }else if (props[index].color == props[index+5].color && props[index].color == props[index-5].color){ + props[index].color = rd.nextInt(4)+1; + props[index+5].color = rd.nextInt(4)+1; + props[index-5].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+5); + colorer(index-5); + }else { + return 0; + } + score++; + break; + } case 4:{//RightMost + + if(props[index].color == props[index-1].color && props[index].color == props[index-2].color){ + props[index].color = rd.nextInt(4)+1; + props[index-1].color = rd.nextInt(4)+1; + props[index-2].color = rd.nextInt(4)+1; + colorer(index); + colorer(index-1); + colorer(index-2); + }else if (props[index].color == props[index+5].color && props[index].color == props[index-5].color){ + props[index].color = rd.nextInt(4)+1; + props[index+5].color = rd.nextInt(4)+1; + props[index-5].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+5); + colorer(index-5); + }else { + return 0; + } + score++; + break; + } case 5:{//TopLeftMost + if(props[index].color == props[index+1].color && props[index].color == props[index+2].color){ + props[index].color = rd.nextInt(4)+1; + props[index+1].color = rd.nextInt(4)+1; + props[index+2].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+1); + colorer(index+2); + }else if (props[index].color == props[index+5].color && props[index].color == props[index+10].color){ + props[index].color = rd.nextInt(4)+1; + props[index+5].color = rd.nextInt(4)+1; + props[index+10].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+5); + colorer(index+10); + }else { + return 0; + } + score++; + break; + + } case 6:{//TopRightMost + if(props[index].color == props[index-1].color && props[index].color == props[index-2].color){ + props[index].color = rd.nextInt(4)+1; + props[index-1].color = rd.nextInt(4)+1; + props[index-2].color = rd.nextInt(4)+1; + colorer(index); + colorer(index-1); + colorer(index-2); + }else if (props[index].color == props[index+5].color && props[index].color == props[index+10].color){ + props[index].color = rd.nextInt(4)+1; + props[index+5].color = rd.nextInt(4)+1; + props[index+10].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+5); + colorer(index+10); + }else { + return 0; + } + score++; + break; + + } case 7:{//BottomLeftMost + if(props[index].color == props[index+1].color && props[index].color == props[index+2].color){ + props[index].color = rd.nextInt(4)+1; + props[index+1].color = rd.nextInt(4)+1; + props[index+2].color = rd.nextInt(4)+1; + colorer(index); + colorer(index+1); + colorer(index+2); + }else if (props[index].color == props[index-5].color && props[index].color == props[index-10].color){ + props[index].color = rd.nextInt(4)+1; + props[index-5].color = rd.nextInt(4)+1; + props[index-10].color = rd.nextInt(4)+1; + colorer(index); + colorer(index-5); + colorer(index-10); + }else { + return 0; + } + score++; + break; + + } case 8:{//BottomRightMost + if(props[index].color == props[index-1].color && props[index].color == props[index-2].color){ + props[index].color = rd.nextInt(4)+1; + props[index-1].color = rd.nextInt(4)+1; + props[index-2].color = rd.nextInt(4)+1; + colorer(index); + colorer(index-1); + colorer(index-2); + }else if (props[index].color == props[index-5].color && props[index].color == props[index-10].color){ + props[index].color = rd.nextInt(4)+1; + props[index-5].color = rd.nextInt(4)+1; + props[index-10].color = rd.nextInt(4)+1; + colorer(index); + colorer(index-5); + colorer(index-10); + }else { + return 0; + } + score++; + break; + } default:{ + System.exit(69); + + } + } + return 1; + + }*/ \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9e91f0c..670efb5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -64,4 +64,18 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/Calculaotr" /> +