This will fix crit issues.
Critical strike is very useful, but sometimes it simply favors one or the other, making fights with critical strike merely being a "who is luckiest" contest. If you are lucky, you can crit several times in a row... with 20% critical strike chance, while the other one has a streak of non-crits when they have 40% crit. This code still provides the chance factor, but evens out the results of the chance factor. One part prevents over critically striking: someone with 10% crit chance is no longer able to crit 4 times in a row. Another part reduces the bad luck of not critically striking: someone with 80% crit chance is no longer able to NOT crit 4 times in a row.
The Java Code
import java.util.*; import java.math.*;
public class Main { public static void main(String[] args) { int choice = 1; //The critical strike chance int strike = 1; //If you hit an enemy double Chance; //Random number generator double Crits = 0; //Comparitive count of critical hits and non-critical hits. Number moves little in either direction. int CritNumber = 0; //Counts the number of critical hits done in the loop int StrikeNumber = 0; //Counts the total number of strikes done in the loop double percent = 0; //The percentage of how much you SHOULD critically strike Scanner Keyboard = new Scanner(System.in); //For interaction Random Generator = new Random(); //For the random generation of numbers
while (choice > 0 && choice <= 100 ) //Setting your critical strike chance { strike = 1; System.out.print ("Please select your critical strike chance (whole number only, between 1 and 100.\n" + "Or enter in 0 to exit.\n:"); choice = Keyboard.nextInt(); if (choice == 0) //The exit function { strike = 0; System.out.println ("You have 0 critical strike chance."); } if (choice < 0 || choice > 100) //Ensuring to keep the values within specified ranges { strike = 0; choice = 1; System.out.println ("Please only enter in a whole number value between 0 and 100.\n\n"); } if (choice > 0 && choice <= 100) //Finally, if this IS within the specified ranges { percent = 0.01 * choice; //Just a simple percentage value while (strike == 1) //To simulate a basic attack on something you can crit on { System.out.print ("Please enter 1 to strike, and 2 to quite.\n:"); strike = Keyboard.nextInt(); if (strike == 1) { Chance = Generator.nextInt(100); //Generating the random comparative value... This is the chance factor
// //Start of Critical hit mathematics if (Crits >= (Crits / percent)) //Limits how often you can critically strike by setting the appropriate minimal spacing { //If the spacing is met, then you move on to the chance factor Chance = Chance + (choice * Crits); //Increasing your chances to crit as you continue to NOT crit (through chance factor) if (Chance < choice) //This is the chance factor. { System.out.println ("Critical Hit!\n"); Crits = Crits - 1 + (1/percent); //Critical part in monitoring crit rate. CritNumber = CritNumber + 1; StrikeNumber = StrikeNumber + 1; } else { System.out.println("normal hit\n"); Crits = Crits - 1; //Critical part in monitoring crit rate StrikeNumber = StrikeNumber + 1; } } else { System.out.println ("normal hit\n"); Crits = Crits - 1; //Critical part in monitoring crit rate StrikeNumber = StrikeNumber + 1; } _//End of Critical hit mathematics _ //
} if (strike != 1 && strike != 2) //Ensuring to keep the values within the specified ranges { System.out.println ("Please ONLY enter either 1 OR 2\n"); strike = 1; //Resets THIS loop, just in case it was unintentional or you don't REALLY want to stop } if (strike == 2) { System.out.println ("\nOk.\n\n"+"Your crit chance is "+choice+"%.\n" + "You should have crit about " + (Math.round(StrikeNumber*percent)) +" times.\n" + "You critically struck "+CritNumber+" times.\n" + "You made a total of "+StrikeNumber+" strikes.\n" + "Your Critical Strike percentage is: " + (Math.round((CritNumber * 100) / StrikeNumber)) + "%.\n"); //Announcing what the results should be, and what the results are } } } } } }
Here is the simple explanation of how the mathematics works. 1: records your previous critical hits. 2: uses your record of critical hits to limit over-critical striking. 3: utilizes a chance factor to determine whether or not you critically strike. 4: uses your record of non-critical hits to increase your chances of critically hitting the more you do not crit. If you have questions, please comment below.