본문 바로가기
App/Android Studio

8강 이상형 월드컵 📌수정중

by 코젼 2022. 8. 8.
728x90
반응형

 

img1.png
0.00MB
img2.png
0.00MB
img3.png
0.00MB
img4.png
0.00MB
img5.png
0.00MB
img6.png
0.00MB
img7.png
0.00MB
img8.png
0.00MB

package com.example.worldcup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<Integer> al;
    ArrayList<Integer> al2;
    ArrayList<Integer> al3;
    ImageView iv_left;
    ImageView iv_right;
    TextView tv;
    static int num = 0;
    static int round = 1;
//    int img[] = {R.drawable.gum1, R.drawable.gum2,R.drawable.gum3, R.drawable.gum4,
//            R.drawable.gum5, R.drawable.gum6, R.drawable.gum7, R.drawable.gum8};

    int img[] = {R.drawable.img1, R.drawable.img2,R.drawable.img3, R.drawable.img4,
            R.drawable.img5, R.drawable.img6, R.drawable.img7,R.drawable.img8};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);

        al = new ArrayList<>();
        al2 = new ArrayList<>();
        al3 = new ArrayList<>();
        tv = findViewById(R.id.tv_round);

        iv_left = findViewById(R.id.iv_left);
        iv_right = findViewById(R.id.iv_right);

        for(int i=0; i<img.length*3; i++) {
            int temp1, temp2;
            temp1 = (int)(Math.random()*img.length);
            temp2 = (int)(Math.random()*img.length);

            int temp = img[temp1];
            img[temp1] = img[temp2];
            img[temp2] = temp;
        }

        iv_left.setBackgroundResource(img[num]);
        iv_right.setBackgroundResource(img[num+1]);

        iv_left.setOnClickListener(lis);
        iv_right.setOnClickListener(lis);


    }

    View.OnClickListener lis = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();

            if (round >= 1 && round <= 4) {

                if(round == 4) {
                    num = 0;
                }

                num += 2;
                if(id == iv_left.getId()) {
                    al.add(img[num]);


                    iv_left.setBackgroundResource(img[num]);
                    iv_right.setBackgroundResource(img[num+1]);

                    round++;
                }
                else if(id == iv_right.getId()) {
                    al.add(img[num+1]);

                    iv_left.setBackgroundResource(img[num]);
                    iv_right.setBackgroundResource(img[num+1]);

                    round++;
                }

            }

            if(round >= 5 && round <= 6) {
                tv.setText("4강");

                if(round == 6) {
                    num = 0;
                }

                if(id == iv_left.getId()) {
                    al2.add(al.get(num));

                    iv_left.setBackgroundResource(al.get(num));
                    iv_right.setBackgroundResource(al.get(num+1));

                    round++;
                    num -= 2;
                }
                else if(id == iv_right.getId()) {
                    al2.add(al.get(num+1));

                    iv_left.setBackgroundResource(al.get(num));
                    iv_right.setBackgroundResource(al.get(num+1));

                    round++;
                    num -= 2;
                }
            }

            if(round == 8) {
                tv.setText("2강");

                if(id == iv_left.getId()) {
                    iv_left.setBackgroundResource(al2.get(0));
                    iv_right.setBackgroundResource(0);
                    tv.setText("결과!");
                }
                else if(id == iv_right.getId()) {
                    iv_left.setBackgroundResource(0);
                    iv_right.setBackgroundResource(al2.get(1));
                    tv.setText("결과!");
                }
            }
        }
    };
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="검둥이 월드컵"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:orientation="horizontal"
        android:id="@+id/ll">

        <ImageView
            android:layout_width="150dp"
            android:layout_height="240dp"
            android:id="@+id/iv_left"
            android:layout_marginRight="10dp"
            android:layout_weight="1"/>

        <ImageView
            android:layout_width="150dp"
            android:layout_height="240dp"
            android:layout_weight="1"
            android:id="@+id/iv_right"/>

    </LinearLayout>


    <TextView
        android:id="@+id/tv_round"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="8강"
        android:textSize="30sp" />

</LinearLayout>
728x90
반응형

댓글