Ayden
01/23/2018, 3:25 PMif (
player1List.containsAll(listOf(1, 3, 7)) ||
player1List.containsAll(listOf(1, 3, 9)) ||
player1List.containsAll(listOf(1, 7, 9)) ||
player1List.containsAll(listOf(1, 3, 8))
) {
cell = 5
} else if (
player1List.containsAll(listOf(1, 3, 4))
) {
cell = 2
} else if (
player1List.containsAll(listOf(1, 5, 7))
) {
cell = 3
} else if (
player1List.containsAll(listOf(1, 3, 5)) ||
player1List.containsAll(listOf(1, 2, 4))
) {
cell = 7
} else if (player1List.containsAll(listOf(3, 5, 9))) {
cell = 1
}
Andreas Sinz
01/23/2018, 3:35 PMList<Pair<List<List<int>>, int>>
. first
is your list of condition-lists and second
is the cell value. then you can do something like cell = map.firstOrNull { pair -> pair.first.all { player1List.containsAll(it) } }?.second
leosan
01/23/2018, 3:39 PMlistOf(1,2,3)
you are creating a new object every time you check the conditionedwardwongtl
01/23/2018, 3:57 PMAyden
01/23/2018, 3:57 PMList<Pair<List<List<int>>, int>>
edwardwongtl
01/23/2018, 3:58 PMMap<List<Int>, Int>
Ayden
01/23/2018, 4:00 PMgaetan
01/23/2018, 4:04 PMif (p1.hasPlayed(1, 3, 7) ||
p1.hasPlayed(1, 3, 9) ||
p1.hasPlayed(1, 7, 9) ||
p1.hasPlayed(1, 3, 8)) { cell = 5 }
else if (p1.hasPlayed(1, 3, 5) ||
p1.hasPlayed(1, 2, 4)) { cell = 7 }
else if (p1.hasPlayed(1, 3, 4)) { cell = 2 }
else if (p1.hasPlayed(1, 5, 7)) { cell = 3 }
else if (p1.hasPlayed(3, 5, 9)) { cell = 1 }
edwardwongtl
01/23/2018, 4:05 PMcell
outside, also replace all those if else if
with when
gaetan
01/23/2018, 4:06 PMwhen {
p1.hasPlayed(1, 3, 7) ||
p1.hasPlayed(1, 3, 9) ||
p1.hasPlayed(1, 7, 9) ||
p1.hasPlayed(1, 3, 8) -> cell = 5
p1.hasPlayed(1, 3, 5) ||
p1.hasPlayed(1, 2, 4) -> cell = 7
p1.hasPlayed(1, 3, 4) -> cell = 2
p1.hasPlayed(1, 5, 7) -> cell = 3
p1.hasPlayed(3, 5, 9) -> cell = 1
}
edwardwongtl
01/23/2018, 4:08 PMcell = when {
p1.hasPlayed(1, 3, 7) ||
p1.hasPlayed(1, 3, 9) ||
p1.hasPlayed(1, 7, 9) ||
p1.hasPlayed(1, 3, 8) -> 5
p1.hasPlayed(1, 3, 5) ||
p1.hasPlayed(1, 2, 4) -> 7
p1.hasPlayed(1, 3, 4) -> 2
p1.hasPlayed(1, 5, 7) -> 3
p1.hasPlayed(3, 5, 9) -> 1
else -> // some default value
}
gaetan
01/23/2018, 4:09 PMedwardwongtl
01/23/2018, 4:09 PMgaetan
01/23/2018, 4:11 PMAyden
01/23/2018, 4:11 PMedwardwongtl
01/23/2018, 4:17 PM0
seems to be a nice choice1-9
Ayden
01/23/2018, 4:18 PMedwardwongtl
01/23/2018, 4:24 PMdata class Condition(val pos1: Int, val pos2: Int, val pos3: Int, val value: Int)
val conditions = listOf(
Condition(1, 3, 7, 5),
Condition(1, 3, 9, 5),
Condition(1, 7, 9, 5),
Condition(1, 3, 8, 5),
Condition(1, 3, 4, 2),
Condition(1, 5, 7, 3),
Condition(1, 3, 5, 7),
Condition(1, 2, 4, 7),
Condition(3, 5, 9, 1)
)
val player1List = listOf(1,2,4)
val cell = conditions.firstOrNull { (pos1, pos2, pos3, _) ->
player1List.contains(pos1) and player1List.contains(pos2) and player1List.contains(pos3)
}?.value ?: 0
Andreas Sinz
01/23/2018, 5:00 PMnull
, the compiler enforces that you check whether its a valid celledwardwongtl
01/23/2018, 6:06 PMAndreas Sinz
01/23/2018, 6:30 PM0
is an ordinary integer just like 1
or 9
and can be mistaken for a valid cell if you forget to check cell != 0
benleggiero
01/24/2018, 2:52 AMcell = when {
player1List.containsAll(listOf(1, 3, 7))
|| player1List.containsAll(listOf(1, 3, 9))
|| player1List.containsAll(listOf(1, 7, 9))
|| player1List.containsAll(listOf(1, 3, 8))
-> 5
player1List.containsAll(listOf(1, 3, 4))
-> 2
player1List.containsAll(listOf(1, 5, 7))
-> 3
player1List.containsAll(listOf(1, 3, 5))
|| player1List.containsAll(listOf(1, 2, 4))
-> 7
player1List.containsAll(listOf(3, 5, 9))
-> 1
else -> defaultValue
}
edwardwongtl
01/24/2018, 3:30 AMcell == 0
before thoughAyden
01/24/2018, 1:58 PMedwardwongtl
01/24/2018, 4:20 PMandyb
01/25/2018, 8:42 AMclass Condition(...)
though.edwardwongtl
01/25/2018, 8:46 AMdata class
allows you to use the destructing syntax