I've got a little mathematics problem here.. It's got me a little stumped so I'm posting it here in case anyone might be able to help solve it before I do.
I have a palindrome that depends on the number of players alive in game. What I need help with is a function pseudocode that takes 3 parameters.
int player1
int player2
The function needs to return the distance between the players. Players are considered to be sitting in a circle. Player indexes range from 0 to 7.
For example:
getDistance(0,7) would return 0, because player 0 and 7 are next to each other. (Circle seating)
getDistance(1,6) would return 3. However, if player 2,3 and 4 are dead. It should return 2.
Let the array storing player death states be:
bool[8] isDead; (True/False)
I've checked the net.. most of them tell how to draw out a palindrome string, which I already know.
Please take note, I can't accept a hard coded solution. Because I'm very particular with clean code. I'm looking for some kinda formula if one even exists. If anyone can figure this out that would be seriously awesome.
Any help would be greatly appreciated.
Edit: If there's a simpler solution, I'm open to it. Also, If a solution doesn't exist, please provide proof to why.
assuming there are no empty slots (correct? Or is my logic flawed).
maxslot for full 8 players would be 8.
So what you do is, if a player leaves the game, you internally reduce slots by 1 and re-arrange the players (if 5 leaves, 6 becomes 5, 7 becomes 6 and maxslots becomes 7)
You can always store the original slots as well and keep them visually.
This was the first I came up with, maybe this is not what you want or there is a better solution.
The perfect solution. Similar to what I came up with, but better. Thanks for helping me out. Owe you one :) I'll need to adapt it a bit though as my intended application has structs indexed by each of the player numbers 0-7 (8 players). But thats cakewalk so two thumbs up!
well i should have used the code markup, but i thought that it doesnt matter since the code should be also readable like this.
my solution should work without changing the player indexes. but would have a higher runtime cause of the selfcalling function (sill linear to num of player)
Returns 1 when they're directly near each other, 0 when both players are the same.
Edit: Too late...? :( 'sob'
Don't worry, I'm still open to solutions. I gave it a try and it works VERY nicely. Even less lines of code that what I had before. I must give my compliments that you are s3riusly amazing. Thanks for the pseudocode :)
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
Hey Y'all,
I've got a little mathematics problem here.. It's got me a little stumped so I'm posting it here in case anyone might be able to help solve it before I do.
I have a palindrome that depends on the number of players alive in game. What I need help with is a function pseudocode that takes 3 parameters.
The function needs to return the distance between the players. Players are considered to be sitting in a circle. Player indexes range from 0 to 7.
For example:
Let the array storing player death states be:
I've checked the net.. most of them tell how to draw out a palindrome string, which I already know. Please take note, I can't accept a hard coded solution. Because I'm very particular with clean code. I'm looking for some kinda formula if one even exists. If anyone can figure this out that would be seriously awesome.
Any help would be greatly appreciated.
Edit: If there's a simpler solution, I'm open to it. Also, If a solution doesn't exist, please provide proof to why.
I would internally re-arrange the positions if one player leaves. The distance for full players is always kinda like this:
assuming there are no empty slots (correct? Or is my logic flawed).
maxslot for full 8 players would be 8.
So what you do is, if a player leaves the game, you internally reduce slots by 1 and re-arrange the players (if 5 leaves, 6 becomes 5, 7 becomes 6 and maxslots becomes 7)
You can always store the original slots as well and keep them visually.
This was the first I came up with, maybe this is not what you want or there is a better solution.
dont know if its correct, i just writed down what came into my mind...
int getDistance(int a,int b){ switch(abs(a-b)){ case(0): return 0 case(1): if(isDead[a] OR isDead[b]) {return 0} else {return 1} case(maxplayers-1): if(isDead[a] OR isDead[b]) {return 0} else {return 1} case(default): if(a==(maxplayers-1)){new_a=0}else{new_a=a+1} return (getDistance(a,new_a) + getDistance(new_a,b)) } }
@Kueken531: Go
The perfect solution. Similar to what I came up with, but better. Thanks for helping me out. Owe you one :) I'll need to adapt it a bit though as my intended application has structs indexed by each of the player numbers 0-7 (8 players). But thats cakewalk so two thumbs up!
@Infirisios: Go
You gotta hate how this forum ignores your linebreaks when you type =/
That should do the trick.
Returns 1 when they're directly near each other, 0 when both players are the same.
Edit: Too late...? :( 'sob'
@FuzzYD: Go
well i should have used the code markup, but i thought that it doesnt matter since the code should be also readable like this. my solution should work without changing the player indexes. but would have a higher runtime cause of the selfcalling function (sill linear to num of player)
Don't worry, I'm still open to solutions. I gave it a try and it works VERY nicely. Even less lines of code that what I had before. I must give my compliments that you are s3riusly amazing. Thanks for the pseudocode :)