c# - Is there a way to make this more efficient? -
so working on text adventure improve programming skills (just beginner) , working on new combat system because old 1 boring. came across rock paper scissors system wanted used rock paper scissors system 5 options player choose from, , enemy or monster attacking player.
i used lot of if statements, didn't take long, i'm wondering if there's better way code more efficient, , not large.
public static void resultsofmoves(string playermove, string monstermove, monster currentmonster, weapon currentweapon, armor currentarmor, player currentplayer) { //monster responses player if (playermove == "dodge" && monstermove == "heavy"||monstermove == "stealth") { if (monstermove == "heavy") { monsterheavyattack(); } if (monstermove == "stealth") { monsterstealthattack(); } } else if (playermove == "charge" && monstermove == "dodge"||monstermove == "stealth") { if (monstermove == "dodge") { monsterdodge(); } if (monstermove == "stealth") { monsterstealthattack(); } } else if (playermove == "block" && monstermove == "charge" || monstermove == "dodge") { if (monstermove == "charge") { monsterchargeattack(); } if (monstermove == "dodge") { monsterdodge(); } } else if (playermove == "heavy" && monstermove == "block" || monstermove == "charge") { if (monstermove == "block") { monsterblock(); } if (monstermove == "charge") { monsterchargeattack(); } } else if (playermove == "stealth" && monstermove == "heavy" || monstermove == "block") { if (monstermove == "heavy") { monsterheavyattack(); } if (monstermove == "block") { monsterblock(); } } //players responses monster if (monstermove == "dodge" && playermove == "heavy" || playermove == "stealth") { if (playermove == "heavy") { monsterheavyattack(); } if (playermove == "stealth") { monsterstealthattack(); } } else if (monstermove == "charge" && playermove == "dodge" || playermove == "stealth") { if (playermove == "dodge") { monsterdodge(); } if (playermove == "stealth") { monsterstealthattack(); } } else if (monstermove == "block" && playermove == "charge" || playermove == "dodge") { if (playermove == "charge") { monsterchargeattack(); } if (playermove == "dodge") { monsterdodge(); } } else if (monstermove == "heavy" && playermove == "block" || playermove == "charge") { if (playermove == "block") { monsterblock(); } if (playermove == "charge") { monsterchargeattack(); } } else if (monstermove == "stealth" && playermove == "heavy" || playermove == "block") { if (playermove == "heavy") { monsterheavyattack(); } if (playermove == "block") { monsterblock(); } } }
first create move
enum, rather using strings:
public enum moves { charge, dodge, heavy, steath, block }
next, use dictionary
determine moves:
var moveresolution = new dictionary<tuple<moves, moves>, action> { { new tuple<moves, moves>(moves.dodge, moves.heavy), monsterheavyattack }, { new tuple<moves, moves>(moves.dodge, moves.steath), monsterstealthattack }, { new tuple<moves, moves>(moves.charge, moves.dodge), monsterdodge }, ... };
then determine appropriate move, do:
var movecombination = new tuple<moves, moves>(playermove, monstermove); if (moveresolution.containskey(movecombination)) { moveresolution[movecombination](); }
this code further improved replacing lazy tuple<moves, moves>
movecombination
struct. note, use struct
ensure moveresolution.containskey(movecombination)
part works need compare value, not reference.
Comments
Post a Comment