0% found this document useful (0 votes)
4 views4 pages

Memory Card Game Implementation

The document is a Java class for an Android application that implements a memory card game. It initializes a grid of image views representing cards, randomizes their arrangement, and handles user interactions to flip cards and check for matches. The game progresses until all pairs are cleared, at which point it navigates to a finish activity.

Uploaded by

Realyn Babagay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Memory Card Game Implementation

The document is a Java class for an Android application that implements a memory card game. It initializes a grid of image views representing cards, randomizes their arrangement, and handles user interactions to flip cards and check for matches. The game progresses until all pairs are cleared, at which point it navigates to a finish activity.

Uploaded by

Realyn Babagay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link].

pairs;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


ImageView[] img = new ImageView[13];;
Integer[] Array;
String test = "facedown";
int count = 0, cleared = 0, num1 = 100, num2 = 100, holder = 0;
ArrayList<Integer> arrangements = new ArrayList<Integer>();
boolean sameCard = false, flipcard = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
randomize();
for (int i = 0; i < 13; i++){
img[i] = new ImageView(this);
img[i].setImageResource([Link]);
}
adjustSize();
adjustLocation();
actionPerformed();
}
public void adjustSize(){
ConstraintLayout cl = new ConstraintLayout(this);
for(int i = 0; i< 12; i++) {
img[i].setAdjustViewBounds(true);
img[i].setLayoutParams(new [Link](340, 450));
[Link](img[i]);
}
setContentView(cl);
}
public void adjustLocation(){
img[0].setX(0);
img[1].setX(370);
img[2].setX(740);
img[3].setX(0);
img[3].setY(500);
img[4].setX(370);
img[4].setY(500);
img[5].setX(740);
img[5].setY(500);
img[6].setY(1000);
img[7].setX(370);
img[7].setY(1000);
img[8].setX(740);
img[8].setY(1000);
img[9].setY(1500);
img[10].setX(370);
img[10].setY(1500);
img[11].setX(740);
img[11].setY(1500);
}
public void randomize(){
Random r = new Random();
int num = [Link](12);
while([Link]() != 12) {
if ([Link](num)) {
randomize();
} else {
[Link](num);
}
}
Array = [Link](new Integer[[Link]()]);
}
public void actionPerformed(){
for(int i = 0; i < 12; i++ ){
int finalI = i;
img[i].setOnClickListener(new [Link]() {
@Override
public void onClick(View v) {
if (count != 2) {
if (Array[finalI] == 0 || Array[finalI] == 1) {
img[finalI].setImageResource([Link]);
if(holder == 1){
num2 = finalI;
checker();
}
else if(flipcard){
num2 = finalI;
}
else {
num1 = finalI;
flipcard=true;
}
holder = 1;
} else if (Array[finalI] == 2 || Array[finalI] == 3)
{

img[finalI].setImageResource([Link]);
if(holder == 2){
num2 = finalI;
checker();
}
else if(flipcard){
num2 = finalI;
}
else {
num1 = finalI;
flipcard=true;
}
holder = 2;
}
else if (Array[finalI] == 4 || Array[finalI] == 5) {
img[finalI].setImageResource([Link]);
if(holder == 3){
num2 = finalI;
checker();
}
else if(flipcard){
num2 = finalI;
}
else {
num1 = finalI;
flipcard=true;
}
holder = 3;
}
else if (Array[finalI] == 6 || Array[finalI] == 7) {
img[finalI].setImageResource([Link]);
if(holder == 4){
num2 = finalI;
checker();
}
else if(flipcard){
num2 = finalI;
}
else {
num1 = finalI;
flipcard=true;
}
holder = 4;
}
else if (Array[finalI] == 8 || Array[finalI] == 9) {
img[finalI].setImageResource([Link]);
if(holder == 5){
num2 = finalI;
checker();
}
else if(flipcard){
num2 = finalI;
}
else {
num1 = finalI;
flipcard=true;
}
holder = 5;
}
else {
img[finalI].setImageResource([Link]);
if(holder == 6){
num2 = finalI;
checker();
}
else if(flipcard){
num2 = finalI;
}
else {
num1 = finalI;
flipcard=true;
}
holder = 6;
}
count++;
}
counter();
}
});
}
}

public void checker(){


sameCard = true;
num1=12;
num2=12;
cleared++;
cleared();
}
public void counter(){
Handler handler = new Handler();
[Link](new Runnable() {
public void run() {
if(count==2 && sameCard==false) {
img[num1].setImageResource([Link]);
img[num2].setImageResource([Link]);
count = 0;
num1 = 0;
num2 = 0;
holder = 0;
flipcard=false;
}
sameCard=false;
}
}, 2000);
}
public void cleared(){
if(cleared == 6){
Intent intent = new Intent(this, [Link]);
startActivity(intent);
}
}
}

You might also like