Rounding glitch in javascript, returning 4000% - javascript

I am trying to measure the percentage of the amount of enemies in a javascript game I am developing.
Math.round(10000000 / V.S * 100) / 100
This code works fine when V.S is over 1 million, however returns observed values such as 3400% when V.S is around the thousands, but this should be about 0.01%.
V.S is the amount of enemies
10 million is the amount of enemies needed to make you lose the game
I want to show the percentage between the amount currently and the amount needed to lose the game.
How can I make it show that?

V.S is the amount of enemies
10 million is the amount of enemies needed to make you lose the game
Your current code yields how many times V.S goes into 10 million
I want to show the percentage between the amount currently and the
amount needed to lose the game.
If you want to know what percentage of the 10 million has shown up:
var pctShown = V.S/100000; //or Math.round(V.S/100000) if you want only whole pcts
(note: reduction of dividing by 10,000,000 then * 100 to get a pct representation)
If you want the percent remaining, simply find the inverse:
var pctRemaining = 100 - V.S/100000; //or again Math.round(100 - V.S/100000)

Related

How to make optimal decision in poker like single player card game?

I need some help to finish my app that helps to make optimal decision in card game.
Short game description:
Single player game - 1 player one deck only for himself
24 card deck, card values from 1 to 8, 3 colors of each (Red,Yellow, Blue).
At start of the game deck gets randomly shuffled then player pulls 5 cards (19 left in deck) to his hand and from this point he's allowed to:
Put away one card (this card is not in game anylonger) and take one card from top of shuffled deck for its place.
or
Can put away 3 cards that form a one of possible point granting combinations, then takes 3 cards to his hand.
Game ends when cards in deck ends and player have no more possible scoring combinations.
Goal of the game is to obtain as much points as possible.
List of variants: (called later combinations)
Three of a kind (8 possible in total):
111 - 20 points
222 - 30 points
333 - 40 points
444 - 50 points
555 - 60 points
666 - 70 points
777 - 80 points
888 - 90 points
Straight (card can have any color, 144 possible in total):
123 - 10 points
234 - 20 points
345 - 30 points
456 - 40 points
567 - 50 points
678 - 60 points
Straight flush (cards have to be in the same color, RRR/YYY/BBB, 18 possible in total):
123 - 50 points
234 - 60 points
345 - 70 points
456 - 80 points
567 - 90 points
678 - 100 points
I created the whole game logic in JS but suffer to make algoritm that will help me:
which card to put away from hand? - to get highest chance for combination
or:
which card to put away from hand?- to lose least amount of possible combinations
I don't really know which approach i should take. Or should i combine them somehow and let the formula decide? Currently I'm reading about risk reward ratio maybe thats the way to go?
I have to remember that with each card less i have less and less possible combinations (starting with 170), but sometimes its worth in later stage of the game to sacrifice cheaper combination for higher point score.
My past ways of thinking:
Calculate the probability of each possible remaining combination when putting away each card from hand.(i.e. 5 times) and decide which one is the worst.
Calculate the probability of each possible remaining combination for every 2 card subset of 5 card set in hand (so 10 times looking for 1 card to fill the combination) and decide which card from hand is the worst.
Calculate the probability of each possible remaining combination left in deck (not counting cards in hand there). 3+ moves in advance
And somehow add those three together to get optimal/close to optimal solution?
Should i add potential loss to my calculations and how to form them? If i take weak combination and later in the game i can not obtain higher ones since they are mutually exclusive.

Stacking percentage reduction depending on input (java script)

I'm sure this is simple enough for someone better at maths than me, but the solution is eluding me.
I need to take a number, the block chain height, which at every 500 thousand blocks, reduces the reward by 5%.
The original reward is 3 per block. So block 500001 would be 2.85, block 1,000,001 would be 2.7075, block 1,500,001 would be 2.572125, etc.
This happens all the way to block 30,000,000 so using case or if is not practical.
I can't quite tell if you need continuous reduction, or "sudden" reduction after crossing the next 500,000 (for example, is the reward at block 499,999 still 3%?).
Assuming you need "sudden" reduction, you first calculate the number of reductions.
var blocks = 10000000; // 10 million, just as an example
var reductions = Math.floor(blocks / 500000);
Then apply the reductions to your reward
var reward = 0.03 * Math.pow(0.95, reductions);
That will take off 5% (by multiplying by 0.95) the correct number of times. The result will be a decimal percent (like 0.0285 representing 2.85%, etc).
If you need a continuous reduction, you can actually just remove the Math.floor from the calculation of reductions above.

Two player number battling simulator favours second player

I am writing a fairly basic (for now) number crunching battle simulator in javascript. Its purpose is to test formula for a forthcoming game, so we can balance players stats.
Currently, the sim reduces a players health based on the following formula
player1.hp -= (Math.floor(Math.random() * 5) + 1) * player2[Math.floor(Math.random() * player2.length)].str;
HP starts on 100, and str is 2.
player2[Math.floor(Math.random() * player2.length)].str
The above code picks 1 of 3 "parts" of the player (head, body, legs) as each piece has its own stats.
So for any particular turn, starting with player 1, their head's HP is reduced by an amount based on the first formula (random part of player 2 picked, it's str stat is multiplied by a random number between 1 and 5)
Then player 2's parts are attacked, with a random part of player 1 being picked for the str stat.
(Yes, player 2 attacks first always currently, then player 1 - this may be the issue?)
When I run a single "battle", the results are fairly mixed. But when I run 100 or 1000 battles, player 2 typically comes out on top, with an average 60% win rate.
JS file here (download and run via Node)
Player who starts always has advantage because he is 1st to be able to do the finishing blow if you think of a situation where each player has 100 hp and 1 hit = 10 damage then player who started will do the 10th hit and finish the battle while player 2 will have done only 9 hits. Randomizing damage still gives advantage as 1st player has 1 hit advantage
This is a well-known problem in military combat simulation models which are turn-based. There are a couple of solutions that are commonly used. One is to randomize who gets to go first. The second is to calculate the deltas for both players before applying those deltas. The second approach has the interesting (and realistic) result that in some cases both players can be killed.

Plot exponential pulses over time

I've been racking my brain for days over this.
I'm planning on using PHP with SoX to produce audio pulses over a certain amount of time. In this case i'm using frequency as a way to describe how many pulses occur per second. So a frequency of 8 is 8 pulses per second.
Example of the pulse I'll be using (Isochronic tone)
I need to know the start time of each of these pulses so that I can plot them in time when producing the audio. This is easily done when the frequency remains constant. But when the frequency changes over time, I run into the problem that it takes too much or too little time to complete the average amount of pulses.
Here's an example in Javascript (I'm using JS for now to get the maths working)
var f0 = 8; //Start frequency
var f1 = 1; //End frequency
var interval = 30; //Total time
var average_freq = (f0 + f1) / 2; //Average frequency
var pulses = average_freq * interval; //Total number of pulses needed
var pulse_start_time = 0;
for(var i = 0; i < pulses; i++) {
var delta = i / pulses;
var this_freq = f0 + (f1 - f0) * delta;
/*
need exponential formula to find start time
*/
console.log( "pulse: "+ (i+1) + ", "+
"pulse frequency: "+ this_freq + ", "+
"start time: "+ pulse_start_time);
pulse_start_time += 1 / this_freq; //This method works fine providing
//both start and end frequency are
//the same.
}
I've done a lot of researching on this subject using topics like:
Drawing sine wave with increasing Amplitude and frequency over time
Plot Frequency Sweep In Python
sine wave that slowly ramps up frequency from f1 to f2 for a given time
sine wave that exponentialy changes between frequencies f1 and f2 at given time/amount of samples
But I'm still unable to figure out how to get the start time of each pulse using these techniques, or maybe I'm going about this all wrong.
Any help will be appreciated! :)
you need to use proper math equation y=f(t) for your signal instead. In that way you will obtain also the amplitude. I do not know the properties of your signal but it does not look like logarithmic. I would construct it like this:
y=( |sin(c0*t)| + sin(c0*t) )^c1 * sin(c2*t) * c3
where:
t is time
c0=frequency of gaps / 2*Pi
c1 is sharpness of gap/signal transition
c2=frequency of pulses / 2*Pi
c3 is amplitude of signal
The first part just modulate the base signal with gaps and shaped amplitude shape, the last therm is your base signal.
The average pulses per second is not your signal frequency !!! instead it is around half of it (depends on the signal to gap ratio). Now just increment t in some loop and compute y(t) for your data. To avoid floating point rounding problems limit the t to common period between the 2 sin waves.

Sample array at even intervals

I have an array that could be any size and I'm interested in a fixed number of elements, but evenly distributed. So say I want 1000 elements. If there are 1000 or less elements in the array, I want them all, so that's easy. If there were 2000 elements in the array, I would want every second one. 3000 elements and I'd want every third one... and so on.
I'm having trouble implementing this. I thought of using modulo and this works for round numbers. e.g. 2000 / 1000 = 2 so a simple if (n % 2 == 0) works here (so skip each element where it evaluates true). Likewise for 1200: the excess is 200 so 1200 / 200 = 6 so if (n % 6 == 0) also works.
However, it breaks down when the number of elements divided by the excess isn't a nice round number:
elements = 2788
limit = 1000
factor = 2788 / (2788 - 1000) = 1.559288
Here the modulo operator won't work as n % 1.55928 is never going to give a remainder of zero. Does anyone know how to achieve this? I'm using JavaScript.
Thanks!
What you want is a uniform discrete distribution. Because the number of elements is not divisible by the limit, you would want some flexibility in the limit to keep it evenly distributed. To do so, you could just perform the sampling in elements/limit intervals, rounding the result to its nearest integer.
If your limit is flexible and you can cover more ground by finding the biggest possible limit (in this case, 697).

Categories

Resources