Click here to Skip to main content
Click here to Skip to main content

Daadi - A Strategy Board Game

By , 15 Oct 2012
 

Please note

This article is an entry in our AppInnovation Contest. Articles in this sub-section are not required to be full articles so care should be taken when voting.

Sample Image - maximum width is 600 pixels

Introduction

Daadi (means ‘attack’ in Telugu Language) is an ancient strategy board game from India. It is also well known as Nine Men Morris or Mills in the western countries.  This is a two player game.

This application is written in WPF and C# for a Windows 8 Desktop App. 

Background

This is a game played by two members. Each player gets nine pawns. The goal is to form mills and knout out the opponent players pawns to reduce their count to two or block all the moves of the opponent.

Each player has 9 pawns which move among the available boards 24 spots. 

The game starts with an empty board. The game starts by players placing their 9 pawns empty spots on the boards taking turns. 

Once the all the pawns are placed on the board. Now the players take turns to move the pawns to the neighboring spots in an attempt to form a Mill. (A Mill is formed when three pawns of a same player are in a row on one of the board’s lines). On successful Mill formation, the player can remove an apposition player’s pawn from the board. A pawn once removed cannot be used again.

Players continue to play until the opponent pawn count reduced to 2 or blocking all the moves for the opponent player leaving him unable to play. 

Screenshots

Sample Image - maximum width is 600 pixels

Sample Image - maximum width is 600 pixels

Using the code

On start, Main Window loads the Board intially and keeps track of location of each of the 24 spots on the board.

    public MainWindow()
    {
        this.InitializeComponent();
        Common.InstantiateMainWindow(this);
        LoadBoard();
    }

Player class that holds the details of a Player including score.

    public class Player
    {
        public Player(int id, string name)
        {
            PlayerId = id;
            PlayerName = name;
        }

        public int PlayerId;
        public string PlayerName;
    }

When a spot/pawn is selected, the following code handles all the rules and places/moves the pawn accordingly.

    void PawnCanvas_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Common.ShowMessage(string.Empty);

        bool isPawnAvailable = IsPawnAvailable();
        if (isPawnAvailable)
        {
            if (IsOccupied == false)
            {   
                DisplaySelectUI(this);
                Occupy(Common.CurrentPlayer);
            }
        }
        else if (!isPawnAvailable)
        {
            if (IsOccupied == true)
            {
                if (Occupant.PlayerId == Common.CurrentPlayer.PlayerId) //selecting to MOVE only if it the Players turn
                {
                    if (Common.SourcePawn == null)
                    {
                        Common.SourcePawn = this;
                        DisplaySelectUI(this);

                        if (PawnClicked != null)
                            PawnClicked(this, new PawnClickedEventArgs(null));
                    }
                    else if (Common.SourcePawn.Occupant.PlayerId == Common.CurrentPlayer.PlayerId)
                    {
                        Common.SourcePawn = this;
                        DisplaySelectUI(this);
                    }
                }
                else if (Common.SourcePawn != null) // If the right player is moving and if the current pawn is occupied
                {
                    Common.ShowMessage("You can only move a pawn to an empty locataion.");
                }
                else // If the player is trying to move opposition pawn
                {
                    Common.ShowMessage("Please select a " + GetPlayerPawnColor(Common.CurrentPlayer).ToString() + " Color pawn");
                }
            }
            else
            {
                if (Common.SourcePawn != null) // If not occupied and the source pawn is already selected to move
                {
                      
                    if (Common.IsNeighbor(Common.SourcePawn, this)) // Allow to move only if the source and destination are neighboring locations.
                    {
                        DisplaySelectUI(this);
                        Common.DestinationPawn = this;
                        Move();
                    }
                    else
                    {
                        Common.ShowMessage("You can only move a pawn to a neighboring location.");
                    }
                }
                else // If the player has not selected anything move
                {
                    Common.ShowMessage("Please select a " + GetPlayerPawnColor(Common.CurrentPlayer).ToString() + " Color pawn");
                }
            }

        }
    }


    public void Occupy(Player player)
    {
            
        Occupant = player;

        if (Occupant.PlayerId == 1)
        {
            this.FillPawn(Common.PlayerAColor);
        }
        else
        {
            this.FillPawn(Common.PlayerBColor);
        }
            
        Common.CurrentPlayer = Common.SecondaryPlayer;
        Common.SecondaryPlayer = Occupant;

        if (PawnClicked != null)
            PawnClicked(this, new PawnClickedEventArgs(Common.SecondaryPlayer));

    }

    public void Leave(Player player)
    {
        Occupant = null; ;
    }

    public void DisplaySelectUI(PawnUC myPawn)
    {
        foreach (UIElement element in Common.myMainWindow.myCanvas.Children)
        {
            if (element is PawnUC)
            {
                if ((PawnUC)element != myPawn)
                {
                    ((PawnUC)element).SelectedEllipse.Visibility = Visibility.Collapsed;
                }
                else
                {
                    ((PawnUC)element).SelectedEllipse.Visibility = Visibility.Visible;
                }
            }
        }
    }

    public bool IsOccupied { get { return (Occupant != null); } }
    public Player Occupant { get; private set; 


    private void Move()
    {
        if (Common.CurrentPlayer.PlayerId == 1)
        {
            Common.DestinationPawn.FillPawn(Common.PlayerAColor);
        }
        else
        {
            Common.DestinationPawn.FillPawn(Common.PlayerBColor);
        }

        Common.SourcePawn.FillPawn(Common.DefaultColor);
        Common.SourcePawn = null;


        Occupant = Common.CurrentPlayer;
        Common.CurrentPlayer = Common.SecondaryPlayer;
        Common.SecondaryPlayer = Occupant;
        CheckForMills();
    }



What's Next

  • Validting the Mill formation, tracking the scores.
  • Improving the User Interface.
  • Adding the touch screen capatility.
  • Animating the moves and more..

Points of Interest

When I was trying to find out more about ancient board games from India, I encounterd the following website. kreedaakaushalya.blogspot.in It was very facinating to find out the history of various board games.
"Courtesy: Ramsons Kala Pratishtana. kreedaakaushalya.blogspot.in"

License

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

About the Author

uday-r
Software Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow does this make use of the features of an Ultrabook?adminChris Maunder15 Oct '12 - 8:21 
AnswerRe: How does this make use of the features of an Ultrabook? [modified]memberuday-r15 Oct '12 - 8:29 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 15 Oct 2012
Article Copyright 2012 by uday-r
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid