-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMainActivity.kt
More file actions
80 lines (57 loc) · 2.23 KB
/
Copy pathMainActivity.kt
File metadata and controls
80 lines (57 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Memory Game
// By Kurt Kaiser
// kurtkaiser.us
package com.example.memorygame
import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.memorygame.R.drawable.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val images: MutableList<Int> =
mutableListOf(camel, coala, fox, lion, monkey, wolf, camel, coala, fox, lion, monkey, wolf)
val buttons = arrayOf(button1, button2, button3, button4, button5, button6, button7, button8,
button9, button10, button11, button12)
val cardBack = code
var clicked = 0
var turnOver = false
var lastClicked = -1
images.shuffle()
for(i in 0..11){
buttons[i].setBackgroundResource(cardBack)
buttons[i].text = "cardBack"
buttons[i].textSize = 0.0F
buttons[i].setOnClickListener {
if (buttons[i].text == "cardBack" && !turnOver) {
buttons[i].setBackgroundResource(images[i])
buttons[i].setText(images[i])
if (clicked == 0) {
lastClicked = i
}
clicked++
} else if (buttons[i].text !in "cardBack") {
buttons[i].setBackgroundResource(cardBack)
buttons[i].text = "cardBack"
clicked--
}
if (clicked == 2) {
turnOver = true
if (buttons[i].text == buttons[lastClicked].text) {
buttons[i].isClickable = false
buttons[lastClicked].isClickable = false
turnOver = false
clicked = 0
}
} else if (clicked == 0) {
turnOver = false
}
}
}
}
}