Monday, October 19, 2015

How to properly shuffle virtual cards in software.

Last night, during one of many bouts with insomnia, I was attempting to bring on sleep by playing a mindless game,  Solitaire.   I say mindless since it requires only the tiniest bit of skill to play.  All you have to do is not miss a play.  The game will be won if it is winnable,  it depends on the order the cards come out of the deck.

This particular version of the game,  installed from the Chrome web store,  looked beautiful,  had wonderful card animation, nice sound, and terrible card shuffling!  After 30 lost games in a row,  I was quite annoyed, and further from sleep than when I began.

How do I know it was bad shuffling?   Well it was obvious by the regular discovery of two or three of the same number of card in succession.  Three Aces in a row,  or fives, etc.  This poor shuffle, at least in Solitaire,  leads to unwinnable games far more often than winnable games.

A poor shuffle might be advantageous in Poker,  to one player at least, but still a bad thing in the bigger picture.

I see this often.  Poor shuffling in a card game program.    I always remember my high school days and a certain programming contest a few friends and I had.

It was 1983.  My high school had several Apple ][+ computers in the library, as well as a couple in the vocational electronics class I was taking.   This was the early days of personal computers and almost nobody had one at home.   

Myself and a few friends, classic nerds of the day, were very much in love with these little machines.  We often fought over who would get time on them after class.  Our parents had become accustom to us staying after school until the janitor kicked us out and we trudged home to a cold dinner.   We amused ourselves with programming contests we’d come up with.  Each hoping to achieve the position of alpha male in our geeky clique.

The contest of one particular week grew out of a problem one of our group was having with trying to write a good blackjack game.    You see, we were programming in BASIC at the time, and it was not a very fast language.   The Apple ][+ had a CPU running at around 1Mhz.  BASIC, being an interpreted language was slow,  very slow,  unbelievably slow by today's standards.   Akin to comparing a formula one car to a cart drawn by a donkey.

Doug,  the kid working on the blackjack game, was rather cocky.  He was similar to Dr. Sheldon Cooper on The Big Bang Theory.  Very smart, yes, but so incredibly full of himself that we tolerated him only.   His problem was that it took well over two minutes for his card shuffle to complete at the beginning of the game.   He proposed a contest to come up with a better sorting routine.

By the end of the week, when we gathered to present our results, I was the clear winner and not by a narrow margin!    There were four of us, not counting Doug.   Doug had managed to get his shuffle down to one minute and thirty two seconds while sacrificing a bit of randomness.  Faster, but the shuffle was poor with many cards adjacent to their brothers.

The other three guys varied with one being a bit slower, and the other two beating Doug by just a couple of seconds.   All three also had somewhat poor shuffles.

My shuffle took all of four seconds.    Four,  yes,  only four seconds, in BASIC, on an Apple ][+.   Additionally, my shuffled deck was thoroughly shuffled with hardly a single instance of card pairs left. For awhile thereafter, Doug wasn't so cocky.

No,  I didn’t cheat, or use machine language.  It was a method that I came up with that I wish all programmers would use in modern card games.   Not for the speed, that hardly matters on today's incredibly fast machines.  No,  I wish programmers would use my method for the thorough randomness of the shuffled deck.

Here’s how it works.   Now,  I can’t present this in C, or Python,  .NET, or any modern language.   I’m not going to bore you with BASIC since it’s basically a dead language now.   I’m going to attempt to convey the idea,  it’s simple enough.

So first you need the deck to be shuffled.   In BASIC I simply loaded an array with numbers to represent the cards,  one through fifty two. I guess you'd use a table or something today.

Now, to shuffle them  you need a simple loop.   The loop with run fifty two times with a counter that’s incremented during each pass.   This counter will point to the position in the deck we’re working with.

For each pass of the loop,  you take the current card and exchange it with a random position in the deck.   So for pass one,  we’re working with the first card in the deck.  Pick a random number between one and fifty two and swap those two ‘cards’.

After only one pass through the loop, you’ll already have a deck that is shuffled better than what most modern card games end up with.   And it will happen incredibly fast.   Now, run the loop two or three times and you have a properly and thoroughly shuffled deck of cards.

Easy.   Simple.   So please,  programmers,  do this and properly shuffle your cards.  It will make for a much more realistic flow of the card game.


Monday, October 12, 2015

Woa!   Wow, weird....     I haven't posted here for over two years?    I kind of forgot this place existed.

I guess it doesn't help that life got really tough at work and began to encroach upon my home life to the point that I was dreaming a day at work more and more often.   Hell, one week I dreamed work three nights in a row.   Vivid,  realistic dreams with all the detail of a complete day.   That week really and truly felt like it was 8 full days long with only two nights.

I buckled under the stress and pressure,  butted heads a bit with my new boss.  The son of the owners wife's best friend, who was originally just another guy but in a few months was director of our department.   Young and reckless,  he created more work for me weekly with his mistakes.  It all came to a head and I resigned.

That was just over four months ago.   I've sent out many resumes,  but had no interviews.

On top of that, my memory is getting bad,  the days are spinning by, I'm scratching at the walls in frustration and boredom,  and feeling like I'm on a slow descent into some kind of madness.

OK,  that's all off my chest now.   Whew.

There's upsides to losing your memory by the way.  I'm re-watching The Big Bang Theory and enjoying it tremendously.  I can't remember any of the episodes, so it's like watching it for the first time.   If this is my new normal, then I've got a lifetime of television shows that I can watch for the first time all over again,  yay!  And if this trend of worsening memory continues,  eventually I can watch the same show day to day and it will always be the first time.

Come to think of it,  I think my grandfather used to do that.   Not sure if that's a memory, or my imagination trolling me.

I have an acquaintance that got married last weekend.   He's one of the modern connected generation and I've notice a big downside to the Facebook obsessed world.   When one of them get married,  you will be inundated by their wedding.

Ha!  Just realized I have another older friend that also recently got married,  it's not you I'm ranting about here buddy,  this is a more recent co-worker.

So  pre-Facebook,  you could go to a wedding, then the reception, then go home.   It was a social event for a day and then it was over.

But now, thanks to the addiction to 'likes' that people have,  his wedding has been a five day stream of pictures and short video clips, or 'vines'.     Vines are the animated GIF's of the current decade.   And each post is followed by the same stream of drivel.    "Awww so cute",  "You guys look so nice",  "Love it", "So pretty",  and on and on.

It's almost like the person that posts a selfie every few days just to get that stream of compliments that produce the endorphin response they've become so addicted to.

And since I've 'friended' him,  I am at the mercy of the waterfall of his wedding material and a bing from FB every damn time someone else comments on one of the thirty plus photos that have been posted so far.

I came back to Facebook after a year long hiatus due to the boredom of being unemployed.   I hadn't realized how much of my social life was the daily interaction with people at work.

I'd tried going to old haunts like a local coffee shop where I used to hang out with people.  Some of them were still there.  But,  being without a cell phone or Facebook,  those visits turned in to me sitting at the table and waiting for the few seconds when they put their phones down.   Followed by more minutes of sitting and waiting after they picked them back up.

I actually had someone tell me THEY were uncomfortable with me just sitting there looking at them.  Really?  You're uncomfortable with the reality of physical presence outside of your perceptual bubble emanating from your little device that you hide in?  

It was the same in other scenarios.  I was now the odd one, not connected,  forced to interrupt our conversation when they dove back under the digital surface into their small rectangular temple.

So I reluctantly rejoined Facebook.  It seemed the only way to still feel somehow connected to my friends.   And now I fight with myself to not go there too often.  To stay outside, out here in the world, on the planet, feeling like I'm really part of life on this earth.  And it's becoming a lonely place.

I walk down the street some days and don't see another soul.  Just a long row of silent houses and empty porches.   When someone is out, if even just walking to their car,  often they only glance at me and don't return my smile.    Sometimes they do, but the majority of the time I get a suspicious look, like they're wondering if I'm up to something.

How far will this go?    In ten years what will it be like?   Will the average neighborhood be indistinguishable from the set of a post-apocalyptic movie?   Will people be startled by the physical presence of another?  Are we moving toward that strange isolationist society in that Asimov story, The Naked Sun?   It certainly seems so.  Virtual Reality might actually be the catalyst that begins the final descent into the Asmovian conclusion.  And VR is on the cusp of major adaptation.  Just a few more years, or maybe only one or two.

Enough raving for now.   This post certainly seems on topic with the title of the blog.   Sorry.  Or not.