5 kyu

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

964 of 2,481NateBrady23

Description:

You and your friends have been battling it out with your Rock 'Em, Sock 'Em robots, but things have gotten a little boring. You've each decided to add some amazing new features to your robot and automate them to battle to the death.

Each robot will be represented by an object. You will be given two robot objects, and an object of battle tactics and how much damage they produce. Each robot will have a name, hit points, speed, and then a list of battle tactics they are to perform in order. Whichever robot has the best speed, will attack first with one battle tactic.

Your job is to decide who wins.

Example:

 robot1 = {
  "name": "Rocky",
  "health": 100,
  "speed": 20,
  "tactics": ["punch", "punch", "laser", "missile"]
 }
 robot2 = {
   "name": "Missile Bob",
   "health": 100,
   "speed": 21,
   "tactics": ["missile", "missile", "missile", "missile"]
 }
 tactics = {
   "punch": 20,
   "laser": 30,
   "missile": 35
 }
 
 fight(robot1, robot2, tactics) -> "Missile Bob has won the fight."
robot1 =
  name: "Rocky"
  health: 100
  speed: 20
  tactics: [
    "punch", "punch", "laser", "missile"
  ]
robot2 =
  name: "Missile Bob"
  health: 100
  speed: 21
  tactics: [
    "missile", "missile", "missile", "missile"
  ]
tactics =
  punch: 20
  laser: 30
  missile: 35
 
fight(robot1, robot2, tactics) # "Missile Bob has won the fight."
robot1 = {
  "name" => "Rocky",
  "health" => 100,
  "speed" => 20,
  "tactics" => ["punch", "punch", "laser", "missile"]
 }
 robot2 = {
   "name" => "Missile Bob",
   "health" => 100,
   "speed" => 21,
   "tactics" => ["missile", "missile", "missile", "missile"]
 }
 tactics = {
   "punch" => 20,
   "laser" => 30,
   "missile" => 35
 }
 
 fight(robot1, robot2, tactics) # "Missile Bob has won the fight."
  robot1.getName() => "Rocky"
  robot1.getHealth() => 100
  robot1.getSpeed() => 20
  robot1.getTactics() => ["punch", "punch", "laser", "missile"]
    
  robot2.getName() => "Missile Bob"
  robot2.getHealth() => 100
  robot2.getSpeed() => 21
  robot2.getTactics() => ["missile", "missile", "missile", "missile"]
 
  tactics = {
    "punch" => 20,
    "laser" => 30,
    "missile" => 35
  }
 
 fight(Robot robot1, Robot robot2, Map<String, Integer> tactics) -> "Missile Bob has won the fight."
 robot_1 = {
  "name": "Rocky",
  "health": 100,
  "speed": 20,
  "tactics": ["punch", "punch", "laser", "missile"]
 }
 robot_2 = {
   "name": "Missile Bob",
   "health": 100,
   "speed": 21,
   "tactics": ["missile", "missile", "missile", "missile"]
 }
 tactics = {
   "punch": 20,
   "laser": 30,
   "missile": 35
 }
 
 fight(robot_1, robot_2, tactics) -> "Missile Bob has won the fight."

robot2 uses the first tactic, "missile" because he has the most speed. This reduces robot1's health by 35. Now robot1 uses a punch, and so on.

Rules

  • A robot with the most speed attacks first. If they are tied, the first robot passed in attacks first.
  • Robots alternate turns attacking. Tactics are used in order.
  • A fight is over when a robot has 0 or less health or both robots have run out of tactics.
  • A robot who has no tactics left does no more damage, but the other robot may use the rest of his tactics.
  • If both robots run out of tactics, whoever has the most health wins. If one robot has 0 health, the other wins. Return the message "{Name} has won the fight."
  • If both robots run out of tactics and are tied for health, the fight is a draw. Return "The fight was a draw."

To Java warriors

Robot class is immutable.

Check out my other 80's Kids Katas:
Fundamentals

Stats:

CreatedDec 11, 2015
PublishedDec 11, 2015
Warriors Trained7268
Total Skips1884
Total Code Submissions39304
Total Times Completed2481
JavaScript Completions964
Ruby Completions168
CoffeeScript Completions14
Java Completions365
Python Completions1013
Total Stars272
% of votes with a positive feedback rating92% of 425
Total "Very Satisfied" Votes369
Total "Somewhat Satisfied" Votes46
Total "Not Satisfied" Votes10
Total Rank Assessments19
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • NateBrady23 Avatar
  • hilary Avatar
  • alessandrodalbello Avatar
  • gabbek Avatar
  • Blind4Basics Avatar
  • FArekkusu Avatar
  • trashy_incel Avatar
  • RileyHunter Avatar
  • saudiGuy Avatar
Ad