使用特定命令“ 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 */
}


如果那时他们还没有回答,那么该回合转到下一位玩家。我不知道如何结合所有这些来制作一个有效的游戏。

最佳答案

您需要找出一种方法来跟踪轮到哪个玩家。在这种情况下,您可以使用布尔值,因为只有两个玩家。如果您希望游戏具有更高的可扩展性,显然您需要使用其他逻辑检查。

在这种情况下

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


当然,这只是处理它的一种方法

09-25 18:26
查看更多