# how to use random() just for the empty cells in kotlin

I'm making a tic tic toe android game using kotlin. So i use this code below(autoPlay function), if the user chooses to play against the system. the problem is that : the system can play the same cell twice witch is not permissible. please help me this is the class code, if thers is something to change please tell me.

```
package com.ysf.android.tictactoe
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_single_player.*
import java.util.*
/**
* Created by ysf on 14/04/2018.
*/
class SinglePlayerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_single_player)
}
//when a button is selected do this
fun buSelect(view: View) {
//things to do
val buChoise = view as Button
var cellID = 0
when (buChoise.id) {
R.id.B1 -> cellID = 1
R.id.B2 -> cellID = 2
R.id.B3 -> cellID = 3
R.id.B4 -> cellID = 4
R.id.B5 -> cellID = 5
R.id.B6 -> cellID = 6
R.id.B7 -> cellID = 7
R.id.B8 -> cellID = 8
R.id.B9 -> cellID = 9
}
playGame(cellID, buChoise)
}
var player1 = ArrayList<Int>()
var player2 = ArrayList<Int>()
var activePlayer = 1
fun playGame(cellID: Int, buChoise: Button) {
if (player1.size < 4 || player2.size < 3) {
if (activePlayer == 1) {
buChoise.text = "X"
buChoise.setBackgroundResource(R.color.holo_purple)
player1.add(cellID)
activePlayer = 2
AutoPlay()
} else {
buChoise.text = "O"
buChoise.setBackgroundResource(R.color.holoGreen)
player2.add(cellID)
activePlayer = 1
}
buChoise.isEnabled = false
CheckWenner()
}else { Toast.makeText(this, "Game over !!", Toast.LENGTH_SHORT).show()}
}
fun CheckWenner() {
var winner = -1
/* that for rows
**/
//row 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
}
//row 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
}
//row 3
if (player1.contains(7) && player1.contains(8) && player1.contains(9)) {
winner = 1
}
if (player2.contains(7) && player2.contains(8) && player2.contains(9)) {
winner = 2
}
/* that for colons
**/
//colon 1
if (player1.contains(1) && player1.contains(4) && player1.contains(7)) {
winner = 1
}
if (player2.contains(1) && player2.contains(4) && player2.contains(7)) {
winner = 2
}
//colon 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
}
//colon 3
if (player1.contains(3) && player1.contains(6) && player1.contains(9)) {
winner = 1
}
if (player2.contains(3) && player2.contains(6) && player2.contains(9)) {
winner = 2
}
/* that for Diagonals
*/
// first diagonal
if (player1.contains(1) && player1.contains(5) && player1.contains(9))
winner = 1
if (player2.contains(1) && player2.contains(5) && player2.contains(9))
winner = 2
// second diagonal
if (player1.contains(3) && player1.contains(5) && player1.contains(7))
winner = 1
if (player2.contains(3) && player2.contains(5) && player2.contains(7))
winner = 2
// the winner is
if (winner != -1) {
if (winner == 1) {
Toast.makeText(this, "the first player win the game!", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(this, "the second player win the game!", Toast.LENGTH_LONG).show()
}
}
}
// autoPlay function
fun AutoPlay() {
// scan for empty cells
val emptyCells = ArrayList<Int>()
for (cellID in 1..9) {
if (!(player1.contains(cellID) || player2.contains(cellID)))
emptyCells.add(cellID)
}
// select ran index
val r = Random()
val randIndex = r.nextInt(emptyCells.size - 0) + 0
val CellID = emptyCells[randIndex]
// interpreter index to button
val buSelect: Button?
when (CellID) {
1 -> buSelect = Bu1
2 -> buSelect = Bu2
3 -> buSelect = Bu3
4 -> buSelect = Bu4
5 -> buSelect = Bu5
6 -> buSelect = Bu6
7 -> buSelect = Bu7
8 -> buSelect = Bu8
9 -> buSelect = Bu9
else -> {
buSelect = Bu1
}
}
playGame(CellID, buSelect)
}
}
```