Разработка игры Mines на блокчейне
Mines (или Minesweeper казино вариант) — игра на поле N×N где скрыты мины и безопасные ячейки. Игрок открывает ячейки одну за одной, каждая безопасная ячейка увеличивает multiplier. Можно забрать выигрыш в любой момент или продолжать рисковать — пока не попадётся мина. Элемент выбора делает игру значительно более вовлекающей чем Dice.
Математика Mines
Стандартное поле: 5×5 = 25 ячеек. Пусть mineCount = 5 (20% шанс мины на каждой открытой ячейке).
Вероятность безопасно открыть k ячеек:
P(k безопасных) = ∏(i=0 to k-1) [(25 - mines - i) / (25 - i)]
При 5 минах, открыть 1 ячейку безопасно: (25-5)/25 = 80%. Открыть 2 подряд: 80% × (19/24) = 63.3%. Открыть 5 подряд: ~33%.
Multiplier при k открытых ячейках = 1 / P(k) × (1 - houseEdge).
Это создаёт экспоненциально растущий риск/награду — именно это делает Mines психологически захватывающим.
Smart contract: reveal pattern
Ключевая сложность Mines на блокчейне: нельзя хранить позиции мин on-chain до завершения игры (пользователь увидит их). Решение: commit-reveal.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BlockchainMines is VRFConsumerBaseV2Plus {
struct Game {
address player;
uint8 fieldSize; // 5 для 5x5
uint8 mineCount;
uint256 betAmount;
uint256 currentMultiplier; // в basis points
uint8 openedCells;
uint256 vrfSeed; // получен из VRF, хранится зашифровано до кассаут
bytes32 minesSeedHash; // hash(vrfSeed) — публично
GameStatus status;
bool[25] openedCellMap; // какие ячейки открыты
}
enum GameStatus { WAITING_VRF, ACTIVE, CASHED_OUT, BUSTED }
mapping(uint256 => Game) public games;
mapping(address => uint256) public activeGame;
// Таблица multipliers: [fieldSize][mineCount][openedCells] → multiplier
// Предрассчитанная off-chain, загружена в контракт
mapping(uint8 => mapping(uint8 => mapping(uint8 => uint256))) public multiplierTable;
function startGame(uint8 mineCount) external payable returns (uint256 gameId) {
require(activeGame[msg.sender] == 0, "Game already active");
require(mineCount >= 1 && mineCount <= 24, "Invalid mine count");
require(msg.value >= MIN_BET, "Bet too low");
gameId = ++gameCounter;
games[gameId] = Game({
player: msg.sender,
fieldSize: 5,
mineCount: mineCount,
betAmount: msg.value,
currentMultiplier: 10000,
openedCells: 0,
vrfSeed: 0,
minesSeedHash: 0,
status: GameStatus.WAITING_VRF,
openedCellMap: [false, false, /*...*/ false],
});
activeGame[msg.sender] = gameId;
// Запрашиваем VRF для генерации seed расположения мин
uint256 vrfRequestId = _requestVRF();
vrfToGame[vrfRequestId] = gameId;
}
function fulfillRandomWords(uint256 requestId, uint256[] calldata randomWords)
internal override
{
uint256 gameId = vrfToGame[requestId];
Game storage game = games[gameId];
// Сохраняем seed — но не раскрываем позиции мин игроку
// Шифруем через xor с secret key (можно раскрыть после игры)
game.vrfSeed = randomWords[0]; // в production — шифровать
game.minesSeedHash = keccak256(abi.encodePacked(randomWords[0]));
game.status = GameStatus.ACTIVE;
emit GameStarted(gameId, game.minesSeedHash);
}
function openCell(uint256 gameId, uint8 cellIndex) external {
Game storage game = games[gameId];
require(game.player == msg.sender, "Not your game");
require(game.status == GameStatus.ACTIVE, "Game not active");
require(cellIndex < 25, "Invalid cell");
require(!game.openedCellMap[cellIndex], "Already opened");
game.openedCellMap[cellIndex] = true;
// Определяем есть ли мина в этой ячейке
bool isMine = _isMine(game.vrfSeed, game.mineCount, cellIndex, game.fieldSize);
if (isMine) {
game.status = GameStatus.BUSTED;
activeGame[msg.sender] = 0;
// Раскрываем все мины
uint8[] memory minePositions = _getMinePositions(game.vrfSeed, game.mineCount);
emit GameBusted(gameId, cellIndex, minePositions);
} else {
game.openedCells++;
game.currentMultiplier = multiplierTable[game.fieldSize][game.mineCount][game.openedCells];
emit CellOpened(gameId, cellIndex, game.currentMultiplier);
}
}
function cashout(uint256 gameId) external {
Game storage game = games[gameId];
require(game.player == msg.sender, "Not your game");
require(game.status == GameStatus.ACTIVE, "Game not active");
require(game.openedCells > 0, "No cells opened");
game.status = GameStatus.CASHED_OUT;
activeGame[msg.sender] = 0;
uint256 payout = (game.betAmount * game.currentMultiplier) / 10000;
payable(msg.sender).transfer(payout);
emit GameCashedOut(gameId, game.openedCells, game.currentMultiplier, payout);
}
// Определение позиций мин из seed
function _getMinePositions(uint256 seed, uint8 mineCount)
internal pure returns (uint8[] memory positions)
{
positions = new uint8[](mineCount);
bool[25] memory placed;
uint256 minesPlaced = 0;
uint256 i = 0;
while (minesPlaced < mineCount) {
uint8 pos = uint8(uint256(keccak256(abi.encodePacked(seed, i))) % 25);
if (!placed[pos]) {
placed[pos] = true;
positions[minesPlaced] = pos;
minesPlaced++;
}
i++;
}
}
function _isMine(
uint256 seed,
uint8 mineCount,
uint8 cellIndex,
uint8 fieldSize
) internal pure returns (bool) {
uint8[] memory minePositions = _getMinePositions(seed, mineCount);
for (uint i = 0; i < minePositions.length; i++) {
if (minePositions[i] == cellIndex) return true;
}
return false;
}
}
Multiplier таблица
Multipliers предрассчитываются математически и загружаются в контракт при деплое. Пример для 5×5 поля с 3 минами:
| Открытых | Multiplier (1% edge) |
|---|---|
| 1 | 1.14x |
| 2 | 1.32x |
| 3 | 1.56x |
| 5 | 2.22x |
| 10 | 6.60x |
| 15 | 27.3x |
| 22 | 990x |
Анимация и UX
Mines требует хорошей визуальной обратной связи:
- Взрыв при попадании на мину
- Постепенное свечение/усиление поля при успешных ячейках
- Нарастающее напряжение в sound design
- Мгновенная кнопка Cashout всегда видна
Разработка Mines: смарт-контракт + VRF + frontend — 4-5 недель. Математически корректная multiplier таблица и UI с анимацией включены.







