Sunday, December 4, 2016

Fantastic Bot for Fantastic Bits

The winter 2016 CodinGame contest, Fantastic Bits, just wrapped up and I am incredibly excited by how well I was able to do.  This was my fifth contest since joining the site in spring this year, but it was the first time that the contest has fallen on a week where I've been relatively free to focus without distractions since my initial participation in the Smash the Code contest.  The results definitely reflect this, as I finished 4th in the country and 42nd overall out of 2400 entries from top programmers around the world.  In fact, in the final few minutes I climbed as high as 1st in the country and 18th overall, before some last minute changes and optimizations by other players caught me off guard.



Needless to say, I am extremely proud of how well my bot programming turned out.  I used C# for my language this time (and finished as the 6th best C# entry), which meant that I had no problems with performance and was able to do simulations and calculations plenty fast.  The contest this time was a video game version of Quidditch, where you control 2 wizards trying to score goals against your opponent while avoiding bludgers and using flying, throwing, and magic.

The big challenge in this contest was the number of different choices to be made each round.  With 2 wizards, up to 7 balls, 2 bludgers, 2 enemies, and 4 potential spells to use, the options quickly compound.  Early on, I struggled with how to best handle this, until I came up with a generic command and ranking architecture.  This allowed me to iterate through the breadth of options, generate ranked values for each possibility, and then execute whichever ones gave me the best possible score.

Other factors that strongly contributed to performance were how to calculate which wizard pursued which balls, optimizing the trajectory and thrust trigonometry for best intercept, and finally simulating trajectory of shots on goal to avoid getting blocked and maximize scoring potential.  By having a generic system that assigned value ranks to each action, I was also able to adjust weighting and scoring ratios to optimize my wizards' AI behaviors.  This made a big difference once I broke into the top 100, because I needed a way to genetically adjust to wins and losses in order to slowly climb up the ranks.

This was another great, engaging contest and I'm sad that it's over.  I can't say enough good things about CodinGame.  I look forward to seeing reading the breakdown of the contest once all the data is analyzed, and I hope I can learn enough to improve on my performance even more for next time and maybe threaten for a top spot.

Tuesday, May 10, 2016

Smashing Code

In a recent post, I talked about CodinGame, a website full of programming challenges, games, and AI puzzles.  Well, this past week, they also ran a week long, competitive programming challenge called Smash the Code.  Apparently they hold these periodically and some companies use them as recruiting opportunities, sort of like a more fun version of a technical coding interview problem.

Since I've been enjoying myself so much wiriting code to solve the games and puzzles, I figured I'd give the contest a shot to see how I do.  The contest was essentially a week long hackathon to write AI code to control one side of a 2 player Tetris style game.  The rules got more complex as you progressed through different tiers, but the basics were that you tried to clear rows on your side and pile up rows on your opponents and whoever scored more points or stayed alive the longest won.

Once you were happy with your code, you could choose to submit it against the other players in your current tier, at which point they ran 100's of simulations to determine your respective Elo rating.  If you ranked higher than that tier's baseline "boss" you leveled up to the next tier, and if not, it was back to the drawing board.

I'm happy to say that, even though I don't have any formal game training, my programming skills were enough for me to earn my way to a finish in the top 20% overall and 22nd in the country.  This was despite choosing to program in JavaScript, when the main program involved a lot of breadth and depth simulations and had a 100 ms time limit per round.  Not surprisingly, many of the top finishers used the more efficient choices of C++, C#, Java, etc. while the top JavaScript finisher was 61st.



Even though my program was only able to simulate about 4 rounds ahead, I was pretty happy with the solution I came up with for my first contest.  It ended up being flexible and able to be tuned with different weighting parameters that allowed me to tweak the behavior and helped me make a last minute push up the rankings.  Not bad for the first time, but I'm already thinking of ways to improve for next time...

Friday, April 15, 2016

Games and AI

A few weeks ago on Facebook, I saw an ad for CodinGame advertising games for programmers.  Despite the fact that I work full time as a software developer and love writing good, pretty code, I've never really managed to get any good projects started in my spare time like working on open source team.  Of course I do a lot of dabbling, creating tools to optimize my international travel plans, programming hobby robotics, building up my home automation network, and of course, volunteering at the museum.

Still, I hadn't found much to fill random bits of free time when I want to flex my programming muscles without starting a long term project, but so far, CodinGame has been the perfect fit for just that.  It's a treasure trove of programming challenges divided into different categories and difficulties, each with a fairly well made backstory and graphics.  The stories so far have included the Matrix, Terminator, space travel and are a fun way to give a contrived programming task some flavor.

I'm quickly learning fundamentals of game development and AI that I haven't been exposed to working on more business-y software like backtracking, pathfinding, and depth and breadth first searches.  I'm also seeing how concepts I'm already familiar with like binary searches, recursion, and simulation apply in different ways, and, of course, I'm brushing up on a lot of geometry and trigonometry.  In addition, since they have IDE's for lots of languages, it's a great tool to stay fresh in different languages, like writing some JavaScript when you've been doing tons of backend C# or some C++ when everything you've been working on has been managed code.

I haven't been signed up long, but I've already flown through a bunch of the easy ones, created solutions that I'm pretty proud of on some mediums, and started to unlock the hard levels.  I know it's not productive in the same way as open source projects, but it's definitely helping me learn, grow, and stay sharp as a developer.  I don't totally know how they make money, because there don't seem to be many ads given the quality of the platform, but while it lasts, I'm happy to have it as an outlet for some of my programming energy.

Friday, January 15, 2016

My First Maker Project

We're going through a bit of early year cleaning, and I came across the remains of what I consider to be my very first "maker" project.


What is it you might ask?  Well, believe it or not it's a projector made from a bunch of chipboard, a dissected LCD screen, a Fresnel refracting sheet to focus the image, a map loupe as a projecting lens, and a work light to power the whole thing.  I forget when exactly I built this, but I think it was summer 2003, when the cheapest projectors were close to a grand and the discount, pocket sized ones we have today were still a long way off.

I made the whole thing with hand tools in my parents' basement over a college break.  It's safe to say it wasn't HD quality (the edges of the screen had the hazy border like TV sitcom dream sequences), but given time it was built and the materials it required, I thought it was pretty impressive.

The front piece slid on 4 carriage bolts as a focusing mechanism and the lid had a couple small fans built into it to prevent overheating.

Here's an example of my design-on-the-fly approach.  I was able to keep the monitors control board attached and just bend the ribbon cable 90 degrees to expose the controls and VGA port on the outside of the projector.

I took inspiration from other projects on the internet, but like many of my projects, pretty much designed things as I went.  Besides learning about optics and of course, having a passably working projector, the coolest thing about building this was taking apart an LCD screen and realizing that the image on the screen is displayed on a transparent layer, which is simply lit from behind by a light with a diffuser.  Here's a pretty nice video showing what I mean: