5 kyu

80's Kids #6: Rock 'Em, Sock 'Em Robots

968 of 2,495NateBrady23
Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • m1dn1ght.exe Avatar

    Today, I want to share a pivotal moment in my coding journey that has transformed my understanding and appreciation of while loops. Not long ago, I found myself grappling with the complexities of these seemingly simple constructs. My mental models felt muddled, and I often struggled to visualize how they operated within my code. The frustration was real, and I was yearning for clarity.

    Then came THIS coding challenge that introduced me to the beauty of nested loops. As I engaged with the problem, something magical happened. One line of code led to another, and with each nested loop, I began to unravel the intricate dance of logic and flow. The once-confusing world of while loops became a vivid tapestry in my mind, each loop representing a vital thread in the action sequence I was constructing.

    In the process, I discovered that while loops don’t just execute a block of code—they echo the rhythm of our thoughts and actions. They represent a series of choices, each one leading us closer to a solution. As I worked through the challenge, I saw my ideas take shape and my confidence grow. I no longer viewed loops as mere constructs; instead, they became a harmonious sequence of actions, each contributing to a greater purpose.

    Now, I possess a clear, structured mental model of while loops and their nested counterparts. It’s as if a fog has lifted, revealing a pathway of logic that was once obscured. This experience has not only enhanced my coding skills but has also inspired me to embrace challenges with renewed enthusiasm. Each loop now symbolizes a journey, a series of decisions leading to discovery.

    Thank you, nested loops, for illuminating my path and reshaping my understanding. You've not only changed my approach to coding but have also fueled a passion that drives me to explore and learn even more. Here’s to the beauty of loops and the clarity they can bring to our minds!

  • JiPiBi Avatar

    Hi Just a remark : robots are not objects as said in the Details, but dictionnaries : code is less simple and if they were objects , we could have written a method for the fight

  • RileyHunter Avatar

    I've forked and done a major rework of this kata, waiting for review here:

    https://www.codewars.com/kumite/651df75aefd0b637976a502a

    Included:

    • Enabled Node v18.x
    • Refactored to use Chai/Mocha test assertions
    • Added a fixed test case with uneven tactics lengths where both lengths are > 0
    • Replaced the old random test generator which only randomised health and speed with one that:
      • Randomises health and speed
      • Samples from a broader and more varied set of tactics for each fight
      • Generates robots with variable numbers of tactics, rather than re-using identical tactics for every robot in every random fight
    • Fixed the (obviously) broken reference solution; the only reason it appeared to work was a paucity of tests
    • Updated the description a touch

    I believe this addresses all open issues for JS on this kata. I also believe it's going to invalidate a few existing solutions, but (hopefully) that's only because neither they nor the old test harness actually fit the requirements.

  • conch07 Avatar

    This comment has been hidden.

  • saudiGuy Avatar
  • PabloMatMar Avatar

    You must add a test that forces you to continue rotating turns even if one of the robots does not attack. In this example must be win Robot2, but if you only change round when it was attacked had been produced, in that case robot 2 only excute one of all attacks, and how his attack is less he lost. My initial code worked whitout change round if attack is not produced and pass the all test when i tryed 2 or 3 times.

    Add a test that looks like this example and returns that robot two won. Don't leave it to chance...

    fight({
        "name": "Robot1",
        "health": 100,
        "speed": 200,
        "tactics": ["supermissile"]
    },
        {
            "name": "Robot2",
            "health": 100,
            "speed": 0,
            "tactics": ["missile","missile","missile"]
        },
        {   "suppermissile":40,
            "missile": 20
        })" => 'Robot2 has won the fight.'
    
    
  • laurelis24 Avatar

    Spent good hour because of 1 spelling mistakee.. Also, like others mentioned. Felt like something is wrong with random test. But this was a nice kya.

  • teapot00222 Avatar

    Really enjoyed this Kata - thanks!

  • benjaminzwhite Avatar

    Thanks for this kata, it was a nice challenge with a great theme! I used it to practice Python classes but some of the short solutions are very interesting also.

  • ejini战神 Avatar
    • Node 18 (mocha + chai) should be enabled (Refer this and this for more info)

    • Ruby 3.0 should be enabled (Refer this & this for more detail)

  • nicholas1301 Avatar

    My solution passes all of the 100+ randomized tests, but fails on fixed test #7, which I suspect is the same one user szpakusik posted 2 years ago (see a few comments below). Don't know how to access the test parameters, but the output was 'Rocky has won the fight.' should equal 'Missile Bob has won the fight.'

  • dicodiaz Avatar

    This comment has been hidden.

  • PaulinCrtn Avatar

    JAVASCRIPT

    TESTS Fight 3: One or both robots have no tactics Fight 4: Everything is the same.

    ERROR Expected: ''The fight was a draw.'', instead got: ''The fight was a draw''

  • B1ts Avatar

    Possible duplicate of issues below, but reference solution might have a hole in it. Also, tests are vulnerable to input modification.

    Posting an example of a failing test below:

  • dicodiaz Avatar

    Some random cases are wrong; I submitted the same code several times and the 3rd time it passed all the tests, and I can't find any obvious mistakes in it.

  • Sheffboyrd Avatar

    So I'm not sure why test#4 should result in a draw. If everything is the same for both robots then robot1 should attack first - so this robot should win. Only case of a draw in this situation would be if they both have enough health to be above 0 health after all the attacks are done.

    Can someone explain how I am not understanding? Thank you!

  • Beefy1 Avatar

    Anyone else got stuck on timeout execution error (python) and tried everything to fix it but couldn't?

  • Machireddy  Avatar

    Add "Space ray" in tactics for 6th Fixed test. else mention in Kata Description to store tactics for future use.

  • TM7 Avatar

    Python: I passed all tests except one. It's the the third one from the fixed tests.

    • 'The fight was a draw.' should equal 'Missile Bob has won the fight.'

    Weird cause after each attack i check if somebody has won (and if somebody reached health==0, i print the other guy won). Please check the test case. Thanks :)

  • mrpow92 Avatar

    Need help. (Python) I pass every Test but 1. The Robots do have the same Health, the same Attack(s), the same speed. My function returns a draw but expected is Robot 1 to win. I dont get it. Is this just a faulty test?

    #Roboter 1 ['space ray'] r1tac 100 r1.hp 21 r1.speed #Roboter 2 ['space ray'] r2tac 100 r2.hp 21 r2.speed

    like I said the Test Failes with something like "expected 'Bob won the fight.'"

  • mhj Avatar

    Can't pass fixed tests 6 and 7. My code passes the rest of the tests (fixed/random) just fine. Is there any way to see what values are being passed into my functions?

    I'm not sure how to troubleshoot, as I'm only getting "'The fight was a draw.' should have been 'Rocky/Missile Bob has won the fight'" as a test result. Any ideas?

  • laternader Avatar

    Out of curiosity, does this kata encourage the use of OOP?

  • chucky-1 Avatar

    Good kata!

  • costagavras Avatar

    Great kata with a random error in the tests as flagged by many.

  • ilya_plotnikov Avatar

    It's absolutely brilliant! Brings you right back to 80s. Great you have many more of them.

  • shikui Avatar

    In the JavaScript version, some of the random test case may produce a wrong "Expected"?

    May the author fixing that?

  • kathrin298 Avatar

    In the Ruby version, I found a typo in test #6: Tactic order matters, where missile is spelled like missle. The task is not specific what happens when a tactic is not in tactic hash. I did not use that tactic if not found, however, then the wrong robot wins according to the test.

    Anyone else had the same issue?

  • SuperCodePascal Avatar

    There might be a problem with the kata. I failed some of the random tests. So I attempted my code multiple times without changing anything. The fifth attempt was a success...

    I can't find any obvious mistake in my solution, so I'm not sure if there is a bug in the random testing.

  • szpakusik Avatar

    { name: 'Rocky', health: 10, speed: 59, tactics: [ 'punch',

     'punch',
     'laser',
     'missile',
     'punch',
     'laser',
     'missile',
     'punch',
     'laser',
     'missile',
     'punch',
     'laser',
     'missile' ] }
     
    

    { name: 'Missile Bob', health: 25, speed: 18, tactics: [ 'punch',

     'punch',
     'laser',
     'missile',
     'punch',
     'laser',
     'missile',
     'punch',
     'laser',
     'missile',
     'punch',
     'laser',
     'missile' ] }
    

    { punch: 20, laser: 30, missile: 35 }

    Expected: ''Rocky has won the fight.'', instead got: ''Missile Bob has won the fight.''

    Seems wrong as few per 100 all the times

    Good Kata. I enjoyed it :)

  • Tonsias Avatar

    You should give the Robot the mehtod or told the Users that there is none: (Java)

    public void setHealth(int health){ this.health = helath; }

  • CyberPaint Avatar

    Hi there,

    I believe there might be a mistake with Fight 5: Speed is Everything - Case 1 should be a 'draw', but for some reason it is not. Both robots have same health and same speed, and same tactics.

    Why should Rocky win at this case and not be a draw?

  • mcspenzer Avatar

    Should the Robot class have setters for this Kata?

  • TixorCoder Avatar

    This comment has been hidden.

  • G_kuldeep Avatar

    kata was fun. thanks;)

  • user14 Avatar

    Hi! Why in the description does not say anything about using the tactics of the "space ray"?

  • kacper_lukaszewski Avatar

    This comment has been hidden.

  • hjacobs3 Avatar

    my code fails on test "SpeedIsEverything" can anyone give me a hint, please?:c upd : I solved it, kata is so smart :) respect to the author:)

  • clcraig Avatar

    @NateBrady23, C++ translation ready for review.

  • RyanHill92 Avatar

    Is it possible that the first test in the third category ("One or both robots have no tactics") is faulty? I'm passing 106 of 107 tests, with my code accounting for all three possible outcomes: robot1's health > robot2's health, robot2's health > robot1's health, robot1's health = robot2's health. The test I mentioned expected "Missile Bob has won the fight" but instead returned "undefined."

  • andrewsleake85 Avatar

    One of the random tests internally returned the wrong result. Attempting a few times got them all to pass. Missible bob had -1 health and The other guy was still alive, yet the random test said (falsely) missile bob was the winner. Also small hint: if you are getting timeouts, you have not provided an exit condition where there is a draw or both of them are out of tactics but still alive.

  • dinglemouse Avatar

    Kata description typo cut/paste wrong for all languages

    "tactics": ["missle", "missle", "missle", "missle"]
    

    that should be missile

  • FArekkusu Avatar

    Python translation is ready. Please, review and approve.

    Note: includes the "missle" typo fix.

  • wayne1512 Avatar

    error: health has private access in Robot error: speed has private access in Robot error: tactics has private access in Robot ...

    Why would they be private? How are we supposed to even check the value if it's private?

  • clcraig Avatar

    @NateBrady23, C++ translation ready for review.

  • zach.rodefer Avatar

    Missile is spelt wrong in the kata description:

    "tactics": ["missle", "missle", "missle", "missle"]

  • joecastle Avatar

    C# translation added!

  • Firefly2002 Avatar

    Kata could be updated to use Ruby objects :)

  • Voile Avatar

    This comment has been hidden.

  • hei-yu-fa Avatar

    Fight 5: Speed is everything. ✘ Expected: ''Rocky has won the fight.'', instead got: ''The fight was a draw.'' ✘ Expected: ''Missile Bob has won the fight.'', instead got: ''The fight was a draw.''

    I passed all except this...,and why?

  • LambyPants Avatar

    Fun kata, be sure and fix spelling errors (missle is spelled missle in the examples) which can throw off answers.

  • docgunthrop Avatar

    Nice kata; it could be forked into one where the attack sequence is based on the speed for each respective robot instead of alternating order.

    For example, if we have robot #1 with speed: 10, health: 100 and robot #2 with speed: 15, health: 125 (let's assume the attack damage is 10 and the tactics array is the same for both robots with a length of 15), then robot #1 would emerge as winner.

  • smashzen Avatar

    I'm getting occasional (appx 1 in 100) errors where my answer seems right. One example (using console.log on the arguments):

    { name: 'Rocky',
      health: 249,
      speed: 46,
      tactics: 
       [ 'punch',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile' ] } { name: 'Missile Bob',
      health: 233,
      speed: 85,
      tactics: 
       [ 'punch',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile' ] } { punch: 20, laser: 30, missile: 35 }
    ✘ Expected: '\'Missile Bob has won the fight.\'', instead got: '\'Rocky has won the fight.\''
    

    Another example:

    { name: 'Rocky',
      health: 58,
      speed: 61,
      tactics: 
       [ 'punch',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile' ] } { name: 'Missile Bob',
      health: 77,
      speed: 16,
      tactics: 
       [ 'punch',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile',
         'punch',
         'laser',
         'missile' ] } { punch: 20, laser: 30, missile: 35 }
    ✘ Expected: '\'Rocky has won the fight.\'', instead got: '\'Missile Bob has won the fight.\''
    

    I got one or two errors out of 107, then passed the kata on my third try. Then refreshed and tried again, got one error out of 107.

    Enjoyable kata, thanks.

  • dinglemouse Avatar

    It seems wrong that the Java robot object health member cannot be set. So these are only "what if" battles - since in a real battle the robot instance health remains unchanged no matter how many hits it takes.

  • Sokas Avatar

    "Missile" is spelt wrong. The penultimate point should be 'If both robots run out of tactics, whoever has the most health wins. Return the message " has won the fight."'

  • gcm Avatar

    Really good kata! Just a little typo on the second paragraph (java): "Each robot will have a name, hit points, speed, and then a list of battle tacitcs they are to perform in order." tacitcs -> tactics, no big deal.

  • ITSOES Avatar

    This (or equivalent) should be a test case for ruby:

    Test.describe("Fight 6: Tactic order matters") do
      robot1 = {"name"=>"Rocky", "health"=>255, "speed"=>2, "tactics"=>["punch", "punch", "laser", "missile", "missle", "missile"]}
      robot2 = {"name" => "Missile Bob", "health" => 100, "speed" => 21, "tactics" => ["missile", "missile", "laser", "punch", "punch", "punch"]}
      tactics = {"punch" => 20, "laser" => 30, "missile" => 135}
    
      Test.assert_equals(fight(robot1, robot2, tactics), "Missile Bob has won the fight.")
    end
    

    I was passing the random tests half the times I hit submit, and it took me a while to realize that it was because I was using pop instead of shift. Hence, this test checks that tactics are executed in the right order.

  • Evgen123 Avatar

    "If both robots run out of tactics and are tied for health, the fight is a draw. Return "The fight is a draw."" or "The fight was a draw."

  • Evgen123 Avatar

    "robot2 uses the first tactic, "missle" because he has the most speed. This reduces robot1's health by 20" but " "missile" => 35" 20 or 35 ?

  • alessandrodalbello Avatar

    Java translation available! Robot is an immutable class that represents a robot.

  • ucy Avatar

    Some of the test cases look wrong

  • alessandrodalbello Avatar

    From description:

    This reduces robot1's health by 20.

    I think you meant to say 35, not 20.

  • hilary Avatar

    Nice kata! Upvoted and translated into ruby for your approval. Happy to make any revisions you might want.