Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用JavaScript制作一个不和谐机器人:尝试在两个玩家之间制作一个wordchain游戏_Javascript_Node.js_Loops_Bots_Discord.js - Fatal编程技术网

用JavaScript制作一个不和谐机器人:尝试在两个玩家之间制作一个wordchain游戏

用JavaScript制作一个不和谐机器人:尝试在两个玩家之间制作一个wordchain游戏,javascript,node.js,loops,bots,discord.js,Javascript,Node.js,Loops,Bots,Discord.js,使用特定命令“shiritori”并标记另一个用户的用户将是player1。被标记的用户将是player2。我已经下载了一个包含大多数字典单词的JSON文件,因此首先,我在这里对其进行了测试,结果似乎是成功的: let usedWords = [] let points = 0 function shiritoriCommand(arguments, receivedMessage) { let word = receivedMessage.content.substr(11)

使用特定命令“shiritori”并标记另一个用户的用户将是player1。被标记的用户将是player2。我已经下载了一个包含大多数字典单词的JSON文件,因此首先,我在这里对其进行了测试,结果似乎是成功的:

let usedWords = []
let points = 0

function shiritoriCommand(arguments, receivedMessage) {
    let word = receivedMessage.content.substr(11)
    fs.readFile('./words_dictionary.json', 'utf8', (err, jsonString) => {
            if (err) {
                console.log("Error reading file from disk:", err)
                return
            }
            try {
                const dictionary = JSON.parse(jsonString)
                if (word in dictionary && !(usedWords.includes(word)) && word.length >= 4) {
                    points = points + word.length
                    receivedMessage.channel.send('The word is in the dictionary! You have a total of ' + points + ' points!')
                    usedWords.push(word)
                } else {
                    receivedMessage.channel.send('Either the word is not in the dictionary, it is too short or it has already been used. You gain no points.')
                }
        } catch(err) {
                console.log('Error parsing JSON string:', err)
            }
        })

}
当前程序接收接收到的消息并用substr()分隔单词。然后它阅读字典,看看是否在里面找到了这个词。如果是,它会将单词推入已用单词的数组中,这样就不能再次使用它来获得分数。点是单词长度(必须为4或更多,否则将忽略)。使用有效单词时显示总数

然而,我发现将2名球员纳入其中很有挑战性。我受到了Pokecord决斗的启发,如何区分两位玩家的话,以及必须做什么?我最初是这样安排的:

let player1 = receivedMessage.author
let player2 = receivedMessage.mentions.members.first()
除此之外,我希望每个球员都有15秒的时间段。当任何一个玩家得了200分时,游戏停止。现在我可以通过while循环来管理它:

points1 = 0
points2 = 0

while (points1 <= 200 || points2 <= 200) {
/* Do I use set interval and duplicate the first function for each player
 and assign their respective points */
}
points1=0
点2=0

而(点1你需要找出一种方法来跟踪轮到哪个玩家。在这种情况下,你可以使用布尔值,因为只有两个玩家。如果你想让游戏更具可扩展性,你显然需要使用不同的逻辑检查

在这种情况下,如果

(player1Turn === true) {
// do some stuff
}

当然,这只是处理它的一种方法。你需要找出一种方法来跟踪轮到哪个玩家。在这种情况下,你可以使用布尔值,因为只有两个玩家。如果你想让游戏更具可扩展性,你显然需要使用不同的逻辑检查

在这种情况下,如果

(player1Turn === true) {
// do some stuff
}
当然,这只是处理问题的一种方法