Code Monkey home page Code Monkey logo

connect4.js's Introduction

Yelp ConnectFour Coding Challenge

This project is the work of Jen Hamon. You can reach me at [email protected]

Object-oriented game design

All of my code is namespaced under ConnectFour. In the namespace, there are three important classes:

  • ConnectFour.Board: This class maintains the representation of the board state, and has several methods for querying and changing that state.
  • ConnectFour.Game: This class is responsible for tracking whose turn it is, making moves on the board, and checking win/lose conditions.
  • ConnectFour.View: This class is responsible for listening to user inputs and updating the DOM as game state changes.

All important game state lives in objects and not the DOM, making it easy to reset the game for another play after a win condition or gameover condition is met. This separation of the data from the view is critical in building single page applications that are easy to extend and modify. Although I didn't use a frontend framework in this project, I probably would if I needed to add additional subviews.

Testing

Unit-tests have been written for much of the game's logic. These tests make use of a testing technique called dependency-injection. The main constructor functions in my game (Connect4.Board and Connect4.Game) have been written to accept a configuration options object that allows us to pass in specific configuration for testing. For example, while testing my Game objects, it is sometimes useful to specify a board for testing in a particular state.

Third-party tools and libraries

This project uses jQuery library to set click event listeners and add CSS classes to appropriate DOM elements as the game progresses.

The Jasmine BDD testing framework is used to test the board and game logic. To view tests, just open the SpecRunner.html file in your browser.

I have also used the Sass CSS preprocessor to build my stylesheets. Sass gives us the ability to define variables and compute properties, which is very useful in experimenting with the layout of a geometric game board.

The production stylesheet is compiled using the sass command line program. To have changes in styles.scss reflected in the final project, I must compile with sass styles.scss styles.css. In a longer-term project, I would automate this compilation process with a grunt or gulp task.

Other approaches

I could have used a framework like Backbone.js to keep the separation between data (aka models) and views more explicit, but with a simple game like Connect4 it was easy enough to maintain the separation with a few classes written entirely in vanilla javascript. If I wanted to expand the game to have more complex features (e.g. a subview for to keep track of score/win histories, interplayer chat, or have players interacting via a backend service) I would definitely look closer at using a framework to keep the project structured and organized as it grows.

connect4.js's People

Contributors

jhamon avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.