Я новичок, и сейчас я играю в игру.Когда игра заканчивается, отображается сообщение с указанием победителя.После этого сообщения я хочу отключить все кнопки.
fun buselect(view: View) {
val buchice = view as Button
var cellid = 0
when (buchice.id) {
R.id.button -> cellid = 1
R.id.button2 -> cellid = 2
R.id.button3 -> cellid = 3
R.id.button4 -> cellid = 4
R.id.button5 -> cellid = 5
R.id.button6 -> cellid = 6
R.id.button7 -> cellid = 7
R.id.button8 -> cellid = 8
R.id.button9 -> cellid = 9
}
var player1 = ArrayList<Int>()
var player2 = ArrayList<Int>()
var activeplayer = 1
fun Playgame(cellid: Int, buchice: Button) {
if (activeplayer == 1) {
buchice.text = "x"
buchice.setBackgroundResource(R.color.blue)
player1.add(cellid)
activeplayer = 2
} else {
buchice.text = "o"
buchice.setBackgroundResource(R.color.green)
player2.add(cellid)
activeplayer = 1
}
buchice.isEnabled = false
checkwinner()
}
fun checkwinner() {
var winner = -1
if (player1.contains(1) && player1.contains(2) && player1.contains(3)) {
winner = 1
}
if (player2.contains(1) && player2.contains(2) && player2.contains(3)) {
winner = 2
}
if (player1.contains(4) && player1.contains(5) && player1.contains(6)) {
winner = 1
}
if (player2.contains(4) && player2.contains(5) && player2.contains(6)) {
winner = 2
}
if (player1.contains(7) && player1.contains(8) && player1.contains(9)) {
winner = 1
}
if (player2.contains(7) && player2.contains(8) && player2.contains(9)) {
winner = 2
}
// clon1
if (player1.contains(1) && player1.contains(4) && player1.contains(7)) {
winner = 1
}
if (player2.contains(1) && player2.contains(4) && player2.contains(7)) {
winner = 2
}
if (player1.contains(2) && player1.contains(5) && player1.contains(8)) {
winner = 1
}
if (player2.contains(2) && player2.contains(5) && player2.contains(8)) {
winner = 2
}
if (player1.contains(3) && player1.contains(6) && player1.contains(9)) {
winner = 1
}
if (player2.contains(3) && player2.contains(6) && player2.contains(9)) {
winner = 2
}
if (winner != -1) {
if (winner == 1) {
Toast.makeText(
this,
"player 1 win this sgame ",
Toast.LENGTH_LONG
).show()
} else {
Toast.makeText(
this,
"player 2 win this sgame",
Toast.LENGTH_LONG
).show()
}
}
}
}
проблема в последней части, где определяется победитель
<TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center">
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button3"
android:background="@android:color/background_light" android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" android:onClick="buselect" android:textSize="90sp"/>
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button2"
android:background="@android:color/background_light" android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" android:onClick="buselect" android:textSize="90sp"/>
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button"
android:background="@android:color/background_light" android:layout_marginBottom="10dp"
android:onClick="buselect" android:textSize="90sp"/>
</TableRow>
<TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center">
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button6"
android:background="@android:color/background_light" android:layout_marginBottom="10dp"
android:layout_marginRight="10dp" android:onClick="buselect" android:textSize="90sp"/>
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button5"
android:background="@android:color/background_light" android:layout_marginBottom="10dp"
android:layout_marginRight="10dp" android:onClick="buselect" android:textSize="90sp"/>
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button4"
android:background="@android:color/background_light" android:layout_marginBottom="10dp"
android:onClick="buselect" android:textSize="90sp"/>
</TableRow>
<TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center">
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button9"
android:background="@android:color/background_light" android:layout_marginRight="10dp"
android:onClick="buselect" android:textSize="90sp"/>
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button8"
android:background="@android:color/background_light" android:layout_marginRight="10dp"
android:onClick="buselect" android:textSize="90sp"/>
<Button
android:text=""
android:layout_width="120dp"
android:layout_height="120dp" android:id="@+id/button7"
android:background="@android:color/background_light" android:onClick="buselect"
android:textSize="90sp"/>
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>