Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Laurie Wu
Laurie Wu
709 Points

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.