JavaScript Games

I’ve recently been focusing on creating classic games in pure JavaScript.  I started with tic-tac-toe. Here is the code on GitHub and the result on GitHub Pages. I compared it with my version made in Ruby, made earlier this year, only after completion and I was happy with the improvement. In particular, use of arrays to store and compare values was valuable in the new version.

I moved on to Connect Four after that and at first found myself overwhelmed by the complexity in comparison to tic-tac-toe. Here is the JS I came up with and the result if you want to play it. There are over a lot of winning combinations and an incredible number of board combinations (over 4.5 million). I sketched out a plan and was able to come up with a solution. Each time a player drops a piece, the program finds the 24 winning combinations that piece could complete and stores them in a nested array. Then I compared each of those possibilities with the locations ‘owned’ by that player. If the player owns all 4 of those locations in that possibility, Connect Four!

Currently, I am working on wrapping up a game of Minesweeper. I attempted a different approach with the code on this project. Give it a try if you want to play it. Rather than use a nested array of coordinates (i.e. [ [0,0], [0,1], [0,2], [1,0], [1,2] … ]) to identify areas of the board (like I did with my snake game), I used a flat array. Each index stores ‘m’ for a mine, ‘f’ for a correctly flagged mine, or an integer representing the number of neighboring mines. The challenge in this approach was finding the values that are spatially above or below the referenced index. I successfully wrote a method to return an array or neighboring indexes, but I will attempt to make it more elegant in the future.

Advertisements

1 thought on “JavaScript Games”

Comments are closed.