简体   繁体   中英

Numbers from inside a string without regex?

I'm making a bot for a gaming chatroom with some friends, but I've hit an impasse. Is there a reliable way to get numbers from inside a string of text that won't completely break an inexperienced script kiddy's brain? Here's the best I've been able to come up with so far, variables simplified slightly for illustration's sake:

var k = [0];
function dieRoll(m,n) {
    for(i = 0; i < m; i++) {
        k[i] = Math.floor(Math.random()*n)+1;
    }
}

var m = text[5];
var n = text[7];
if (text === 'roll '+m+'d'+n) {
    dieRoll(m,n)
    console.log(k);
}

The biggest problem as-is is that it's limited to single-digit input.

EDIT: Looping through the text looking for integers is exactly the kind of thing I'm looking for. I don't have much experience with programming, so I probably tend to end up with overly complicated and confusing messes of spaghetti code that would embarrass anyone remotely professional. As for the format of the input I'm looking for, "roll [number of dice]d[highest number on the dice]". For anyone who doesn't know, it's the notation most tabletop rpgs use. For example, "roll 2d6" for two normal six-sided dice.

EDIT: It's not that I'm necessarily against regex, I just want to be able to understand what's going on, so that if and when I need to edit or reuse the code it I can do so without going completely insane.

EDIT: Thank you all very much! split() seems to be exactly what I was looking for! It'll probably take some trial and error, but I think I'll be able to get her working how she's supposed to this weekend (Yes I call my bots 'she').

Basically, you need to look at the format of the input you're using, and identify certain facts about it. Here are the assumptions I've taken based on your question.

1) The "roll" command comes first followed by a space, and 2) After the command, you are provided with dice information in the form x d y .

Here's something that should work given those constraints:

 function getRollParameters(inputCommand) { var inputWords = inputCommand.split(' '); //Split our input around the space, into an array containing the text before and after the space as two separate elements. var diceInfo = inputWords[1]; //Store the second element as "diceInfo" var diceDetails = diceInfo.split('d'); //Split this diceInfo into two sections, that before and after the "d" - ie, the number of dice, and the sides. //assign each part of the dicedetails to an appropriate variable var dice = diceDetails[0]; var sides = diceDetails[1]; //return our two pieces of information as a convenient object. return { "dice": dice, "sides": sides }; } //a couple of demonstrations console.log(getRollParameters("roll 5d8")); console.log(getRollParameters("roll 126d2"));

Effectively, we're first splitting the string into the "command", and the "arguments" - the information we want. Then, we split our arguments up using the "d" as a midpoint. That gives us two numbers - the one before and the one after the d. Then we assign those values to variables, and can use them however we like.

This obviously won't deal with more creative or flexible inputs, and isn't tested beyond the examples shown but it should be a decent starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM