-3

I have an issue random number generating. I've made an AI for game "Guessing number game" and it works just fine until the Player hit number lower than searched number. After that Math.random(); may and it is very likely to generate negative numbers. If player hit the number lower than searched number. (Range of numbers gessing by AI is defined by two nums (the highest from array of nums lower[]; than searched and lowest from array higher[]; than searched).

Here is whole code on GitHub since its too large to put it in here: https://github.com/lkobylanski/Guessing-number-game-1P-AI/blob/master/PvAv1.2.js

My question is, how do I properly debug application to find this kind of issue. I was using console.log(functionName) to display functions one by one in order they are executed. I've made alerts that are displaying arrays, arrays[0], arrays.length but it didn't help me and I got stuck. I have no more ideas what I can do.

Function starts to generate negatvies during executting this part of code:

else if(higher_l > 0 && lower_l > 0)
    { //losuje od 0 (ZERA) do (max-min+1) i dodaje + min;
        guessed = Math.floor(Math.random()*((higher[0])-((lower[0]+1)+1))+(lower[0]+1));

        if(guessed > drawn)
        {
            c2--;
            document.getElementById("notes2").innerHTML = p2 + " remaining chances: " + c2;
            higher.push(guessed);
            higher_l = higher.length;
            higher.sort(function(a, b) {return a-b;});
            resultH = resultH + guessed + " ";
            document.getElementById("higher").innerHTML = "Proper number is lower than: " + resultH;

            counter = setTimeout(checkCounters, 100);

            switch_trigger = setTimeout(switchPlayers, 2000);

            return;
        }
        else if(guessed < drawn)
        {   
            c2--;
            document.getElementById("notes2").innerHTML = p2 + " remaining chances: " + c2;
            lower.push(guessed);
            lower_l = lower.length;
            lower.sort(function(a, b) {return b-a});
            resultL = resultL + guessed+ " ";
            document.getElementById("lower").innerHTML = "Proper number is higher than: " + resultL;

            counter = setTimeout(checkCounters, 100);

            switch_trigger = setTimeout(switchPlayers, 2000);

            return;
        }
        else if(guessed == drawn)
        {
            p2w++;
            document.getElementById("all_wins").innerHTML = p1 + " wins: " + p1w + "/" + wins + "<br/>" + p2 + " wins " + p2w + "/" + wins;

            if(p2w < wins)
            {
                document.getElementById("msg").innerHTML = "AI found the number: " + guessed + "first!<br/>" + currentp + " and gets a point!";
                document.getElementById("next").disabled = false;
            }
            else if(p2w == wins)
            {
                alert("Winner is " + currentp + "!");
                document.getElementById("msg").innerHTML = "AI found the number: " + guessed + " <br/> Player: " + currentp + " reached required number of: " + wins + " wins first!";
            }

            end = setTimeout(endMatch, 100);

            return;
        }
    }

I know that the code itself could be a way better than this but I want to improve it when the whole thing will be working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Luke_Nuke
  • 461
  • 2
  • 6
  • 23
  • You should do some step-by-step debugging. – Oliver Charlesworth Jun 11 '17 at 11:11
  • I was trying used console.log after every function that is involved to guessing nums and extending arrays etc. also create alerts that shows both arrays, arrays[0], arrays.length. Moves: AI:70, Me: 100, AI:71, Me: 1, AI:-25 (on screens you can see arrays were made and sorted properly: https://gyazo.com/6dc763cee81651002c6478ae3913e81e here screen for AI next move: https://gyazo.com/65457da3e13074b73953e3c14bc99984 – Luke_Nuke Jun 11 '17 at 11:41
  • I read the Math function like 100 times: guessed = Math.floor(Math.random()*((higher[0])-((lower[0]+1)+1))+(lower[0]+1)); Math function and arrays are ok (or it just seems to me) If player will pick nums **higher** than searched whole game this issue won't occur even if AI picsk nums lower than. I'm literally out of ideas. – Luke_Nuke Jun 11 '17 at 11:45
  • `Math.random()` per se isn't generating negative numbers. It is what your code is doing to those random numbers that is yielding negatives. Thus, the title to your question seems inaccurate. – John Coleman Jun 11 '17 at 14:46
  • @JohnColeman You're right ill fix that, thanks. – Luke_Nuke Jun 11 '17 at 15:45
  • @OliverCharlesworth I need to say thank you, you were right. I spend a little longer with firefox debugger and I've learn a lot useful stuff! I've found the issue thanks to the debugger. It was stupidly simple... I've learned a lesson :) I didn't parseInt values from HTML so the nums guessed by Math.random were ints but provided by player were all strings. In debugger they were as "100", "25". I was so fixated searching issue in existing code, that i totally forgot to check if maybe i forgot about something. Thank you all guys :) – Luke_Nuke Jun 11 '17 at 18:47
  • @rcgldr: we don't use [solved] devices in titles here, or edit solutions into questions - especially at the start of a question. If you see this, you can roll it back, or ask the OP to move it into an answer proper. Thanks! – halfer Jun 12 '17 at 20:30
  • @rcgldr: there's a few thousand [solved] questions on the site, but we still should discourage it - there's a fully-fledged acceptance system that should be used instead. I think posters do it because they know how to edit, but they don't know to click the Answer Your Question button to reveal an answer box. I don't understand what you mean about downvoting, but I don't think it could be a sufficient reason for leaving answers appended (or worse, prepended) to questions. – halfer Jun 12 '17 at 22:06
  • That's not the primary message from that link at all @rcgldr. The message is "don't ask off-topic or bad questions". FWIW, I added an answer to that Meta question at the time, though it was not well received. – halfer Jun 13 '17 at 08:29
  • @halfer - I deleted my prior comments. In the future, I'll avoid editing any questions or answers that aren't mine to avoid issues I'm unaware of. – rcgldr Jun 13 '17 at 13:42
  • I wouldn't want to discourage you from editing, @rcgldr - in fact one probably cannot learn about the _Meta_ nuances without getting stuck in and making the odd mistake. I make them too (usually either over-editing or introducing new errors, to the frustration of OPs who want their chatty mess to be preserved _verbatim_!). So, no worries, it's all good. – halfer Jun 13 '17 at 14:57
  • Ok, to be clear. If I find solution my self is it ok, to just click answer your question and i shouldn't add tags like SLOVED? :) I'm sorry for the mess, I didn't mean to. It was my first post ever here. Thank you for your indulgence! – Luke_Nuke Jun 14 '17 at 10:06

1 Answers1

-1

(Posted on behalf of the OP).

After spending a little longer with debuger I've found the source of the issue. Nums provided by the Player were not parseInt(); so they were strings while nums from Math.random(); where ints. After parsing everything is working. Thanks to every one, I've learned the lesson! :)

halfer
  • 19,824
  • 17
  • 99
  • 186