C# Tic Tac Toe

[Click here for C# Tic Tac Toe on GitHub]

In an effort to move beyond my VBA comfort zone to a “real” programming language I decided to learn a bit of C#. It should be obvious from the material on this site, but I like to learn new programming languages by developing basic games. A few years ago an intern expressed interest in learning VBA and I directed him to creating Tic Tac Toe. It seemed like a relatively straight forward game to learn the programming syntax.

I had him start with a two player version, where the players would pass the mouse back and forth to make their moves. In this mode the main functionality consists of displaying the board, accepting player inputs, and checking if there is a winner after each move. From there he added an option for a single player game against a computer opponent that would randomly pick an available move. I took the same approach in my C# version.

I never liked the fact that the computer moved at random so I spent five minutes with Google and found that there is in fact a perfect Tic Tac Toe strategy that will always result in at least a tie. However, once I programmed the computer logic and played it a few times I found that it’s not particularly fun to play against an opponent who never loses. Therefore, I went with a hybrid approach where there’s a 25% percent chance on each move that the computer might deviate from the perfect strategy and instead move at random.

Optimal Tic Tac Toe Strategy for X

The project was developed in Visual Studio and posted to GitHub if you want to play or look at the code. Because it was my first foray into C# the code is more functional than elegant. The cyclomatic complexity (a metric to evaluate and identify intricate, and therefore difficult to maintain, code) for the ComputerMove and WinnerCheck methods is off the charts - a sure sign that I could improve those methods. If you’d rather run the executable without having to look at all the code you can find it here.


Following last week’s Mashable article this site and the projects I’ve developed have received a lot more attention. After speaking with my colleague and fellow Tumblr user Sean / Birdasaur I decided to move my files from Google Drive to GitHub. It seems to be the industry standard for this type of thing and will better enable me to share projects in C#, Python, and other languages moving forward. At some point soon I’ll edit my older posts to point to the new GitHub repositories, but for now here’s where you can find the latest version of each project:

Binary Converter
Excel Trainer

Excel w/VBA
Blackjack Strategies
Excel Monopoly
Ms. Pac Man
Rubik’s Cube Solver
Trivia Game
VBA Trainer

PowerPoint w/VBA
Chutes and Ladders
Dynamic Presentation

In response to some recent user comments, I’ve updated the Excel Monopoly file. Now you can select your icon from the eight pieces: car, dog, hat, iron, ship, shoe, thimble, wheelbarrow. There’s even logic to yell at you if two people have the same icon. Just note that you have to hit the (re)start button to get the right icons.

15 Minutes of Fame

Upon a routine check of my email on Wednesday I saw a dozen new Tumblr followers and several questions about the Excel Monopoly game I made back in 2011. I was confused until a call a few minutes later from a coworker who had seen the project featured on Mashable. The article was also referenced on the Gamelogical portion of the AV Club site, which I came across organically later that night. The article highlights my project along with five others, which are all fantastic.

Back in 2011 I used this site as a means to display my skills and interests as I changed industries. After starting a new job and having a child there has been a considerable absence. This article has inspired me to dust off the site and my VBA skills.

I have been working on several projects over the past few years, branching out into C# and Python. I’ll be posting more frequently in the coming weeks with the intention of at least one update each month.

Thanks to Mashable for the press!

Information Design: Space Flight

I’ve been a big fan of information design after hearing Edward Tufte speak a few years ago. Since starting a new contracting job with NASA at the beginning of the year my friend and designer extrodinaire Carolyn Sewell has sent me two amazing infographics that I felt compelled to share. One is by the design firm MGMT and one is by Corey.

I think it’s fascinating that since 1961 there has been a US citizen in space for all but ten of those years. My only exception is the omission of the International Space Station, which still fits into the category of manned flights. It seems like we’re due for another gap like the one from ‘76 to ‘80, although at that time we had a plan in mind and were working on the shuttle.

When it comes to the future of US space exploration my views fall in line with Neil deGrasse Tyson, who was great on a recent Daily Show. We’ve got to get back up there, and go farther. The technology that got us to the moon resulted in a multitude of advances here on earth. Not to mention the inspiration for future scientists, engineers, and mathematicians. I think it is fantastic that the government spent $3.4 billion on STEM initiatives last year, but why not add that to NASA’s $18.7 billion budget and do something truly inspirational.

As for this second diagram, I can think of no omissions. I really like including a missile, because who knows what you’re going to come across in space. Well done sir.

Trivia Game

[download the Trivia Game here]

I found my first thumbdrive the other day while cleaning so I popped it in the laptop and was treated to 256Mb of nostalgia. Among the files was quite possibly the first thing I ever wrote in VBA - a trivia game in the vein of Jeopardy. I was a year or two out of college and my former roommates and I were getting back together. I decided to put together a few questions about our group referencing inside jokes and the stupid things we did, with each column / category as a different roommate.

As a word of warning, this was my first foray into VBA so the code is not particularly well documented or well done. Yet I wanted to preserve this initial work so I didn’t clean up the code. I did however change the questions to protect my roommates. Now it is set up as a Saved By The Bell trivia game, mainly because I could think up all the questions and answers in a few minutes.

The file can easily be modified for your own purposes. The second sheet has a matrix of all the questions and answers. So feel free to make it your own (and clean up the code if you want). However, it does require someone who knows all the answers to run the game.

Relaxing and Reading

It’s been a relaxing holiday season. Even more so because I took two weeks off while changing jobs. I started on a handful of projects that I’m sure will show up on this site in the near future. I was also able to catch up on quite a bit of reading, including the fantastic Ready Player One.

The novel is set in the not too distant future where a Steve Jobs type wills his fortune to anyone who can find a secret he’s hidden in a virtual world. Oh, and all of the clues and trials to reach the secret are centered around 80’s trivia, video games, and nerd culture. It’s Ernest Cline’s first novel and at times it shows. The foreshadowing can be pretty heavy handed, leading to plot twists you can see coming from miles away. But overall I really enjoyed it, and apparently it’s going to be made into a movie. In fact, Warner Brothers bought the rights a year before the book was published. Although I’m not sure how exciting an epic game of Joust will be…

Completed Powerbook Mini Arcade

In May I got off to a strong start creating a miniature arcade cabinet using an old Mac Powerbook. The project quickly moved from a scale model to a full size cardboard model with working controls. Then it sat for several months. With some time off around Thanksgiving I was finally able to construct the completed Powerbook Mini Arcade.

On the first of what would prove to be many trips to Home Depot I picked up two sheets of MDF (2’ x 4’ x 1/4”), hinges for the control panel, and assorted screws, nails, and bolts. Although most full size homemade arcade cabinets tend to go with 3/4” thick MDF I figured I could get away with 1/4” because mine wouldn’t have to support much weight. After outlining the cuts in pencil on the MDF I got to work with the circular saw and drill to cut out each piece. I tried cutting the curved sections on the arcade’s sides with a reciprocating saw but that made a bit of a mess out of the MDF so I ended up doing that with box cutters.

To check that I had not made any major mistakes I taped the whole thing together to see how it would look. Not too bad.

As I started to put the buttons into place I realized that the holes I had drilled for them were too small. The buttons themselves are one inch in diameter, which is how I drilled the holes, but the screw threads on the bottom of the button made it too big to fit. After sanding out the holes and attaching the hinges the control surface was assembled.

Now it was time to assemble the rest of the cabinet. Initially I wanted to nail the pieces directly together, but 1/4” was too thin to line up the pieces properly and the MDF might start falling apart when working that close to the edge. To get around this I decided to add some framing pieces of wood inside the cabinet. I screwed the sides to these frame pieces and nailed the other boards to the wood. In theory this means I could unscrew a side to take it off and make changes. Once the cabinet was constructed I drilled out holes on one side to access the power and USB ports.

When I moved the controls from the cardboard model to the final cabinet I had to unplug some of the buttons which resulted in a bit of rewiring. Working with the control wiring is by far the least enjoyable aspect of this entire project. The connections to the keyboard circuit are incredibly finicky and I’m concerned that if I frequently open and close the control panel something will come undone. I’ll likely end up buying a fabricated connector like the mini-pac and scrapping my keyboard hack but this will work for now. As before, once I had each button hooked up I opened Excel on the Mac to ensure that every button press or joystick move corresponded to the appropriate key.

With the cabinet constructed and the controls wired all that was left was to start up some emulators, configure the inputs to use the appropriate keys, and start playing. Which is exactly what I did. Although after a bit I turned it over to my wife to test out Ms. Pacman. It worked great.

All told this entire project took roughly twenty hours although that included making scale and full size models, wiring the controls twice, and at least a half dozen trips to Home Depot. I didn’t add up all the costs but it’s probably somewhere around $50 with the most expensive part being the joystick at around $20. This doesn’t include the cost of tools, or the laptop and keyboard that I already had lying around. In the future I’d like to pretty up the exterior of the cabinet and replace my keyboard hack controls with a solution that is less prone to coming apart over time. But for now I can play old school video games with old school arcade controls and that’s good enough for me.

Arcade Cabinet Dimensions

A few months ago I made an arcade cabinet for an old Mac PowerBook out of cardboard. It was a proof of concept, not meant to last, and used some pretty rough measurements. Having done that I went back and measured more carefully, coming up with a plan to create a final arcade cabinet. Looking at websites for similar projects 3/4” MDF seems to be the standard construction material so that’s what I used in my plans. The images below show a side view of what the constructed arcade would look like and how those pieces could be cut from a pair of 2 x 4’s.


This is one of those times that I really miss college. Not because of the partying and free time, but because of the CAD programs and power tools that I had easy access to in the Physics department at JMU - although the parties were great. Instead I drew these plans up in PowerPoint, paying close attention to the dimensions so the scale should be correct. Next up is actually buying the MDF, making the cuts, and putting this thing together. I’ll be sure to take pictures throughout the construction process for a future post. Hopefully I can have this completed by the holidays.

Lego Rubik’s Cube Solver

When I finished the Excel Rubik’s Cube solver I felt that it was a pretty nice little product. It was tricky creating the logic and determining the correct permutations, and rewarding to see it all work properly at the end. Then I look at an article like this and am blown away. These two guys built a Rubik’s Cube solver out of Lego and a smartphone that can solve a cube faster than any human. Now that’s impressive. I know that Lego has evolved quite a bit since I put together the Black Seas Barracuda in the late 80s, but I had no idea it could interface via Bluetooth. Back then it was groundbreaking that the figures had eye-patches and facial hair - now they’re wirelessly communicating with a phone to solve a Rubik’s Cube.

Rubik’s Cube Solver

[Click here to download the Rubik’s Cube Solver]

I have never been able to solve a Rubik’s cube. I can get to the point where one side is correct before getting frustrated. But then I invariably try to get one more piece in place and end up ruining the whole thing. With that in mind, I decided to write a computer program that would solve a Rubik’s cube for me.

First, I wanted a way to display the cube and manipulate it. Displaying a 3D surface on a 2D computer screen is always a bit tricky because you can’t see the entire object. My initial thought was to show the “unwrapped” cube, where the six faces are joined at the appropriate edges. For rotation the user would be able to move any row or column on the front face in either direction. Because they could only manipulate the front face I added the capability to rotate the entire cube to show any of the adjoining faces.

This was a mess.

The “unwrapped” cube creates issues because no matter how you unwrap it one of the faces will be upside-down from what it should intuitively look like. Additionally, it’s restrictive to think about rotating the cube by only manipulating the front face. A quick search showed that there is actually a standard nomenclature for manipulating a Rubik’s cube. F denotes rotating the front (F) face of the cube 90 degrees clockwise. F’ is a counterclockwise rotation.

With this knowledge I modified my display to show each of the six faces independently, with buttons to rotate them clockwise or counterclockwise. I added similar buttons and icons to rotate the entire cube along any of its three axes. In the earlier version I had created a 3D rendering of the cube that displayed the front, up, and left faces. I added a see-through view of the cube next to this so that the back, right, and down faces of the cube would be visible in 3D as well.

Once I had written the code to rotate the faces and cube appropriately I added two buttons. The first to reset the cube to a solved state - the equivalent of peeling the stickers off and putting them back in the right place. The second to randomly perform some number of rotations along different faces and axes until the cube was nicely randomized, but still guaranteed to be solvable. Finally, it was time to add a button to solve the cube.

There are a variety of ways to solve a Rubik’s cube and this is by no means the most efficient. I wanted to follow a structure that led itself to programming and could potentially teach a person how to solve the cube themselves. The process treats the cube as having three layers, like a cake. It starts by solving the top layer, then the middle, then the bottom.

A cross of a single color is formed first on the top layer, and then the corner pieces are put into place. It is important that not only are the colors on the top correct, but the edges as well. Having done this there are only four spots in the middle layer that might need adjusting and only four spots that they could potentially be. To solve the bottom layer the program actually flips the cube over and then solves the upper layer again. However, it uses a more complicated set of rotations to ensure that it doesn’t change anything in the other, completed layers.

With the solver algorithm developed I decided to add a second worksheet that would list the steps so the user could follow along. The idea is a user could color the squares to match a real cube and then run the solver to discover what steps to take in order to solve it. They could then follow each step, rotating their own cube, and see how it is solved. If nothing else, at the end of the process they would have a completed cube to impress and amaze their friends.

Because my solver algorithm follows this layer concept it is by no means the most efficient way of completing a cube. Running it on randomly scrambled cubes I’ve found that it generally takes between 200 and 250 steps to solve it this way. At some point in the future I might look at optimizing my solver to use fewer steps I’m fairly happy right now that the thing actually works. Enjoy!