DEV Community

loading...

Discussion on: Daily Challenge #43 - Boardgame Fight Resolver

Collapse
vivek97 profile image
Vivek97

private static Map pair = new HashMap<>();

void challange_43()
{
    //Archers > Swordsmen > Pikemen > Cavalry > Archers
    pair.put("Archers","Swordsmen");
    pair.put("Swordsmen","Pikemen");
    pair.put("Pikemen","Cavalry");
    pair.put("Cavalry","Archers");
}

public static void main(String[] args) {

    Challange_43 object = new Challange_43();
    System.out.println("Winner between Pikemen, Cavalry is "+object.getWinner("Pikemen","Cavalry"));
    System.out.println("Winner between Archers Swordsmen is "+object.getWinner("Archers","Swordsmen"));
    System.out.println("Winner between Archers, Cavalry is "+object.getWinner("Archers","Cavalry"));


}

public String getWinner(String attacker, String defender)
{

    if(pair.containsKey(defender)&& pair.get(defender).equals(attacker))
        return defender;    
    else
        return attacker;    
}