0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Level 3. Coin Flip(The Ethernaut)

Last updated at Posted at 2018-04-25

「The Ethernaut by Zeppelin Level 3. Coin Flip」を翻訳してみました(CryptoZombiesのように?)

原文はこちら

https://ethernaut.zeppelin.solutions
3. Coin Flipをクリック

3. Coin Flip

難易度 3/6
コインフリップゲーム(コインを投げて裏表を当てるゲーム)にて結果を推測し、必勝パターンを構築する必要があるぞ。
このレベルのクリア条件は、予知能力を駆使して10回連続で結果を推測することだ。
(10回連続でコインの裏表を当てよ)

回答の助けになるヒント
  • Solidity Remix IDEについて

ソースコード

pragma solidity ^0.4.18;

contract CoinFlip {
  uint256 public consecutiveWins;
  uint256 lastHash;
  uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;
  
  function CoinFlip() public {
    consecutiveWins = 0;
  }

  function flip(bool _guess) public returns (bool) {
    uint256 blockValue = uint256(block.blockhash(block.number-1));
    
    if (lastHash == blockValue) {
      revert();
    }

    lastHash = blockValue;
    uint256 coinFlip = uint256(uint256(blockValue) / FACTOR);
    bool side = coinFlip == 1 ? true : false;
    
    if (side == _guess) {
      consecutiveWins++;
      return true;
    } else {
      consecutiveWins = 0;
      return false;
    }
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?