table of Contents
1, the actual wins and lose 1-
3, the actual 3- failed zombie
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 twowinCount
properties:uint16
( ),lossCount(uint16)
2.
Modify zombie generating_createZombie()
function definition, so that each new zombies has0
won and0
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 checkrand
is not less than or equal toattackVictoryProbability
.
2.
If the above conditionstrue
, our zombies win, you do the following:(1) an increase
myZombie
ofwinCount
.(2) an increase
myZombie
oflevel
.(3) increase
enemyZombie
oflossCount
.(4) run
feedAndMultiply
function. Inzombiefeeding.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
oflossCount
.(2) an increase
enemyZombie
ofwinCount
.
2.
Inelse
the end, on themyZombie
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);
}
}
}