Ch 3: Rounds and Contributor Access
In this chapter, we'll implement a reset mechanism that allows the clown to get back up for multiple rounds, ensuring each game session starts fresh. We'll also track contributors per round so that only players who participated in knocking out the clown can call rob(). By the end, we'll have a fully functional round-based game where secrets remain shielded until conditions are met! Estimated time: ~15 minutes.
The need for a Reset mechanism
Right now, once the clown is knocked out, there's no way to reset it. If a game session were to continue, we'd have no way to start fresh — the stamina would remain at 0, and the secret would be permanently accessible.
To solve this, we need to introduce:
A
resetfunction that restores the clown to its original state.Round tracking, so each reset creates a new round.
The need for a contributor check
While the reset mechanism and round tracking allow us to restart the game for continuous play, they still don't address who should be allowed to call the rob() function.
Right now, any player can call rob() once the clown is knocked out, even if they didn't participate in hitting it during the current round. This creates the following issues:
Fairness: Players who didn't contribute should not be able to reap the benefits of robbing the clown.
Incentivizing Contribution: The game needs to encourage active participation by ensuring that only those who helped knock out the clown in a specific round are rewarded with access to the secret.
The solution to this is implementing a conditional check on rob() which allows only those players who contributed at least one hit in the current round to steal the secret.
Implementing the Reset Mechanism
The reset mechanism allows the clown to get back up for multiple rounds, with each round starting fresh. It restores the clown's stamina and picks a new random secret, then increments the round counter.
Here's how we can implement the reset function:
What's Happening Here?
Condition for Reset (
requireDown): The reset function can only be called once the clown is knocked out, enforced by therequireDownmodifier.Restoring Initial State: The stamina is reset to
initialClownStamina, and a new random secret is selected via_randomIndex().Round Tracking: The
roundcounter increments each time the clown is reset, allowing us to distinguish between rounds.
Modifying hit() to track contributions
To enforce fair access to the secrets, we'll track the number of hits each player contributes in a given round. This is achieved using the hitsPerRound mapping:
Every time a player calls the hit() function, we update their contribution in the current round:
What's Happening Here?
Tracking Contributions: The
hitsPerRoundmapping records each player's hits in the current round. This ensures we can verify who participated when the clown was knocked out.Replayable Rounds: Because contributions are tracked by round, the game can fairly reset and start fresh without losing player data from previous rounds.
Restricting rob() with a contributor check
To ensure only contributors can rob the clown, we'll use a modifier called onlyContributor:
We'll then apply this modifier to the rob() function:
Congratulations! You made it through writing the entire shielded smart contract for a multiplayer, multi-round, Clown Beatdown game!
Final ClownBeatdown contract
Now, onto testing the contract!
Last updated

