Issue
I write a program in Kotlin. The program is working fine, but I only test the function with running the program. I think I know what the problem is, but I don't know, how to modiy the test. Thanks in advance!
class Dice {
var firstDice = 1
var secondDice = 1
var thirdDice = 3
var fourthDice = 2
var fifthDice = 2
val dices: MutableList<Int> = ArrayList()
fun throwDices() {
firstDice = Random.nextInt(1,7)
dices.add(firstDice)
println("Eldobtad a kockákat, az első kocka értéke: $firstDice")
secondDice = Random.nextInt(1,7)
dices.add(secondDice)
println("A második kocka értéke: $secondDice")
thirdDice = Random.nextInt(1,7)
dices.add(thirdDice)
println("A harmadik kocka értéke: $thirdDice")
fourthDice = Random.nextInt(1,7)
dices.add(fourthDice)
println("A negyedik kocka értéke: $fourthDice")
fifthDice = Random.nextInt(1,7)
dices.add(fifthDice)
println("Az ötödik kocka értéke: $fifthDice")
dices.sort()
println(dices)
}
fun winningCheck() :Int {
if(dices[0] == dices[1] && dices[0] == dices[2] && dices[0] == dices[3] && dices[0] == dices[4] ) {
return 15
}
else if(dices[0] == dices[1] && dices[0] == dices[2] && dices[0] == dices[3]) {
return 9
}
else if (dices[0] == dices[1] && dices[0] == dices[2] && dices[3] == dices[4]) {
return 5
}
else if(dices[0] == dices[1] && dices[0] == dices[2] || dices[1] == dices[2] && dices[1] == dices[3] ||
dices[2] == dices[3] && dices[2] == dices[4]) {
return 3
}
else if(dices[0] == dices[1] && dices[2] == dices[3] ||
dices[1] == dices[2] && dices[3] == dices[4] ||
dices[0] == dices[1] && dices[3] == dices[4]) {
return 2
}else if (dices[0] == dices[1] || dices[1] == dices[2] || dices[2] == dices[3] || dices[3] == dices[4]) {
return 1
}
else
return 0
}
}
This is the class, I want to test, and here is the testing code:
class DiceTest {
private val dice = Dice()
private val dices: MutableList<Int> = ArrayList()
@Before
fun init() {
dice.firstDice = 1
dice.secondDice = 1
dice.thirdDice= 1
dice.fourthDice = 1
dice.fifthDice = 1
dices.add(dice.firstDice)
dices.add(dice.secondDice)
dices.add(dice.thirdDice)
dices.add(dice.fourthDice)
dices.add(dice.fifthDice)
}
@Test
fun checkingFiveOfAKind () {
assertEquals(15,dice.winningCheck())
}
And here is the error code:
Solution
I think your example could be simplified to ask, why does't this code work:
@Test
fun checkingFiveOfAKind() {
val dices: MutableList<Int> = ArrayList()
dices.add(1)
dices.add(1)
dices.add(1)
dices.add(1)
dices.add(1)
val anotherDices: MutableList<Int> = ArrayList()
val result :Int
if(anotherDices[0] == anotherDices[1]
&& anotherDices[0] == anotherDices[2]
&& anotherDices[0] == anotherDices[3]
&& anotherDices[0] == anotherDices[4] ) {
result = 15
} else {
result = 0
}
assertEquals(15, result)
}
But this code does work:
@Test
fun checkingFiveOfAKind() {
val dices: MutableList<Int> = ArrayList()
dices.add(1)
dices.add(1)
dices.add(1)
dices.add(1)
dices.add(1)
val result :Int
if(dices[0] == dices[1]
&& dices[0] == dices[2]
&& dices[0] == dices[3]
&& dices[0] == dices[4] ) {
result = 15
} else {
result = 0
}
assertEquals(15, result)
}
Or written another way:
You are adding your dice rolls to a variable called dices
but this is a declared in your test, it is not the variable you are using inside your Dice
class, that is another List.
You may be able to fix your test like this:
class DiceTest {
private val dice = Dice()
@Before
fun init() {
dice.firstDice = 1
dice.secondDice = 1
dice.thirdDice= 1
dice.fourthDice = 1
dice.fifthDice = 1
dice.dices.add(dice.firstDice)
dice.dices.add(dice.secondDice)
dice.dices.add(dice.thirdDice)
dice.dices.add(dice.fourthDice)
dice.dices.add(dice.fifthDice)
}
@Test
fun checkingFiveOfAKind () {
assertEquals(15, dice.winningCheck())
}
Answered By - Blundell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.