Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#

ReversiEight - A Windows 8 Reversi Game

Rate me:
Please Sign up or sign in to vote.
4.97/5 (36 votes)
22 May 2013CPOL11 min read 44.7K   695   24  
Describes the development of a Windows 8 Reversi game, covering the topics of UI design, the minimax algorithm and an interesting use for Linq.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ReversiEight.Common
{

  public class GridUtils
  {
    #region RowDefinitions attached property

    /// <summary>
    /// Identified the RowDefinitions attached property
    /// </summary>
    public static readonly DependencyProperty RowDefinitionsProperty =
        DependencyProperty.RegisterAttached("RowDefinitions", typeof(string), typeof(GridUtils),
            new PropertyMetadata("", new PropertyChangedCallback(OnRowDefinitionsPropertyChanged)));

    /// <summary>
    /// Gets the value of the RowDefinitions property
    /// </summary>
    public static string GetRowDefinitions(DependencyObject d)
    {
      return (string)d.GetValue(RowDefinitionsProperty);
    }

    /// <summary>
    /// Sets the value of the RowDefinitions property
    /// </summary>
    public static void SetRowDefinitions(DependencyObject d, string value)
    {
      d.SetValue(RowDefinitionsProperty, value);
    }

    /// <summary>
    /// Handles property changed event for the RowDefinitions property, constructing
    /// the required RowDefinitions elements on the grid which this property is attached to.
    /// </summary>
    private static void OnRowDefinitionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      Grid targetGrid = d as Grid;

      // construct the required row definitions
      targetGrid.RowDefinitions.Clear();
      string rowDefs = e.NewValue as string;
      var rowDefArray = rowDefs.Split(',');
      foreach (string rowDefinition in rowDefArray)
      {
        if (rowDefinition.Trim() == "")
        {
          targetGrid.RowDefinitions.Add(new RowDefinition());
        }
        else
        {
          targetGrid.RowDefinitions.Add(new RowDefinition()
          {
            Height = ParseLength(rowDefinition)
          });
        }
      }
    }

    #endregion


    #region ColumnDefinitions attached property

    /// <summary>
    /// Identifies the ColumnDefinitions attached property
    /// </summary>
    public static readonly DependencyProperty ColumnDefinitionsProperty =
        DependencyProperty.RegisterAttached("ColumnDefinitions", typeof(string), typeof(GridUtils),
            new PropertyMetadata("", new PropertyChangedCallback(OnColumnDefinitionsPropertyChanged)));

    /// <summary>
    /// Gets the value of the ColumnDefinitions property
    /// </summary>
    public static string GetColumnDefinitions(DependencyObject d)
    {
      return (string)d.GetValue(ColumnDefinitionsProperty);
    }

    /// <summary>
    /// Sets the value of the ColumnDefinitions property
    /// </summary>
    public static void SetColumnDefinitions(DependencyObject d, string value)
    {
      d.SetValue(ColumnDefinitionsProperty, value);
    }

    /// <summary>
    /// Handles property changed event for the ColumnDefinitions property, constructing
    /// the required ColumnDefinitions elements on the grid which this property is attached to.
    /// </summary>
    private static void OnColumnDefinitionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      Grid targetGrid = d as Grid;

      // construct the required column definitions
      targetGrid.ColumnDefinitions.Clear();
      string columnDefs = e.NewValue as string;
      var columnDefArray = columnDefs.Split(',');
      foreach (string columnDefinition in columnDefArray)
      {
        if (columnDefinition.Trim() == "")
        {
          targetGrid.ColumnDefinitions.Add(new ColumnDefinition());
        }
        else
        {
          targetGrid.ColumnDefinitions.Add(new ColumnDefinition()
          {
            Width = ParseLength(columnDefinition)
          });
        }
      }
    }

    #endregion

    /// <summary>
    /// Parses a string to create a GridLength
    /// </summary>
    private static GridLength ParseLength(string length)
    {
      length = length.Trim();

      if (length.ToLowerInvariant().Equals("auto"))
      {
        return new GridLength(0, GridUnitType.Auto);
      }
      else if (length.Contains("*"))
      {
        length = length.Replace("*", "");
        if (string.IsNullOrEmpty(length)) length = "1";
        return new GridLength(double.Parse(length), GridUnitType.Star);
      }

      return new GridLength(double.Parse(length), GridUnitType.Pixel);
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions