首页 > 解决方案 > Discord.js 机器人随机回合制战斗

问题描述

所以,我是编码新手,我正在尝试创建一个随机的、基于回合的战斗系统。但是,我认为我在代码中犯了一个错误。它不是随机的,也不是最后一击。我未能找到错误。

if(command === "holmgang"){
    if(args[0] === "help"){
        msg.channel.send("Holmgang is a duel practiced by early medieval Scandinavians. It was a legally recognized way to settle disputes./Anyone offended could challenge the other party to holmgang regardless of their differences in social status. This could be a matter of honor, ownership or property, demand of restitution or debt, legal disagreement or intention to help a wife or relative or avenge a friend.")
    }
    else{
        let user = msg.member.user
        let enemy = msg.mentions.users.first()
        if(enemy){
            msg.channel.send("**You called your enemy forward for a holmgång!**")
            setTimeout(function(){msg.channel.send("**Both of you will arrive on a small island nearby. Only one shall return.**")},5000);
            var i;
            var attacker;
            var waiting;
            var finisherattack = Math.floor(Math.random() * 5);
            var finishing = 0
            var attackmoves = []
            for (i = 1; finishing<= 80 ; i++) {
                finishing = Math.floor(Math.random() * 100);
                var a = i*3
                var b = a*1000
                var time = b + 5000
                if (i%2 == 0) {
                    attacker = user
                    waiting = enemy   
                }
                else{
                    attacker = enemy
                    waiting = user
                }
                var randomattack = Math.floor(Math.random() * 5);
                attackmoves = [attacker + " throws his spear to " + waiting + ", but misses.", attacker + " attacks " + waiting + " with his sword, but " + waiting + "uses his shield to block.", attacker + " swings his sword aiming at " + waiting + "s head. but " + waiting + "dodges.", attacker + " throws his spear to " + waiting + ", but " + waiting + " catches it with his shield.", attacker + " attacks " + waiting + " with his axe, but " + waiting + " blocks it with his shield"]
                setTimeout(function(){msg.channel.send(attackmoves[randomattack])},time);
            }
            if(finishing > 80){
                let winner = attacker
                let loser = waiting
                var finishingmoves = [winner + " throws his spear to " + loser + ", killing him.", winner + " chops " + loser + "s head off with his axe.",winner + " stabs " + loser + " with his sword, killing him.", winner + " kills " + loser + ", with his axe.",winner + " bashes his shield against " + loser + "s head. Then chops his head off with his sword."]
                
                setTimeout(function(){msg.channel.send(finishingmoves[finisherattack])},time); 
            }
        }

输入:

.holmgang @EmoBey09

预期输出:

@EmoBey09 swings his sword aiming at @Phoenixs head. but @Phoenix dodges.
@Phoenix throws his spear to @EmoBey09, but misses.
@EmoBey09 throws his spear to @Phoenix, but @Phoenix catches it with his shield.
@Phoenix attacks @EmoBey09 with his axe, but @EmoBey09 blocks it with his shield
@EmoBey09 chops @Phoenix's head off with his axe.

实际输出:

@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.
@Phoenix swings his sword aiming at @EmoBey09s head. but @EmoBey09 dodges.

标签: javascriptnode.jsdiscord.js

解决方案


不要使用var,const用于以后不会修改的变量和let其他变量。


推荐阅读