O'Reilly logo

Express.js Blueprints by Ben Augarten, Marc Kuo, Eric Lin, Aidha Shaikh, Fabiano Pereira Soriani, Geoffrey Tisserand, Chiqing Zhang, Kan Zhang

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix A. Connect 4 – Game Logic

In Chapter 3, Multiplayer Game API – Connect we built a multiplayer game API for the game Connect 4 in which we focused on the general mechanics of creating a game, joining a game, and playing it. This Appendix shows the accompanying game logic that we omitted in Chapter 3, Multiplayer Game API main text.

src/lib/connect4.js /* Connect 4 Game logic Written for Blueprints: Express.js, Chapter 3 */ var MIN_ROWS = 6, MIN_COLUMNS = 7, players = ['x','o']; // Initializes and returns the board as a 2D array. // Arguments accepted are int rows, int columns, // Default values: rows = 6, columns = 7 exports.initializeBoard = function initializeBoard(rows, columns){ var board = []; rows = rows || MIN_ROWS; columns = columns ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required