Click here to Skip to main content
13,248,107 members (35,239 online)
Click here to Skip to main content
Add your own
alternative version

Stats

11.7K views
396 downloads
27 bookmarked
Posted 8 Dec 2015

Group Sudoku in AngularJs and SignalR

, 17 Dec 2015
Rate this:
Please Sign up or sign in to vote.
A group Sudoku game to demonstrate the usage of SignalR in AngularJs application

Introduction

There is a famous saying: "None of us is as good as all of us" (by Kenneth H. Blanchard). This simply means that if we solve a puzzle together as a team, we can solve it faster than anyone else in the team. Let's try it out with Sudoku. Yes, this Sudoku here is not about competing with your rival, this is about solving the Sudoku as a team player. Here, each one solves the Sudoku puzzle written in angular js, which will synchronize with others screen via signalR.

SignalR and web sockets has proven itself a mind blowing technology when it comes to real time update from the server in web application any field. Some of the examples include cricket score update, stock market relate app, chat application, or multi-player online games. This article will demonstrate the use of SignalR with AngularJs in group/team sudoku web application. Technically, this article can be treated as a simplified solution on how we can use SignalR with AngularJs to solve problems together, even if we reside at different geographical locations. To keep things simple, no database has been used. All data resides in static dictionary/variable, which, you might want to keep in persistence storage in your real application.

Background

Sudoku is one of the most popular puzzle games of all times. When we used to solve Sudoku, we used to get to solve it together on paper where my colleague used to help and we still used to note the time how long it took to solve it. This puzzle is just the re-invention of the same wheel, but, with multiple steering. The idea is simple, when you updated a cell, the same cell should get updated for all members of the group so that all can solve the same problem faster, together.

About the Game

The rule of the game is the same as that of popular Sudoku game. The only difference is: here you will play with your colleague instead of against him. (And all will have win-win result. :P). If you are unaware of the rules of the Sudoku game, please visit: http://www.sudoku.com/.

Below are the screenshots of the puzzle:

Using the Code

Now gear up to understand the code written to build the game. This game is developed in Visual Studio 2015, AngularJs, and SignalR.

Sudoku Class

The Sudoku class contains the grid, group name and level. The sudoku grid contains is a collection of GridRow which in turn contains an array of GridCells. GridCell stores the cell value along with other information like RowIndex, ColIndex, whether the cell is part of puzzle (Freezed property), etc.

SudokuHub and SignalR Communication

The SudokuHub class is basically the signalr hub that communicates between client and server to send the set of messages. The two of the major functions of this class is GetSudoku and UpdateCell. GetSudoku method is called when user is first time entering to the Sudoku Group. After that, UpdateCell will be called each time user updates the cell and the same cell will be broadcasted to all members of the group.

public void GetSudoku(string groupName)
{
    var group = _Gropus.FirstOrDefault(x => x.Name.Equals(groupName));
    Clients.All.getSudoku(db.GetSudoku(groupName));
}

public void UpdateCell(string groupName, GridCell cell)
{
    db.UpdateSudoku(groupName, cell);
    var sudoku = db.GetSudoku(groupName);  // updated sudoku.//
    var updatedCell = sudoku.Grid[cell.RowIndex].Cells[cell.ColIndex];

    // Trigger the update to all users of that Group.//
    Clients.Group(groupName).cellUpdated(updatedCell, sudoku);
}

// Please download the source code to see the full class code.//

AngularJs View

The AngularJs view is simply a grid that contains 9x9 cells. This is achieved by using ng-repeat on sudoku.Grid model.

//
<div class="sudoku">
        <div class="sudoku-row" ng-repeat="row in sudoku.Grid">
            <input type="number" class="sudoku-cell" 

            	ng-repeat="cell in row.Cells"

                   min="0" max="9" step="1" 

                   maxlength="1" ng-readonly="cell.Freezed"

                   ng-class="{'sudoku-cell-readonly': cell.Freezed, 
                   'has-success': cell.IsCorrect}"

                   ng-change="onCellChange(cell);" ng-model="cell.Data" />
        </div>
    </div>
//

sudokuService Service

AngularJs service named sudokuService has been used for all communication to the signalR server. The service mainly contains 3 methods: init, on and invoke. As the name suggests, init method is used to initialize the signalR server connection, on method can be used to register the event handler where a custom signalR event can be registered at client side. You need to have corresponding signalr method at server side in your signalr hub. invoke method can be used to call the server side signalr method explicitly from the signalr hub.

service.init = function () {
    var deferred = $q.defer();
    $.connection.hub.start().done(function () {
        deferred.resolve();
    }).fail(function () {
        deferred.reject();
        console.log('Could not Connect!');
    });
    return deferred.promise;
};

service.on = function (event, callback) {
    service.sudokuHub.on(event, function (result) {
        if (callback) {
            callback(result);
        }
        if (!$rootScope.$$phase) {
            $rootScope.$apply();
        }
    });
};
// Please see attached code for full source code.//

The above code snippet demonstrate the signalr service. service.init method returns the promise object so that any call can be done easily once signalr connection has been started. This can be used the same way any promise is being used in angularjs. service.on method takes 2 parameter, the event and the callback. callback is the JavaScript function that user wishes to call when the event has been triggered.

sudokuPlayCtrl

sudokuPlayCtrl is the angularjs controller for sudoku play view which handles all UI related logic for it. It mainly triggers 3 methods of sudokuService:

  1. init: To get and initialize the sudoku of present room.
  2. onCellChange: This method is called when user updates the value in Sudoku grid cell. In this case, it triggers updateCell method at server side to broadcast the same update to all members of the room.
  3. cellUpdated: This event is triggered when someone else updated the sudoku grid cell.

Create Group

Now, you can create your own group and play with your friends, colleagues or family members. For that, just click on create group link on sudoku home page and then other members can join the same group.

Things To Do

Just like any other prototype or concept, this prototype is also evolving and has a way to go to be rich in feature. Following are the things I have planned to implement in the near future depending on the feedback and my available time for it.

Create Sudoku

Currently, the concept has a hard-coded single sudoku puzzle. To make it more effective, the puzzle needs to be generated dynamically with difficulty level. I will try to provide the same soon.

Create Rooms and Invite Friends

The fun with team sudoku will come really when you will be able to create a private room and able to invite only the friend you wants to play with. You may expect this in my next update soon.

Auto-Validation Option

You may not be correct everytime. Once your friend has entered a wrong value in any cell, you might not want to get misled. In such case, validation while broadcasting the updated cell make sense.

Coloring and Status

It is also needed to have the status that each member filled how many cells and assign a color to each member. So, you can easily see each members contribution and their filled items in a different color.

More Updates

As and when I get the feedback, I will try to implement them over here and make it more feature rich. For now, this is just a simple group sudoku game where you can solve a puzzle collectively. Hope you will play and enjoy the game.

Points of Interest

Apart from signalr communication with angularjs, this article can also be treated as a sample on some basic elements of angularjs for beginners, like, how to call $q service, how to call angularjs service, etc.

History

  • 2015-12-08: Initial version
  • 2015-12-18: Create group feature added + Issue fix

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anurag Gandhi
Architect
India India
Anurag Gandhi is a Developer, Consultant, Architect, Blogger, and a Speaker. He is passionate about programming.
He is extensively involved in Asp.Net Core, MVC/Web API, Microsoft Azure/Cloud, web application hosting/architecture, Angular, AngularJs, design and development. His languages of choice are C#, Node/Express, JavaScript, Asp .NET MVC, Asp, C, C++. He is familiar with many other programming languages as well. He mostly works with MS Sql Server as the preferred database and has worked with Redis, MySQL, Oracle, MS Access, etc. also.
He is active in programming communities and loves to share the knowledge with others whenever he gets the time for it.
He is also a passionate chess player.
He can be contacted at: soft.gandhi@gmail.com

You may also be interested in...

Comments and Discussions

 
PraiseNice Article.... My Vote of 5 Pin
Nhilesh B27-Feb-16 20:38
memberNhilesh B27-Feb-16 20:38 
BugImage links appear broken Pin
Slacker0078-Dec-15 10:43
professionalSlacker0078-Dec-15 10:43 
GeneralRe: Image links appear broken Pin
Anurag Gandhi8-Dec-15 16:52
professionalAnurag Gandhi8-Dec-15 16:52 
GeneralRe: Image links appear broken Pin
bumbala428-Dec-15 21:59
memberbumbala428-Dec-15 21:59 
GeneralRe: Image links appear broken Pin
Slacker0078-Dec-15 22:00
professionalSlacker0078-Dec-15 22:00 
AnswerRe: Image links appear broken Pin
Anurag Gandhi8-Dec-15 22:19
professionalAnurag Gandhi8-Dec-15 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 18 Dec 2015
Article Copyright 2015 by Anurag Gandhi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid