본문 바로가기
코리아 IT아카데미/android

6_1일차 | 로또 번호 생성기

by Sharon kim 2022. 2. 22.

left : 개인 디자인 해보기 (7개ㅋ) / right : 클론 코딩

activity_lotto.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <NumberPicker
        android:id="@+id/numberPicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/add_number"
        app:layout_constraintEnd_toStartOf="@id/initButton"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/numberPicker" />

    <Button
        android:id="@+id/initButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/init_number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/addButton"
        app:layout_constraintTop_toTopOf="@id/addButton" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/addButton"
        tools:background="@color/black">

        <TextView
            android:id="@+id/textView1"
            style="@style/style_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:background="@drawable/round_background1"
            android:gravity="center"
            android:visibility="invisible"
            tools:text="1"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/textView2"
            style="@style/style_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:background="@drawable/round_background1"
            android:gravity="center"
            android:visibility="invisible"
            tools:text="1"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/textView3"
            style="@style/style_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:background="@drawable/round_background1"
            android:gravity="center"
            android:visibility="invisible"
            tools:text="1"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/textView4"
            style="@style/style_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:background="@drawable/round_background1"
            android:gravity="center"
            android:visibility="invisible"
            tools:text="1"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/textView5"
            style="@style/style_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:background="@drawable/round_background1"
            android:gravity="center"
            android:visibility="invisible"
            tools:text="1"
            tools:visibility="visible" />

        <TextView
            android:id="@+id/textView6"
            style="@style/style_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/round_background1"
            android:gravity="center"
            android:visibility="invisible"
            tools:text="1"
            tools:visibility="visible" />

    </LinearLayout>

    <Button
        android:id="@+id/runButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="게임실행하기"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

round_background1.xml


MainActivity.java

package com.example.myapp6_1;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private NumberPicker numberPicker;
    private Button addButton;
    private Button initButton;
    private Button runButton;
    private Set<Integer> pickerNumberSet = new HashSet<>();
    private ArrayList<TextView> textViews = new ArrayList<>();
    private boolean didRun = false;

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

    private void initData() {
        numberPicker = findViewById(R.id.numberPicker);
        numberPicker.setMinValue(1);
        numberPicker.setMaxValue(45);

        addButton = findViewById(R.id.addButton);
        initButton = findViewById(R.id.initButton);
        runButton = findViewById(R.id.runButton);

        textViews.add(findViewById(R.id.textView1));
        textViews.add(findViewById(R.id.textView2));
        textViews.add(findViewById(R.id.textView3));
        textViews.add(findViewById(R.id.textView4));
        textViews.add(findViewById(R.id.textView5));
        textViews.add(findViewById(R.id.textView6));
    }

    private void addEventListener() {
        addButton.setOnClickListener(v -> {

            int selectedNumber = numberPicker.getValue();
            if (didRun) {
                Toast.makeText(this, "초기화 후에 시도해 주세요",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            if (pickerNumberSet.size() >= 5) {
                Toast.makeText(this, "번호는 5개만 선택 가능",
                        Toast.LENGTH_SHORT).show();
                return;
            }
            if (pickerNumberSet.contains(selectedNumber)) {
                Toast.makeText(this, "이미 선택한 번호입니다.",
                        Toast.LENGTH_SHORT).show();
                return;
            }

            TextView textView = textViews.get(pickerNumberSet.size());
            textView.setVisibility(View.VISIBLE);
            textView.setText(String.valueOf(selectedNumber));
            textView.setBackground(setTextViewBackground(selectedNumber));

            pickerNumberSet.add(selectedNumber);
            //Log.d("TAG", numberPicker.getValue() + "");
//            if (pickerNumberSet.size() <= 5) {
//                pickerNumberSet.add(numberPicker.getValue());
//            }
        });
        initButton.setOnClickListener(v -> {
            didRun = false;
            pickerNumberSet.clear();

            for (TextView tv : textViews) {
                tv.setVisibility(View.GONE);
            }

        });
        runButton.setOnClickListener(v -> {
            if (didRun) {
                Toast.makeText(this, "초기화 후에 시도해 주세요",
                        Toast.LENGTH_SHORT).show();
                return;
            }
//            getRandomNumberTest();
            List<Integer> list = getRandomNumber();
            list.addAll(pickerNumberSet);
            // 코드 결과 상태 (6개 난수가 저장 됨) --> ( list )
            Collections.sort(list);
            Log.d("TAG", list.toString());

            for (int i = 0; i < list.size(); i++) {
                textViews.get(i).setText(String.valueOf(list.get(i)));
                textViews.get(i).setVisibility(View.VISIBLE);
                textViews.get(i).setBackground(setTextViewBackground(list.get(i)));
            }
            didRun = true;
        });
    }

    private void getRandomNumberTest() {
        // 랜던 숫자를 만들어주는 클래스를 가져온다.
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        while (list.size() < 6) {
            int number = random.nextInt(45) + 1;
            if (list.contains(number)) {
                continue;
            }
            list.add(number);
        }
        Collections.sort(list);
        Log.d("TAG", list.toString());


    }

    private List<Integer> getRandomNumber() {
        ArrayList<Integer> numberList = new ArrayList<>();

        // 사용자 선택한 번호 확인
        Log.d("TAG", pickerNumberSet.toString());

        for (int i = 1; i < 46; i++) {

            if (pickerNumberSet.contains(i)) {
                continue;
            }
            numberList.add(i);
        }
        Collections.shuffle(numberList);
        Log.d("TAG", "number List " + numberList.subList(0, 6).toString());
        return numberList.subList(0, (6 - pickerNumberSet.size()));
    }

    private Drawable setTextViewBackground(int number) {
        Drawable drawable = null;
        // Drawable Resource 를 가져오는 방법
        // todo 1부터 45까지 색상을 지정해주세요
        if (number < 11) {
            drawable = ContextCompat.getDrawable(this, R.drawable.round_background1);
        } else if (number < 21) {
            drawable = ContextCompat.getDrawable(this, R.drawable.round_background2);
        } else if (number < 31) {
            drawable = ContextCompat.getDrawable(this, R.drawable.round_background3);
        } else if (number < 41) {
            drawable = ContextCompat.getDrawable(this, R.drawable.round_background4);
        } else if (number < 46) {
            drawable = ContextCompat.getDrawable(this, R.drawable.round_background5);
        }
        return drawable;

    }
}

strings.xml

<resources>
    <string name="app_name">My Lotto App</string>
    <string name="numplus">번호추가하기</string>
    <string name="initialization">초기화하기</string>
    <string name="gameStart">게임실행하기</string>
    <string name="add_number">번호추가하기</string>
    <string name="init_number">초기화하기</string>
</resources>