How can I get rid of bias in my code?

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

How can I get rid of bias in my code?

Post by sheamkennedy »

In a little minigame I'm making there are two NPC's which I have coded to shoot and kill one another when I press their corresponding keys. The game works great but there is a bit of a bias. When both NPCs shoot at eachother during the same tick then the code checks which bullet collided with which NPC first... Problem is that the code is written so it always checks if NPC(0) was shot first. Since there are two simultaneous collisions but NPC(0) is checked first then NPC(0) dies and NPC(1) wins the game. This is not fair though...

Can someone suggest a way to remove this bias? I was thinking of implementing a code that checks for "ties" maybe. Or is there a way to more finely measure who was shot first?
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
Taco Bot
Meat, Cheese, and Silicon
Posts: 484
Joined: Fri Jul 18, 2014 12:15 am
Location: Santa Cruz
Contact:

Re: How can I get rid of bias in my code?

Post by Taco Bot »

Maybe you could make a random generator which decides which checks first?
Sent from my iPhone
User avatar
Gizmog
Metal King Slime
Posts: 2622
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

One way to eliminate the bias would be to not kill the NPCs immediately, but "mark" them somehow as killed and wait until all of the collisions have been checked and all of the "killed" npcs marked before deciding who wins and who loses. That way you could see if both guys have been shot at the same time.
User avatar
Urkelbot666
Slime Knight
Posts: 188
Joined: Sat Oct 18, 2014 5:29 pm

Post by Urkelbot666 »

Just out of curiosity, what would you like to have happen in the event of simultaneous hits? Count both kills, negate both, or something else?
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

Maybe there's a way you can start a stopwatch when the game starts and then have two global variables: one for at what tick NPC 0 got shot and at what tick did NPC 1 got shot, and if they're the same number, have a tie.
My pronouns are they/them
Ps. I love my wife
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Gizmog wrote:One way to eliminate the bias would be to not kill the NPCs immediately, but "mark" them somehow as killed and wait until all of the collisions have been checked and all of the "killed" npcs marked before deciding who wins and who loses. That way you could see if both guys have been shot at the same time.
This sounds good. Then I could just implement a "tie" and make it so neither player gains a point.

Thanks for all the responses. Sorry I didn't clarify what I wanted, I just wanted to see he various different things people could come up with to solve this to make sure there wasn't anything I overlooked.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
User avatar
Spoonweaver
Liquid Metal King Slime
Posts: 6466
Joined: Mon Dec 08, 2008 7:07 am
Contact:

Post by Spoonweaver »

What I would do is make a variable, let's call it 'tiebreaker'.
Then, at the beginning of your script loop, set tiebreaker to 0.
When you check collisions, if it's true on player 1, tiebreaker:=tiebreaker+1
if it's true on player 1, tiebreaker:=tiebreaker+2

then at the end of the loop, check your tiebreaker variable
if it's 1, player one hits
if it's 2, player two hits
if it's added up to 3, tie
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

Spoonweaver wrote:What I would do is make a variable, let's call it 'tiebreaker'.
Then, at the beginning of your script loop, set tiebreaker to 0.
When you check collisions, if it's true on player 1, tiebreaker:=tiebreaker+1
if it's true on player 1, tiebreaker:=tiebreaker+2

then at the end of the loop, check your tiebreaker variable
if it's 1, player one hits
if it's 2, player two hits
if it's added up to 3, tie
I went with this in the end. Seemed like the simplest fix. Works great too. It's actually quite hard to time the shots perfectly for the players to get a tie. I just didn't want one player to be at an advantage.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
� C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Post Reply