Game Design, Programming and running a one-man games business…

Algorithmic dilemma

Here is a puzzle:

I have lists of neutral comments, positive comments, and negative comments. Any of the lists could be empty or of any size.

I wish to select roughly 2 comments in total, sometimes 1, sometimes 3. There is a chance that I will only have a total of 1 comment to choose from anyway (in all 3 lists)

I also have a value that is positive or negative. If it’s positive I need to ensure not all my comments I select are negative, given that there are some positive ones. If its negative, I need to do the reverse.

How do you do that?

If the mere thought of it makes your head explode, you will never be a programmer. If you can see immediately how to do it, in explicit steps, efficiently and accurately. Then you may be a very good programmer one day. Hopefully I’ll have sussed it before anyone posts a good reply :D

15 thoughts on Algorithmic dilemma

  1. My thought on this is:
    a1) if happy select a positive (if any)
    a2) if very happy select another positive (if any)
    b1) if sad select a negative (if any)
    b2) if very sad select another negative (if any)
    c1) if neutral select a neutral (if any)

    You now got 0, 1 or 2 of an aligned “happiness”

    3) if there are any unselected comments, select one random

    You now got 1, 2 or 3 comments, where at least one is right. This can still mean that you get a negative comment in a wholly positive context, but at least the odds are low.

    Hope this fits your own views, or at least makes sense. Could supply (Java-like) pseudo-code if that would help anyone. :)

  2. Hi Cliff,

    What do you mean by “I need to do the reverse”? That if the number is negative you need to only select negative comments or that you don’t need only positive comments if there are negative ones?

    Maybe a bit of contextual info would help here :)

  3. I presume that this value represents how happy the person is. If it is positive, the person is generally happy, 0 neutral and negative is generally unhappy. The higher the positive value, the happier the person is, and the lower the negative value, the more unhappy the person is.

    Now I wonder: if your value is positive, are you guarunteed to have at least one comment in your positive list? Or may you, say, only have comments in your negative list? And if you only have comments in your negative list but a positive score, what then? Display no comments at all? Or display a negative comment? Some clarification here would help. Obviously same question visa versa.

    Taking what you said about a positive value meaning that there must be a positive comment, presumably your first port of call is thus to select a comment (at random?) from the positive list. This will guaruntee you a positive comment (assuming that there are any). Now what you do next depends on whether the strength of the number (how high it is) has any impact on what comments are selected. If this does not matter, then you could simply select a comment at random from all of the lists put together. If you want to vary the amount of comments output, then having got your definite positive comment, you could then randomly select a value between 0 and 2 to decide how many comments to add to your initial comment, perhaps bias towards receiving the answer of “1” more often than “0” or “2”, thus meaning that more often than not the person receives two comments. The same would happen in reverse with negative comments.

    I hope this makes sense.

  4. Indeed, its the case that the value is overall happiness. The problem is, that the overall happiness is determined by lots of factors, but there are factors that may or may not affect happiness in a big way, but which can be commented on.
    You invite me to the cinema, with you and dave. I LOVE the cinema, and the film was awesome! BUT you are in a crap mood and dave is irritating. You werent in THAT bad a move, and dave wasn’t THAT irritating, but regardless, thats 2 negatives about the event.
    So the problem is, I have 1 positive (love the cinema) and 2 negative potential comments, but overall, I had a good time. so it might say:
    “I loved tonight! but you were miserable and dave is irritating.”
    This clashes badly, so I have to do code that makes sure if I am happy, I ensure that my positive comments outweigh the negative, even if there are more negative things to say than positive.

    The fact that there may be zero negatives or positives, and there are some neutrals just makes it horribly more complex :D

  5. Hmm… maybe I’ve got the wrong end of the stick, but couldn’t you do a piece of code that where each factor on your happiness has a numerical value attached to it. The game then averages the number out and while you can get a positive and negative comment, the game is restrained in how positive or negative it can be.

    So using your example what you could have is:

    (100=happy 0=unhappy)

    Loved Movie = 80
    Crap Mood = 20
    Dave = 30

    Average = 43

    So now the comments are restricted to be around the neutral mark, not at both ends.

    As I said, maybe I’ve got the wrong end of the stick…

  6. Hmmm, first you would count up how many comments there are in total. If it’s positive/negative comment.

    If you need it to be positive and you have a negative or neutral then get a comment from the positives column, otherwise get it from neutral if there are no no positive, and lastly get it from negative.
    If you need negative and you have a positive or neutral then get a comment from negative, then neutral then positive.
    If you don’t need any particular leaning, you could get a comment from the opposite list to one you already have, if available, or you could just get one at random.

    Then you have your second one balanced by the first one. It probably needs refinement though.

  7. Can i ask why a particular comments list would be empty? Will you not be using a set list of comments for each category?

  8. I put together a (slightly) commented pseudo/javaish code for what I meant above. Hope it turns out nice in the comments, and that it is of any help.

    static int VERY_HAPPY = 3;
    static int VERY_SAD = -3;

    public List pickComments(int happyness, List pos, List mid, List neg ) {
    List temp = new List();
    List selected = new List();

    //Shuffle the individual comment categories

    if ( happyness > 0 ) {
    //If happy, add one positive, or two for very happy – if there are.
    if ( pos.size() > 0 ) {
    if ( happyness > VERY_HAPPY && pos.size() > 0 )
    } else if ( happyness 0 ) {
    if ( happyness 0 )
    } else {
    //If neither happy or sad, add one neutral comment – if any.
    if ( mid.size() > 0 ) {
    //Finally add another comment if there are any left.
    //If very sad, don’t add positive comments unless there were no negative or neutral ones
    //If very happy, don’t add negative comments unless there were no positive or neutral ones
    if ( happyness VERY_SAD || (selected.size() == 0 && mid.size() == 0 ) )


    if ( temp.size() > 0 )

    return selected;

  9. This is a fun game, and I think it should continue as a series.

    So each comment has a value in the range 0 to 100, indicating the amount of influence it has, plus a -1, 0, 1 value to indicate whether it is a happy or sad comment. The overall happiness is indicated by the total of influence*happyorsad for all comments. Note that neutral comments end up as zero but doesn’t make sense that they would have a value that contributes to overall happiness.

    So now we can traverse this list in order of happyorsad, and/or order by influence. So if you wanted the most significant comments, starting with positive, you would order by influence first, then take the first three and order by happyorsad, in the order positive to negative (if the overall comment value was positive) or vice versa.

    So if you want to make sure neutral comments appear, then just give them a value.

  10. I wonder whether the problem is just about how you are expressing the comments. Let’s take the example you gave in your earlier comment, where someone has had a great time,but has a few negative things to say. You phrased this as “I loved tonight! but you were miserable and dave is irritating.”

    How about, where the score is positive but there are only negatives, we list the negatives first, perhaps even qualified to make them sound not as bad, so – “You were a little miserable tonight, and Dave was a little irritating, but that couldn’t spoil an otherwise great night!”

    The opposite would happen if your score was very negative but you only had positive comments: “You were in a reasonably good mood tonight, and Dave was quite interesting, but it was still a rubbish night.”

    I would code it something like this. Pull out of the lists two comments at random. Now compare them to the overall value. If they are both opposite to the overall value (they are both negative but the score is positive), do as I said above – put them first, then use something like the “but that couldn’t spoil an otherwise great night!” that I suggested above. If one is positive but the other negative, and, say, the score is positive, then in a similar way put the negative point first, then “but” and the positive point, then you can finish off with “and overall it was a great night”, and so on (eg. “the film was rubbish, but Dave was hilarious, and overall it was a great night”). I think you will need to experiment a little to get the correct syntax, but I think that it should be possible to give negative comments despite an overall positive, or visa versa, if you phrase it right.

  11. Theres been tons of good ideas here. I have actually coded that bit now and moved on, but I do like some of the suggestions, and my own implementation does feel a bit bodged.
    There are some additional considerations, such as not having too many of the neutral comments in general, because they are from a central pool and can be repetitive.
    I may well revisit this code before shipping and implement some of this.

  12. Assuming there is a happyness rating (0-1) and a chattyness rating (0-1) we get:

    How about a more sophisticated system where comments aren’t just ranked -1,-,+1. Give them a rating so that the most positive comments only come out with extremely good outcomes.

    Assuming we have two factors. An chattyness factor for the character, and a happyness rating resulting from recent history, I’d consider something like:

    comments = 2 * (chattyness + random * 0.6)

    if comments>=1
    select comment based purely on happyness rating ;
    if comments>=2
    extend comment based on (happiness rating + random – 0.5)/2 ;
    if comments>=3
    extend comment based on (happiness rating + random – 0.3)/2 ;

    If there’s only one comment we get one which pretty much reflects the actual happiness rating.

    If there’s a second we get one centered around the happiness rating, but not guaranteed to match.

    If we get a third comment it’s biased 70/30 towards positive comments, because chatty folks (there’s three comments) are usually more positive than negative.

    Just my sunday thoughts..

Comments are currently closed.