今天来聊聊我们的第三个案例。我们来做一个比大小的下注游戏。
需要有两个角色。
功能上则需要。
接下来,我们来看看下面范例。
/ SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract BetGame {
//GM
address public owner;
bool isFinshed;
//纪录玩家用的 struct
struct Player {
address payable addr;
uint amount;
}
//存下大的玩家
Player[] big;
//存下小的玩家
Player[] small;
//下大的总金额
uint totalBig;
//下小的总金额
uint totalSmall;
uint nowtime;
constructor () {
owner = msg.sender;
totalSmall = 0;
totalBig = 0;
isFinshed = false;
nowtime = block.timestamp;
}
function Bet(bool flag)public payable returns (bool){
require(msg.value>0);
Player memory p = Player(payable(msg.sender),msg.value);
//透过bool true 表示下大
if (flag){
big.push(p);
totalBig += p.amount;
}
else {
small.push(p);
totalSmall += p.amount;
}
return true;
}
function open() payable public returns(bool){
//开奖至少要游戏开始後60秒
require(block.timestamp > nowtime+60);
require(!isFinshed);
//创造出 0-9的变数 0-4为小 5-9为大
uint points = uint(keccak256(abi.encode(msg.sender,block.timestamp,block.number)))%9;
uint i = 0;
Player memory p;
if (points >=5 ){
for (i=0; i<big.length; i++){
p = big[i];
//给赢家 下注本金+照比例分配奖金
p.addr.transfer(p.amount+totalSmall*p.amount/totalBig);
}
}
else{
for(i = 0 ;i< small.length;i++){
p = small[i];
p.addr.transfer(p.amount+totalBig*p.amount/totalSmall);
}
}
isFinshed = true;
return true;
}
}
<<: [Day 16] Facial Recognition - Local Binary Patterns (LBPs)
磁碟空间快满了 好多朋友胡乱将各种档案存储在 MacBook 里,但又没有定期整理资料的习惯。时间...
在专案中我们常常需要扩充功能,因此我们就可以使用插件,所以今天我们就要来学习如何使用 如何使用 可以...
取得Nonce 根据规格书的说明,每次呼叫api前都要先取得Nonce 要取得Nonce也满简单的,...
接着我们要利用PGADMIN连线我们Heroku上的资料库插件 打开PGADMIN,输入昨天输入的密...
这篇开始使用 Tag Helper 来 Render 出需要的 Html 控制项 name,方便在 ...