Coinflip

Use your psychic abilities to guess 5 coinflips in a row.

Please log in
Difficulty: Novice
User avatar

Author

This is a coin flipping game where you need to build up your winning streak by guessing the outcome of a coin flip. To complete this level you’ll need to use your psychic abilities to guess the correct outcome 5 times in a row.

You can use post conditions to abort transactions after they have completed execution.

cadence
		
			pub contract Coinflip {

    pub var consecutiveWins: UInt64

    pub fun flip(guess: Bool) {
        let randomNum: UInt64 = revertibleRandom()
        let flip: UInt64 = randomNum % 2
        let side: Bool = flip == 1 ? true : false
        
        if side == guess {
            self.consecutiveWins = self.consecutiveWins + 1
        } else {
            self.consecutiveWins = 0
        }
    }

    init() {
        self.consecutiveWins = 0
    }
}