[CryptoZombies - 2 Solidity 008] Advanced real - win and lose zombie

table of Contents

I. Introduction

Second, combat

1, the actual wins and lose 1-

1. Requirements

2. Code

2, 2- zombie combat victory

1. Requirements

2. Code

3, the actual 3- failed zombie

1. Requirements

2. Code


I. Introduction

Read some block chain of tutorials, papers, on the Internet just to find a project combat, CryptoZombies.

We continue to improve our code, this is the real lesson content.

If you want to learn more about machine learning, deep learning, block chain, computer vision and other related technologies, to communicate with more chiefs, then scan the next Fanger Wei code to join us now!

Second, combat

1, the actual wins and lose 1-

Our zombie game, we are going to win or lose track of our zombie how many games. With this we can maintain a "zombie list" in the game.

So we need to add two new properties in the structure of the Zombie winCount and lossCount。并做相关操作:

1. Requirements

1. The modified  Zombie structure, add two winCountproperties: uint16( ),lossCount(uint16)

2.Modify zombie generating _createZombie() function definition, so that each new zombies has  0 won and  0 lost.

2. Code

pragma solidity >=0.5.0 <0.6.0;

import "./ownable.sol";

contract ZombieFactory is Ownable {

    event NewZombie(uint zombieId, string name, uint dna);

    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;
    uint cooldownTime = 1 days;

    struct Zombie {
      string name;
      uint dna;
      uint32 level;
      uint32 readyTime;
      // 1. Add new properties here
      uint16 winCount;
      uint16 lossCount;
    }

    Zombie[] public zombies;

    mapping (uint => address) public zombieToOwner;
    mapping (address => uint) ownerZombieCount;

    function _createZombie(string memory _name, uint _dna) internal {
        // 2. Modify new zombie creation here:
        uint id = zombies.push(Zombie(_name, _dna, 1, uint32(now + cooldownTime), 0, 0)) - 1;
        zombieToOwner[id] = msg.sender;
        ownerZombieCount[msg.sender]++;
        emit NewZombie(id, _name, _dna);
    }

    function _generateRandomDna(string memory _str) private view returns (uint) {
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    function createRandomZombie(string memory _name) public {
        require(ownerZombieCount[msg.sender] == 0);
        uint randDna = _generateRandomDna(_name);
        randDna = randDna - randDna % 100;
        _createZombie(_name, randDna);
    }

}

2, 2- zombie combat victory

1. Requirements

Assumed in our game, players can upgrade their zombies by paying ETH. ETH stored in the contract you have - the example of a straightforward, to show you that you can make money through their own game.

1. Create a  if statement to check  rand is not less than or equal to  attackVictoryProbability.

2.If the above conditions  true, our zombies win, you do the following:

(1) an increase  myZombie of  winCount.

(2) an increase  myZombie of  level.

(3) increase  enemyZombie of  lossCount.

(4) run  feedAndMultiply function. In  zombiefeeding.sol view of the statement in calling it. For the third parameter ( _species), passing the string "zombie". (Now it actually does not do anything, but later, if we wish, you can add additional methods used to make zombies become zombies).

 

2. Code

pragma solidity >=0.5.0 <0.6.0;

import "./zombiehelper.sol";

contract ZombieAttack is ZombieHelper {
  uint randNonce = 0;
  uint attackVictoryProbability = 70;

  function randMod(uint _modulus) internal returns(uint) {
    randNonce++;
    return uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % _modulus;
  }

  function attack(uint _zombieId, uint _targetId) external ownerOf(_zombieId) {
    Zombie storage myZombie = zombies[_zombieId];
    Zombie storage enemyZombie = zombies[_targetId];
    uint rand = randMod(100);
    // Start here
    if (rand <=attackVictoryProbability) {
      myZombie.winCount++;
      myZombie.level++;
      enemyZombie.lossCount++;
      feedAndMultiply(_zombieId, enemyZombie.dna, "zombie");
    }
  }
}

 

3, the actual 3- failed zombie

1. Requirements

In our game, the zombies lost and does not degrade - simply to  lossCount add one, and trigger cooled, wait a day before the war again.

1. Add a  else statement. If we lose zombies:

(1) an increase  myZombie of  lossCount.

(2) an increase  enemyZombie of  winCount.

2.In  else the end, on the  myZombie run  _triggerCooldown method. This allows each zombie war only once a day.

Note, here we use the else statement, learned other programming languages ​​such as C ++, else should be no stranger to the solidity of, and in C ++ is the same.

2. Code

pragma solidity >=0.5.0 <0.6.0;

import "./zombiehelper.sol";

contract ZombieAttack is ZombieHelper {
  uint randNonce = 0;
  uint attackVictoryProbability = 70;

  function randMod(uint _modulus) internal returns(uint) {
    randNonce++;
    return uint(keccak256(abi.encodePacked(now, msg.sender, randNonce))) % _modulus;
  }

  function attack(uint _zombieId, uint _targetId) external ownerOf(_zombieId) {
    Zombie storage myZombie = zombies[_zombieId];
    Zombie storage enemyZombie = zombies[_targetId];
    uint rand = randMod(100);
    if (rand <= attackVictoryProbability) {
      myZombie.winCount++;
      myZombie.level++;
      enemyZombie.lossCount++;
      feedAndMultiply(_zombieId, enemyZombie.dna, "zombie");
    } // start here
    else {
      myZombie.lossCount++;
      enemyZombie.winCount++;
      _triggerCooldown(myZombie);
    }
  }
}

 

Published 272 original articles · won praise 535 · views 540 000 +

Guess you like

Origin blog.csdn.net/shuiyixin/article/details/104573390