// 定義映射
mapping(uint => string) public keyValueMap;
// require():用於檢查條件是否滿足,如果不滿足則拋出錯誤並中止交易
require(condition, "Error message");
array
1.動態大小: 裡面不用放數字, 可以動態新增
2.必須透過push function,不斷新增array後面的值
3. .length可以查看他的長度
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
contract guessnumber {
uint256 number;
uint256 creator;
uint256 answer;
uint256 totalParticipants = 500;
uint256 maxNum = 200;
uint256 minNum = 1;
mapping(address => uint) public guessNumMap; mapping(uint => string) public keyValueMap;
mapping(address => bool) public isVip;
mapping(address => bool) public isGuess;
address[] vipList;
constructor(address[]memory whitelist) {
creator = msg.sedner; // 將當前的發送者(部署者)地址賦值給creator
for(uint256 i = 0; i < whitelist.length; i++){
address vip = whitelist [i];
vipList.push(vip);
isVip [vip]= true;
}
}
function guessNum(uint256 num) public {
require(isVip[msg.sender] == true, "not a vip.");
require(isGuess[msg.sender] == true, "already guess.");
require(num >= minNum && maxNum <= maxNum, "over range.");
guessNumMap[msg.sender] = num;
isGuess[msg.sender] = true;
}
function setAnswer(uint256 _answer) public {
require(msg.sender == creator, "not a creator.");
require(_answer >= minNum && maxNum <= maxNum, "over range.");
answer = _answer;
}
function sendReward() public {
require(msg.sender == creator, "not a creator.");
//1. 取得用戶猜什麼數字
for(uint256 i = 0; i < vipList.length; i++){
address user = vipList[i]; //mapping - key
uint256 guessNum_ = guessNumMap[user];
//2. 取得答案 -> 直接呼叫
//3. 比對用戶數字和答案
//4. 比中發錢
//判斷式
if(guessNum_ == answer){
payable(user).transfer(0.1ether);//0.1ETH
}
}
}
function deposit() external payble {
// 1 ether = 10^18 wei
}
}