Even Distribution - objective-c

Say I have turn based game with 20 attack turns and the players attack speed determines how often they get to attack, how would you calculate and or graph a table of "who attacks when" when one person has an attack speed of 5 and another a speed of 8? (These are test values, the values I will be using will vary. 20 attack turns will be the cap, however, each player will be able to invest in their own attack speed with their skill points up to a value of 10)
I've been programming in C for about a 2 years and I'm currently playing with Obj-C making my first indy game, any advise or knowledge would be of great help.

If I understand the problem correctly:
The first to attack will be min(a,b), where a and b are your initial "attack speeds", 5 and 8. Then subtract the min value of both, and if the result is 0 or less add the attacker's original value again. So after the first attack, a=5 again, but now b=8-5=3. Then it's b's turn:
a = 5 - 8 = -3
b = 3 - 8 = -5
Both less than zero, so add 5 and 8 again:
a = 2
b = 3
a attacks:
a = -3 -> a = 2
b = -2 -> b = 6
and thus a gets to score another hit. And so on, until you get a tie -- you're bound to run into that one way or another. You could return a "tie" result, or let the last attacker or last defender "win" that one. Put some thought in this, because what if both players have the same "attack" value?

This math problem tells me who’s turn is when -
For example:
There’s 11 attacks
you attack 8 times and your enemy attacks 3
Determine an attack sequence thats evenly distributed and consistently repeatable.
In this case:
1. player 1 attacks
2. player 1 attacks
3. player 2 attacks
4. player 1 attacks
5. player 1 attacks
6. player 1 attacks
7. player 2 attacks
8. player 1 attacks
9. player 1 attacks
10. player 1 attacks
11. player 2 attacks
(repeat these 11 steps as long as needed)
I first thought the solution would be best done with division until I ran into 20 walls, so
I choose subtraction bc it can be reproduced and works better programmatically, instead of creating an attack sequence by division and storing it as an array and indexing the array each time you need to determine a persons turn.
so my code to do this was:

if (playerTwoWent == 0)
{
difference = difference - player_twos_speed;
}
else if (playerTwoWent == 1)
{
difference = player_ones_speed + difference;
playerTwoWent = 0;
}
if (difference >= 0)
{
printf("player 1s turn: %d\n", difference);
}
else if (difference < 0)
{
if (playerTwoWent == 0)
{
printf("player 2s turn: %d\n", difference);
playerTwoWent = 1;
}
}

Related

VB.NET doesn't round numbers correctly?

I'm testing the speed of some functions so I made a test to run the functions over and over again and I stored the results in an array. I needed them to be sorted by the size of the array I randomly generated. I generate 100 elements. Merge sort to the rescue! I used this link to get me started.
The section of code I'm focusing on:
private void mergesort(int low, int high) {
// check if low is smaller then high, if not then the array is sorted
if (low < high) {
// Get the index of the element which is in the middle
int middle = low + (high - low) / 2;
// Sort the left side of the array
mergesort(low, middle);
// Sort the right side of the array
mergesort(middle + 1, high);
// Combine them both
merge(low, middle, high);
}
}
which translated to VB.NET is
private sub mergesort(low as integer, high as integer)
' check if low is smaller then high, if not then the array is sorted
if (low < high)
' Get the index of the element which is in the middle
dim middle as integer = low + (high - low) / 2
' Sort the left side of the array
mergesort(low, middle)
' Sort the right side of the array
mergesort(middle + 1, high)
' Combine them both
merge(low, middle, high)
end if
end sub
Of more importance the LOC that only matters to this question is
dim middle as integer = low + (high - low) / 2
In case you wanna see how merge sort is gonna run this baby
high low high low
100 0 10 0
50 0 6 4
25 0 5 4
12 0 12 7
6 0 10 7
3 0 8 7
2 0 :stackoverflow error:
The error comes from the fact 7 + (8 - 7) / 2 = 8. You'll see 7 and 8 get passed in to mergesort(low, middle) and then we infinite loop. Now earlier in the sort you see a comparison like this again. At 5 and 4. 4 + (5 - 4) / 2 = 4. So essentially for 5 and 4 it becomes 4 + (1) / 2 = 4.5 = 4. For 8 and 7 though it's 7 + (1) / 2 = 7.5 = 8. Remember the numbers are typecasted to an int.
Maybe I'm just using a bad implementation of it or my typecasting is wrong, but my question is: Shouldn't this be a red flag signaling something isn't right with the rounding that's occuring?
Without understanding the whole algorithm, note that VB.NET / is different than C# /. The latter has integer division by default, if you want to truncate decimal places also in VB.NET you have to use \.
Read: \ Operator
So i think that this is what you want:
Dim middle as Int32 = low + (high - low) \ 2
You are correct in your diagnosis: there's something inconsistent with the rounding that's occurring, but this is entirely expected if you know where to look.
From the VB.NET documentation on the / operator:
Divides two numbers and returns a floating-point result.
This documentation explicitly states that , if x and y are integral types, x / y returns a Double. So, 5 / 2 in VB.NET would be expected to be 2.5.
From the C# documentation on the / operator:
All numeric types have predefined division operators.
And further down the page:
When you divide two integers, the result is always an integer.
In the case of C#, if x and y are integers, x / y returns an integer (rounded down). 5 / 2 in C# is expected to return 2.

How to test a bit in a decimal number

I have a set of decimal numbers. I need to check if a specific bit is set in each of them. If the bit is set, I need to return 1, otherwise return 0.
I am looking for a simple and fast way to do that.
Say, for example, I am checking if the third bit is set. I can do (number AND (2^2)), it will return 4 if the bit is set, otherwise it will return 0. How do I make it to return 1 instead of 4?
Thank you!
if ((number AND (2^bitnumber) <> 0) then return 1 else return 0 end if
If you can change your return type to boolean then this is more elegant
return ((number AND (2^bitnumber)) <> 0)
While the division solution is a simple one, I would think a bit-shift operation would be more efficient. You'd have to test it to be sure, though. For instance, if you are using 1 based bit indexes, you could do this:
Dim oneOrZero As Integer = (k And 2 ^ (n - 1)) >> (n - 1)
(Where k is the number and n is the bit index). Of, if you are using 0 based bit indexes, you could just do this:
Dim oneOrZero As Integer = (k And 2 ^ n) >> n
Sorry, guys, I am too slow today.
To test a bit number "n" in a decimal number "k":
(k AND 2^(n-1))/(2^(n-1))
will return 1 if the bit is set, otherwise will return 0.
=====================================================
Hi again, guys!
I compared the performance of the three proposed solutions with zero-based indexes, and here are the results:
"bit-shift solution" - 8.31 seconds
"if...then solution" - 8.44 seconds
"division solution" - 9.41 seconds
The times are average of the four consecutive runs.
Surprisingly for me, the second solution outperformed the third one.
However, after I modified the "division solution" this way:
p = 2 ^ n : oneOrZero = (k And p) / p
it started to run in 7.48 seconds.
So, this is the fastest of the proposed solutions (despite of what Keith says :-).
Thanks everybody for the help!
I really don't know if it can help anyone more than the above, but, here we go.
When I need to fast check a bit in number I compare the decimal-value of this bit directly.
I mean, if I would need to see of the 6th bit is on (32), I check its decimal value, like this:
if x and 32 = 32 then "the bit is ON"
Try for instance check 38 with 32, 4 and 2... and the other bits.
You will see only the actual bits turned on.
I hope it can help.
Yes! Simply use a bit mask. I enumerate the bits, then AND the number with the bit value. Very little math on the PC side as it uses lookup tables instead. The AND basically shuts off all the other bits except the one you are interested in. Then you check it against itself to see if it's on/off.
Enum validate
bit1 = 1
bit2 = 2
bit3 = 4
bit4 = 8
bit5 = 16
bit6 = 32
bit7 = 64
bit8 = 128
End Enum
If num And validate.bit3 = validate.bit3 Then true

Understanding the bitwise AND Operator

I have been reading about bit operators in Objective-C in Kochan's book, "Programming in Objective-C".
I am VERY confused about this part, although I have really understood most everything else presented to me thus far.
Here is a quote from the book:
The Bitwise AND Operator
Bitwise ANDing is frequently used for masking operations. That is, this operator can be used easily to set specific bits of a data item to 0. For example, the statement
w3 = w1 & 3;
assigns to w3 the value of w1 bitwise ANDed with the constant 3. This has the same ffect of setting all the bits in w, other than the rightmost two bits to 0 and preserving the rightmost two bits from w1.
As with all binary arithmetic operators in C, the binary bit operators can also be used as assignment operators by adding an equal sign. The statement
word &= 15;
therefore performs the same function as the following:
word = word & 15;
Additionally, it has the effect of setting all but the rightmost four bits of word to 0. When using constants in performing bitwise operations, it is usually more convenient to express the constants in either octal or hexadecimal notation.
OK, so that is what I'm trying to understand. Now, I'm extremely confused with pretty much this entire concept and I am just looking for a little clarification if anyone is willing to help me out on that.
When the book references "setting all the bits" now, all of the bits.. What exactly is a bit. Isn't that just a 0 or 1 in 2nd base, in other words, binary?
If so, why, in the first example, are all of the bits except the "rightmost 2" to 0? Is it 2 because it's 3 - 1, taking 3 from our constant?
Thanks!
Numbers can be expressed in binary like this:
3 = 000011
5 = 000101
10 = 001010
...etc. I'm going to assume you're familiar with binary.
Bitwise AND means to take two numbers, line them up on top of each other, and create a new number that has a 1 where both numbers have a 1 (everything else is 0).
For example:
3 => 00011
& 5 => 00101
------ -------
1 00001
Bitwise OR means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 (everything else is 0).
For example:
3 => 00011
| 5 => 00101
------ -------
7 00111
Bitwise XOR (exclusive OR) means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 AND the other number has a 0 (everything else is 0).
For example:
3 => 00011
^ 5 => 00101
------ -------
6 00110
Bitwise NOR (Not OR) means to take the Bitwise OR of two numbers, and then reverse everything (where there was a 0, there's now a 1, where there was a 1, there's now a 0).
Bitwise NAND (Not AND) means to take the Bitwise AND of two numbers, and then reverse everything (where there was a 0, there's now a 1, where there was a 1, there's now a 0).
Continuing: why does word &= 15 set all but the 4 rightmost bits to 0? You should be able to figure it out now...
n => abcdefghjikl
& 15 => 000000001111
------ --------------
? 00000000jikl
(0 AND a = 0, 0 AND b = 0, ... j AND 1 = j, i AND 1 = i, ...)
How is this useful? In many languages, we use things called "bitmasks". A bitmask is essentially a number that represents a whole bunch of smaller numbers combined together. We can combine numbers together using OR, and pull them apart using AND. For example:
int MagicMap = 1;
int MagicWand = 2;
int MagicHat = 4;
If I only have the map and the hat, I can express that as myInventoryBitmask = (MagicMap | MagicHat) and the result is my bitmask. If I don't have anything, then my bitmask is 0. If I want to see if I have my wand, then I can do:
int hasWand = (myInventoryBitmask & MagicWand);
if (hasWand > 0) {
printf("I have a wand\n");
} else {
printf("I don't have a wand\n");
}
Get it?
EDIT: more stuff
You'll also come across the "bitshift" operator: << and >>. This just means "shift everything left n bits" or "shift everything right n bits".
In other words:
1 << 3 = 0001 << 3 = 0001000 = 8
And:
8 >> 2 = 01000 >> 2 = 010 = 2
"Bit" is short for "binary digit". And yes, it's a 0 or 1. There are almost always 8 in a byte, and they're written kinda like decimal numbers are -- with the most significant digit on the left, and the least significant on the right.
In your example, w1 & 3 masks everything but the two least significant (rightmost) digits because 3, in binary, is 00000011. (2 + 1) The AND operation returns 0 if either bit being ANDed is 0, so everything but the last two bits are automatically 0.
w1 = ????...??ab
3 = 0000...0011
--------------------
& = 0000...00ab
0 & any bit N = 0
1 & any bit N = N
So, anything bitwise anded with 3 has all their bits except the last two set to 0. The last two bits, a and b in this case, are preserved.
#cHao & all: No! Bits are not numbers. They’re not zero or one!
Well, 0 and 1 are possible and valid interpretations. Zero and one is the typical interpretation.
But a bit is only a thing, representing a simple alternative. It says “it is” or “it is not”. It doesn’t say anything about the thing, the „it“, itself. It doesn’t tell, what thing it is.
In most cases this won’t bother you. You can take them for numbers (or parts, digits, of numbers) as you (or the combination of programming languages, cpu and other hardware, you know as being “typical”) usaly do – and maybe you’ll never have trouble with them.
But there is no principal problem if you switch the meaning of “0“ and “1”. Ok, if doing this while programming assembler, you’ll find it a bit problematic as some mnemonics will do other logic then they tell you with their names, numbers will be negated and such things.
Have a look at http://webdocs.cs.ualberta.ca/~amaral/courses/329/webslides/Topic2-DeMorganLaws/sld017.htm if you want.
Greetings

Recognizing when to use the modulus operator

I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator?
I know I can use the modulus operator to see whether a number is even or odd and prime or composite, but that's about it. I don't often think in terms of remainders. I'm sure the modulus operator is useful, and I would like to learn to take advantage of it.
I just have problems identifying where the modulus operator is applicable. In various programming situations, it is difficult for me to see a problem and realize "Hey! The remainder of division would work here!".
Imagine that you have an elapsed time in seconds and you want to convert this to hours, minutes, and seconds:
h = s / 3600;
m = (s / 60) % 60;
s = s % 60;
0 % 3 = 0;
1 % 3 = 1;
2 % 3 = 2;
3 % 3 = 0;
Did you see what it did? At the last step it went back to zero. This could be used in situations like:
To check if N is divisible by M (for example, odd or even)
or
N is a multiple of M.
To put a cap of a particular value. In this case 3.
To get the last M digits of a number -> N % (10^M).
I use it for progress bars and the like that mark progress through a big loop. The progress is only reported every nth time through the loop, or when count%n == 0.
I've used it when restricting a number to a certain multiple:
temp = x - (x % 10); //Restrict x to being a multiple of 10
Wrapping values (like a clock).
Provide finite fields to symmetric key algorithms.
Bitwise operations.
And so on.
One use case I saw recently was when you need to reverse a number. So that 123456 becomes 654321 for example.
int number = 123456;
int reversed = 0;
while ( number > 0 ) {
# The modulus here retrieves the last digit in the specified number
# In the first iteration of this loop it's going to be 6, then 5, ...
# We are multiplying reversed by 10 first, to move the number one decimal place to the left.
# For example, if we are at the second iteration of this loop,
# reversed gonna be 6, so 6 * 10 + 12345 % 10 => 60 + 5
reversed = reversed * 10 + number % 10;
number = number / 10;
}
Example. You have message of X bytes, but in your protocol maximum size is Y and Y < X. Try to write small app that splits message into packets and you will run into mod :)
There are many instances where it is useful.
If you need to restrict a number to be within a certain range you can use mod. For example, to generate a random number between 0 and 99 you might say:
num = MyRandFunction() % 100;
Any time you have division and want to express the remainder other than in decimal, the mod operator is appropriate. Things that come to mind are generally when you want to do something human-readable with the remainder. Listing how many items you could put into buckets and saying "5 left over" is good.
Also, if you're ever in a situation where you may be accruing rounding errors, modulo division is good. If you're dividing by 3 quite often, for example, you don't want to be passing .33333 around as the remainder. Passing the remainder and divisor (i.e. the fraction) is appropriate.
As #jweyrich says, wrapping values. I've found mod very handy when I have a finite list and I want to iterate over it in a loop - like a fixed list of colors for some UI elements, like chart series, where I want all the series to be different, to the extent possible, but when I've run out of colors, just to start over at the beginning. This can also be used with, say, patterns, so that the second time red comes around, it's dashed; the third time, dotted, etc. - but mod is just used to get red, green, blue, red, green, blue, forever.
Calculation of prime numbers
The modulo can be useful to convert and split total minutes to "hours and minutes":
hours = minutes / 60
minutes_left = minutes % 60
In the hours bit we need to strip the decimal portion and that will depend on the language you are using.
We can then rearrange the output accordingly.
Converting linear data structure to matrix structure:
where a is index of linear data, and b is number of items per row:
row = a/b
column = a mod b
Note above is simplified logic: a must be offset -1 before dividing & the result must be normalized +1.
Example: (3 rows of 4)
1 2 3 4
5 6 7 8
9 10 11 12
(7 - 1)/4 + 1 = 2
7 is in row 2
(7 - 1) mod 4 + 1 = 3
7 is in column 3
Another common use of modulus: hashing a number by place. Suppose you wanted to store year & month in a six digit number 195810. month = 195810 mod 100 all digits 3rd from right are divisible by 100 so the remainder is the 2 rightmost digits in this case the month is 10. To extract the year 195810 / 100 yields 1958.
Modulus is also very useful if for some crazy reason you need to do integer division and get a decimal out, and you can't convert the integer into a number that supports decimal division, or if you need to return a fraction instead of a decimal.
I'll be using % as the modulus operator
For example
2/4 = 0
where doing this
2/4 = 0 and 2 % 4 = 2
So you can be really crazy and let's say that you want to allow the user to input a numerator and a divisor, and then show them the result as a whole number, and then a fractional number.
whole Number = numerator/divisor
fractionNumerator = numerator % divisor
fractionDenominator = divisor
Another case where modulus division is useful is if you are increasing or decreasing a number and you want to contain the number to a certain range of number, but when you get to the top or bottom you don't want to just stop. You want to loop up to the bottom or top of the list respectively.
Imagine a function where you are looping through an array.
Function increase Or Decrease(variable As Integer) As Void
n = (n + variable) % (listString.maxIndex + 1)
Print listString[n]
End Function
The reason that it is n = (n + variable) % (listString.maxIndex + 1) is to allow for the max index to be accounted.
Those are just a few of the things that I have had to use modulus for in my programming of not just desktop applications, but in robotics and simulation environments.
Computing the greatest common divisor
Determining if a number is a palindrome
Determining if a number consists of only ...
Determining how many ... a number consists of...
My favorite use is for iteration.
Say you have a counter you are incrementing and want to then grab from a known list a corresponding items, but you only have n items to choose from and you want to repeat a cycle.
var indexFromB = (counter-1)%n+1;
Results (counter=indexFromB) given n=3:
`1=1`
`2=2`
`3=3`
`4=1`
`5=2`
`6=3`
...
Best use of modulus operator I have seen so for is to check if the Array we have is a rotated version of original array.
A = [1,2,3,4,5,6]
B = [5,6,1,2,3,4]
Now how to check if B is rotated version of A ?
Step 1: If A's length is not same as B's length then for sure its not a rotated version.
Step 2: Check the index of first element of A in B. Here first element of A is 1. And its index in B is 2(assuming your programming language has zero based index).
lets store that index in variable "Key"
Step 3: Now how to check that if B is rotated version of A how ??
This is where modulus function rocks :
for (int i = 0; i< A.length; i++)
{
// here modulus function would check the proper order. Key here is 2 which we recieved from Step 2
int j = [Key+i]%A.length;
if (A[i] != B[j])
{
return false;
}
}
return true;
It's an easy way to tell if a number is even or odd. Just do # mod 2, if it is 0 it is even, 1 it is odd.
Often, in a loop, you want to do something every k'th iteration, where k is 0 < k < n, assuming 0 is the start index and n is the length of the loop.
So, you'd do something like:
int k = 5;
int n = 50;
for(int i = 0;i < n;++i)
{
if(i % k == 0) // true at 0, 5, 10, 15..
{
// do something
}
}
Or, you want to keep something whitin a certain bound. Remember, when you take an arbitrary number mod something, it must produce a value between 0 and that number - 1.

How to implement frequency of attack in game?

In a RPG game,suppose there are role A and B.
A will conduct x attacks per second
B will conduct y attacks per second
If we suppose A initiates the attack and the final attacks may be :
A A B A B ...
How to calculate the sequence of attacks?
Here's one way to do it in Python 3.0 using a generator and fractions:
def get_attack_sequence(a, b):
from fractions import Fraction
count_a = count_b = 0
rate_a = Fraction(1, a)
rate_b = Fraction(1, b)
while 1:
new_count_a = count_a + rate_a
new_count_b = count_b + rate_b
if new_count_a < new_count_b:
yield "A"
count_a = new_count_a
elif new_count_a > new_count_b:
yield "B"
count_b = new_count_b
else:
yield "A|B"
count_a = new_count_a
count_b = new_count_b
attack_sequence = get_attack_sequence(3, 2)
print(' '.join(next(attack_sequence) for _ in range(10)))
Output:
A B A A|B A B A A|B A B
An attack frequency of 0 needs to be checked for. I haven't done this in the above code for simplicity, but it's easy to fix and probably best handled outside this function (a battle where one player can't attack wouldn't be very interesting anyway).
An advantage of this idea is that it could be easily extended to more than 2 simultaneous players.
Another advantage is that it can also handle attack rates of less than one attack per second without any modification (e.g. B attacks only once every two seconds, i.e. attack frequency = 0.5).
Count who has more attacks. Call him MORE. Divide MORE/LESS and take floor, the result is = N. Then for every N attacks of MORE add one of LESS and pad with attacks of MORE when finished. That's each second.
Example:
MORE = 5
LESS = 2
MORE/LESS floor = 2
Then:
MORE MORE LESS MORE MORE LESS MORE
Another example:
MORE = 3
LESS = 2
MORE/LESS floor = 1
Then:
MORE LESS MORE LESS MORE