Wednesday, November 9, 2011

Using design patterns to build part of a soccer game engine

My goal here is to see if  I can use and understand design patterns and use them to solve a real world problem. I started by first learning the singleton pattern  and used it in my Dynamic quiz application using ASP.net MVC and Entity Framework Code First. I want to design a soccer game engine .Its a big a complex problem to solve but the best way to solve such a problem is to divide it into smaller parts that contains all components of building a soccer game engine. For this post I will focus on the problem of letting the referees  and players know the current position of the ball always .I play soccer and love soccer so lets get real now to see what will be needed for this
  • A soccer game must have a ball
  • They are 11 players in each team
  • There are two teams
  • Two  lines men and one referee
  • Each team has reserve players
  • There is the playground of course
The ball is the center of attention and  everybody of interest for that game wants to know where the ball is and whats happening to it . Who  are the watchers  of the ball ?
Here is where the first problem comes in .
How do I solve the problem where the referees and players know the current position and get updates of  the ball position  as its played on the field.
Lets define some  real objects needed to solve the problem
  1. Ball
  2. Players
  3. Playground
  4. Referee
  5. Team
Now that we have  some real soccer objects you will find on every soccer game. We need to  create some logical objects (objects that connect the real world objects and use  programming logical controls and give them  some life)


 The Game object defines a football game by  putting all components needed for a soccer game together like the players , ball etc...
The SoccerGame object will simply simulate  a football game.


Here is an abstract view of the system as I have described above.


Using design patterns is not the first thing you think when you  want to solve a problem but  they provide good software design principles that have stood the test of time thanks to the "Gang of Four"(GOF). Any way you can read more about this here design patterns. Since I want to know how to actually use them the best way is to try to use them in real world scenario.

I am a visual person so the best way  to see the problem is to get a visual of the problem .Here  is the way i see it below
The pattern that fits this is the Observer pattern which says that "Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically." In our case  all objects are dependent on the ball for a  soccer game and  the players, referees  must be notified and updated  as it changes state(moving).
The observer pattern in its raw state is shown below




The next step is to put the soccer problem in the context of the  observer pattern  and  this time there is  a position  object in three dimensions(x,y,z)  to connect to the change of the ball state since it describes  the current position on the play ground. Changes of the state of the ball is reflected by the position .It is the state that every observer has be notified and interested  about.


The subject is the soccer ball
The concrete object is the actual football that will be kicked
The observers are the players and referees and they  implement the IObserver interface.


The ball must have a list of observers
The observers have a pointer to the football object
The positions of  all the participants with the ball in the game has to be known.


Now how do we implement this in  C# ? Here is where I have to shut my mouth and get a prototype of the game engine for my customer.


Here is the code of the Game Engine only .It brings everything together

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SoccerGame
{  
    /// 
    /// Simulating  part of a soccer game using the Observer pattern
    /// 
    public class GameEngine
    {

        public static void Main()
        {
            // Create our ball (the ConcreteSubject)
            var ball = new FootBall();

            // Create few players ( some ConcreteObservers)
            var messi = new Player(ball, "Messi");
            var etofils = new Player(ball, "Etofils");
            var Ibrahimmovic = new Player(ball, "Ibrahimovic");

            // Create some referees (also ConcreteObservers)
            var collina = new Referee(ball, "Collina");
            var ngala = new Referee(ball, "Ngala");

            Console.WriteLine();

            // Attach them with the ball
            ball.AttachObserver(messi);
            ball.AttachObserver(etofils);
            ball.AttachObserver(Ibrahimmovic);
            ball.AttachObserver(collina);
            ball.AttachObserver(ngala);
            Console.WriteLine(" After attaching the observers...");

            // Update the position of the ball. 
            // At this point, all the observers should be notified
            ball.SetBallPosition(new Position());
            Console.WriteLine();


            // Remove some observers
            ball.DetachObserver(etofils);
            ball.DetachObserver(collina);
            Console.WriteLine(" After detaching the refree Collina and Etofils from the ball...");


            // Updating the position of ball again
            // At this point, all the observers should be notified
            ball.SetBallPosition(new Position(10, 10, 30));
            // Press any key to continue....more things happen
            Console.Read();
        }
    }
}
That is it for now and happy programming !

No comments:

Post a Comment

Machine learning is the future

I am very enthusiastic about machine learning and the potential it has solve tough problems. Considering the fact that the amount of data we...