Ellen Spertus
10/10/2022, 5:39 PMKlitos Kyriacou
10/10/2022, 6:04 PMvar x: Int
var y: Int
do {
x = Random.nextInt(0, NUM_COLS)
y = Random.nextInt(0, NUM_ROWS)
} while (isOccupied(x, y))
place(entity, x, y)
Ellen Spertus
10/10/2022, 6:05 PMKlitos Kyriacou
10/10/2022, 6:07 PMephemient
10/10/2022, 6:31 PMval
val x: Int
var y: Int
if (Random.nextBoolean()) {
return
} else {
x = 1
y = 2
}
// OK to use x and y here
bezrukov
10/10/2022, 7:05 PMplaceIfEmpty(entity, x, y)
fun which returns true if the entity was added. Then use while or do-while as mentioned above. e.g:
do {
val x = Random.nextInt(NUM_COLS)
val y = Random.nextInt(NUM_ROWS)
} while(!placeIfEmpty(entity, x, y)
Additionally, you will avoid implicit contract "check it's not occupied before placing"Klitos Kyriacou
10/10/2022, 7:50 PMwhile
condition expression. Unlike Java.