import { useState, useEffect } from 'react';
import { Dice1, Dice2, Dice3, Dice4, Dice5, Dice6 } from 'lucide-react';
export default function SnakeLadderGame() {
const [playerPosition, setPlayerPosition] = useState(1);
const [diceNumber, setDiceNumber] = useState(1);
const [isRolling, setIsRolling] = useState(false);
const [message, setMessage] = useState('');
// تعریف مارها و نردبانها
const snakesAndLadders = {
16: 6, // مار
47: 26, // مار
49: 11, // مار
56: 53, // مار
62: 19, // مار
87: 24, // مار
93: 73, // مار
98: 78, // مار
4: 14, // نردبان
9: 31, // نردبان
21: 42, // نردبان
28: 84, // نردبان
36: 44, // نردبان
51: 67, // نردبان
71: 91, // نردبان
80: 100 // نردبان
};
const rollDice = () => {
if (isRolling) return;
setIsRolling(true);
setMessage('');
// انیمیشن تاس
let rolls = 0;
const rollInterval = setInterval(() => {
setDiceNumber(Math.floor(Math.random() * 6) + 1);
rolls++;
if (rolls >= 10) {
clearInterval(rollInterval);
const finalRoll = Math.floor(Math.random() * 6) + 1;
setDiceNumber(finalRoll);
movePlayer(finalRoll);
setIsRolling(false);
}
}, 100);
};
const movePlayer = (spaces) => {
const newPosition = playerPosition + spaces;
if (newPosition > 100) {
setMessage('نمیتوانید بیشتر از ۱۰۰ حرکت کنید!');
return;
}
setPlayerPosition(newPosition);
if (snakesAndLadders[newPosition]) {
setTimeout(() => {
const isSnake = snakesAndLadders[newPosition] < newPosition;
setMessage(isSnake ? 'آخ! مار شما را گزید!' : 'به به! از نردبان بالا رفتید!');
setPlayerPosition(snakesAndLadders[newPosition]);
}, 500);
}
};
const DiceIcon = ({ number }) => {
const icons = {
1:
موقعیت شما:
{playerPosition}
{message}