Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Kotlin for Java Developers Is This Thing On? It's Alive!

I got an assertion failed error for the line assert(numGames < maxGames).

I got an assertion failed error for the line assert(numGames < maxGames). When game number is 10000. Not sure if that's expected?

2 Answers

angel juarez
angel juarez
15,886 Points

I got the same error when i run the test, but I just run it a couple of times and it passed. Here's the code from the video that failed at first and then passed:

class GameTest{ @Test fun kingInFirstFoundationPile(){ // arrange var numGames = 0 val maxGames = 10000

    // act
    for(i in 1.. maxGames){
        numGames++
        GameModel.resetGame()
        for (j in 1..100){
            GamePresenter.onDeckTap()
            GamePresenter.onWasteTap()
            GameModel.tableauPiles.forEachIndexed { index , tableauPile ->
                GamePresenter.onTableauPileTap(index, tableauPile.cards.lastIndex)
            }
        }
        if (GameModel.foundationPile[0].cards.size == 13){
            break
        }
    }
    // assert

    GameModel.debugPrint()
    println("# Games: $numGames")
    assert(numGames < maxGames)
}

}

Seems like the instructor has a hard time getting a situation where the game is not complete. I'm definitely having the opposite problem!

I've run my kingInFirstFoundationPile test about 20 times and it passed twice. I even bumped maxGames up to 100,000 and it still failed at assert(numGames < maxGames)! I reviewed the code - especially the test code - and I couldn't find anything wrong. I also tried increasing (j in 1..100) to (j in 1..200) thinking that more taps might increase my chances of getting a king in that first foundation pile, but it doesn't seem to help.

The error is the same every time, and numGames is always equal to maxGames at the end of my failed tests. I'll review my code again and if there's an error, I'll post an update.