728x90
반응형
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
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - 액티비티 간의 상호작용 (0) | 2022.08.09 |
---|---|
Android Studio - 암시적 인텐트 (0) | 2022.08.09 |
Android Studio - 명시적 인텐트 (0) | 2022.08.08 |
Android Studio - 액티비티 수명주기와 재실행 (0) | 2022.08.08 |
Android Studio - 리스트뷰 아이템에 아이콘 그리기 (0) | 2022.08.05 |
댓글