activity_main.xml
<TextView
android:id="@+id/textView"
android:layout_width="308dp"
android:layout_height="62dp"
android:text="welcome"
android:textSize="30sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.142"/>
<Spinner
android:layout_width="138dp"
android:layout_height="67dp"
android:id="@+id/spinner1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.134"
app:layout_constraintVertical_bias="0.353"/>
<Spinner
android:layout_width="138dp"
android:layout_height="67dp"
android:id="@+id/spinner2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.352"/>
<Spinner
android:layout_width="138dp"
android:layout_height="67dp"
android:id="@+id/spinner3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_bias="0.352"
app:layout_constraintHorizontal_bias="0.863"/>
<Button
android:text="Button"
android:layout_width="175dp"
android:layout_height="86dp"
android:id="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/spinner2"
android:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintVertical_bias="0.0"/>
item_spinner
숫자 선택하는 spinner의 item(Constraintlayout없이 작성)
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingTop="10dp"
android:paddingStart="30dp"
android:textColor="@android:color/darker_gray"
android:textSize="15sp"
android:paddingLeft="30dp"/>
MainActivity
class MainActivity : AppCompatActivity() {
// baseball 클래스 상속
var baseball:Baseball? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupSpinner(R.id.spinner1)
setupSpinner(R.id.spinner2)
setupSpinner(R.id.spinner3)
baseball = Baseball()
baseball!!.random()
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
action()
}
}
fun action(){
val spinner1 = findViewById<Spinner>(R.id.spinner1)
val spinner2 = findViewById<Spinner>(R.id.spinner2)
val spinner3 = findViewById<Spinner>(R.id.spinner3)
val num1 = spinner1.selectedItem.toString().toInt()
val num2 = spinner2.selectedItem.toString().toInt()
val num3 = spinner3.selectedItem.toString().toInt()
var spinnerArr = arrayOf<Int>(num1, num2, num3)
val result:Result? = baseball?.finding(spinnerArr)
val textView = findViewById<TextView>(R.id.textView)
if(result != null){
if(result.strike == 3){
baseball?.clear = true
textView.text = baseball?.resultString()
}
else{
textView.text = "${result.strike} 스트라이크 ${result.ball} 볼입니다"
}
}
}
// spinner
fun setupSpinner(spinnerNum:Int){
var countArr = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val adapter = ArrayAdapter(this, R.layout.item_spinner, countArr)
val spinner = findViewById<Spinner>(spinnerNum)
spinner.adapter = adapter
}
}
Baseball.kt(클래스)
숫자 야구 규칙 설정
class Baseball {
var randNum:IntArray = IntArray(3)
var clear:Boolean = false
constructor(){
clear = false
}
fun random(){
/*
while (true) {
randNum[0] = (Math.random() * 10).toInt() + 1 // -> 1 ~ 10
randNum[1] = (Math.random() * 10).toInt() + 1
randNum[2] = (Math.random() * 10).toInt() + 1
if(randNum[0] != randNum[1]
&& randNum[0] != randNum[2]
&& randNum[1] != randNum[2]){
break
}
}
*/
// switch 0 0 1 0 0 0 0 0 0 0
var switch = BooleanArray(9)
for (i in switch.indices){
switch[i] = false
}
var w = 0
while (w < 3) {
var r = (Math.random() * 9).toInt() // 0 ~ 8
if (switch[r] == false) {
switch[r] = true
randNum[w] = r + 1 // 1 ~ 9
w++
}
}
for (i in randNum.indices){
println("randNum[$i] = ${randNum[i]}")
}
}
// 판정
fun finding(userNum:Array<Int>) : Result {
var strike:Int = 0
var ball:Int = 0
// ball
for (i in userNum.indices){
for (j in userNum.indices){
if(userNum[i] == randNum[j] && i != j){
ball++
}
}
}
// strike
for (i in userNum.indices){
if(userNum[i] == randNum[i]){
strike++
}
}
return Result(strike, ball)
}
// 결과
fun resultString() : String{
if(clear == true){
return "축하합니다 게임클리어!"
}else{
return "아쉽습니다 다시 도전하십시오."
}
}
}
class Result(val strike:Int, val ball:Int)
'안드로이드' 카테고리의 다른 글
[Android] 숫자 +/- (0) | 2022.04.05 |
---|---|
[Android] GoogleMapsFragment (0) | 2022.04.05 |
[Android] Geocoding (0) | 2022.04.03 |
[안드로이드] Camera (0) | 2022.04.03 |
[Android] 동적버튼 (0) | 2022.04.03 |
댓글