Discussion
Yone Moreno · Sep 18

[Create Code Following Best Practices] Two fighters, one winner.

Hello community,

Because of recently the "Green Game Jam" has been celebrated as a part of a mission to improve environmental awareness, as the following quote explains: https://playing4theplanet.org/about

"UNEP has been working with the gaming industry to explore how, through their massive reach, they can inspire young people to learn and act in support of the environment. The gaming industry reaches 1 in 3 people on the planet and has a platform with unprecedented influence. How can this rapidly expanding media platform be harnessed to deliver on the Sustainable Development Goals?"

"The Playing for the Planet Alliance was launched on 23 September 2019 at UN Headquarters in New York during the UN Secretary-General’s Climate Action Summit. The initiative is being facilitated by UNEP with the support of GRID-Arendal and Playmob. In joining the Alliance, members have made commitments ranging from integrating green activations in games, to reducing their emissions and supporting the global environmental agenda."

 

We could try to exercise us with a code challenge, to create the best solution possible, in ObjectScript ( or another language but it would be prefered to use ObjectScript ) to generate the most easy to read and understandable code to solve the following challenge:

 

 

DESCRIPTION:

Create a function that returns the name of the winner in a fight between two fighters.

Each fighter takes turns attacking the other and whoever kills the other first is victorious. Death is defined as having health <= 0.

Each fighter will be a Fighter object/instance. See the Fighter class below in your chosen language.

Both health and damagePerAttack (damage_per_attack for python) will be integers larger than 0. You can mutate the Fighter objects.

Example:

  declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Lew") => "Lew"
  
  Lew attacks Harry; Harry now has 3 health.
  Harry attacks Lew; Lew now has 6 health.
  Lew attacks Harry; Harry now has 1 health.
  Harry attacks Lew; Lew now has 2 health.
  Lew attacks Harry: Harry now has -1 health and is dead. Lew wins.
public class Fighter {
  public String name;
  public int health, damagePerAttack;
  public Fighter(String name, int health, int damagePerAttack) {
    this.name = name;
    this.health = health;
    this.damagePerAttack = damagePerAttack;
  }
}

 

©️©️ This coding challenge has been taken from this source: https://www.codewars.com/kata/577bd8d4ae2807c64b00045b

 

 

Thanks for your replies and time, and we would like to see your answers and best practices.

 

 

I will post my own solution in Java as well as other 2 Java reference solutions, to explain what could be some "understandable" code

 

39 Female Mma Fighter Illustrations & Clip Art - iStock

0
0 74
Discussion (1)1
Log in or sign up to continue

Example solutions:

dinglemouseChungGorrainrainEAnochlunacjulialebowAndrew P.Kuzmanov_MarioIvanKotsovskiZasho (+ 13)'s solutions

public class Kata {
  public static String declareWinner(Fighter fighter1, Fighter fighter2, String firstAttacker) {
    Fighter a=fighter1, b=fighter2;
    if (firstAttacker.equals(fighter2.name)) {
      a = fighter2; b = fighter1;
    }    
    while (true) {      
      if ((b.health -= a.damagePerAttack) <= 0) return a.name;  // a wins
      if ((a.health -= b.damagePerAttack) <= 0) return b.name;  // b wins
    }
  }
}

My own:

public class Kata {
  public static String declareWinner(Fighter fighter1, Fighter fighter2,
                String firstAttacker) {
    
    String nextAttacker = firstAttacker;
    do{
        if(nextAttacker.equals(fighter1.name)){  
          fighter2.health -= fighter1.damagePerAttack;
          if(fighter2.health > 0){
            nextAttacker = fighter2.name;
          }
        }else{
          fighter1.health -= fighter2.damagePerAttack;
          if(fighter1.health > 0){
            nextAttacker = fighter1.name;
          }
        }
    
    }while(fighter1.health > 0 && fighter2.health > 0);
    return nextAttacker;
  }
}

marko-bekhtaKutayBSVarakutarosedhivyaShreyasRanimeshp's solution
 

public class Kata {
  public static String declareWinner(Fighter fighter1, Fighter fighter2, String firstAttacker) {
        int moves1 = (int) Math.ceil( (double)fighter2.health / fighter1.damagePerAttack);
        int moves2 = (int) Math.ceil( (double)fighter1.health / fighter2.damagePerAttack);
        if (moves1 > moves2) {
            return fighter2.name;
        } else if (moves1 < moves2) {
            return fighter1.name;
        } else {
            return firstAttacker;
        }
  }
}

I hope you feel challenged with this exercise