Click here to Skip to main content
15,886,873 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.9K   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.
<common:LayoutAwarePage
    x:Class="ReversiEight.ReversiView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ReversiEight"
    xmlns:common="using:ReversiEight.Common"
    xmlns:conv="using:ReversiEight.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

  <common:LayoutAwarePage.Resources>
    <conv:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
    
    <ControlTemplate x:Key="PlainButtonTemplate">
      <ContentPresenter/>
    </ControlTemplate>
    
    <ControlTemplate x:Key="BlackScoreTemplate">
      <Viewbox>
        <Grid Width="150" Height="150">
          <Image Source="Assets/BlackPiece.png" Stretch="Uniform"/>
          <TextBlock Text="{Binding BlackScore}" VerticalAlignment="Center" HorizontalAlignment="Center"
                     FontSize="50"/>
        </Grid>
      </Viewbox>
    </ControlTemplate>
    
    <ControlTemplate x:Key="RestartGameTemplate">
      <Button Visibility="{Binding Path=GameOver, Converter={StaticResource BoolToVisibilityConverter}}"
              Command="{Binding Path=RestartGame}"
              Template="{StaticResource PlainButtonTemplate}">
        <Image Source="Assets/GameOver.png"
               Stretch="Uniform" Margin="40"
               VerticalAlignment="Top"/>
      </Button>
    </ControlTemplate>
    
    <ControlTemplate x:Key="ReversiTitleTemplate">
      <Image Source="Assets/ReversiText.png"
             Stretch="Uniform"
             Margin="0,80,0,0"/>
    </ControlTemplate>
    
    <ControlTemplate x:Key="WhiteScoreTemplate">
      <Viewbox>
        <Grid Width="150" Height="150">
          <Image Source="Assets/WhitePiece.png" Stretch="Uniform"/>
          <TextBlock Text="{Binding WhiteScore}" VerticalAlignment="Center" HorizontalAlignment="Center"
                     FontSize="50" Foreground="Black"/>
        </Grid>
      </Viewbox>
    </ControlTemplate>

    <ControlTemplate x:Key="ReversiBoardTemplate">
      <Viewbox>
        <Grid Margin="20">
          <Image Source="Assets/Board.png" Stretch="UniformToFill"/>
          <ItemsControl ItemsSource="{Binding Squares}">
            <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                <Grid Width="1200" Height="1200" x:Name="boardContainer"
                        common:GridUtils.RowDefinitions="*,*,*,*,*,*,*,*"
                        common:GridUtils.ColumnDefinitions="*,*,*,*,*,*,*,*">
                </Grid>
              </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
              <DataTemplate>
                <local:BoardSquareView Grid.Row="{Binding Row}" Grid.Column="{Binding Column}"
                                         Width="150" Height="150"/>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </Grid>
      </Viewbox>
    </ControlTemplate>
  </common:LayoutAwarePage.Resources>
  
  <Grid>

    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="ApplicationViewStates">
        <VisualState x:Name="FullScreenLandscape">
          <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FilledLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PortraitLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LandscapeLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
            </ObjectAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="Filled">
          <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FilledLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PortraitLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LandscapeLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="FullScreenPortrait">
          <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FilledLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PortraitLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LandscapeLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="Snapped">
          <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FilledLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PortraitLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
            </ObjectAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LandscapeLayout" Storyboard.TargetProperty="Visibility">
              <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
            </ObjectAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    
    <Image Source="Assets/Background.jpg" Stretch="UniformToFill"/>

    <!-- Portrait layout -->
    <Grid x:Name="PortraitLayout"
          common:GridUtils.ColumnDefinitions="*,*"
          common:GridUtils.RowDefinitions="*,3*,0.5*,*">
      <ContentControl Grid.ColumnSpan="2"
                      Template="{StaticResource ReversiTitleTemplate}"/>
      <ContentControl Grid.ColumnSpan="2" Grid.Row="1"
                      Template="{StaticResource ReversiBoardTemplate}"/>
      <ContentControl VerticalAlignment="Bottom" Grid.Row="2"
                      Template="{StaticResource BlackScoreTemplate}"/>
      <ContentControl VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="2"
                      Template="{StaticResource WhiteScoreTemplate}"/>
      <ContentControl Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Center"
                      Template="{StaticResource RestartGameTemplate}"/>
    </Grid>

    <!-- Filled layout -->
    <Grid x:Name="FilledLayout"
          common:GridUtils.ColumnDefinitions="3*,*,*"
          common:GridUtils.RowDefinitions="*,0.4*,*">
      <ContentControl VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1"
                      Template="{StaticResource BlackScoreTemplate}"/>
      <ContentControl Grid.Column="1" Grid.ColumnSpan="2"
                      Template="{StaticResource ReversiTitleTemplate}"/>
      <ContentControl Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2"
                      Template="{StaticResource RestartGameTemplate}"/>
      <ContentControl Grid.Column="0" Grid.RowSpan="3"
                      Template="{StaticResource ReversiBoardTemplate}"/>
      <ContentControl Grid.Column="2" Grid.Row="1"
                      Template="{StaticResource WhiteScoreTemplate}"/>
    </Grid>

    <!-- Landscape layout -->
    <Grid x:Name="LandscapeLayout"
          common:GridUtils.ColumnDefinitions="Auto,*,*"
          common:GridUtils.RowDefinitions="*,0.5*,*">
      <ContentControl VerticalAlignment="Bottom" Grid.Column="1" Grid.Row="1"
                      Template="{StaticResource BlackScoreTemplate}"/>
      <ContentControl Grid.Column="1" Grid.ColumnSpan="2"
                      Template="{StaticResource ReversiTitleTemplate}"/>
      <ContentControl Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2"
                      Template="{StaticResource RestartGameTemplate}"/>
      <ContentControl Grid.Column="0" Grid.RowSpan="3"
                      Template="{StaticResource ReversiBoardTemplate}"/>
      <ContentControl VerticalAlignment="Bottom" Grid.Column="2" Grid.Row="1"
                      Template="{StaticResource WhiteScoreTemplate}"/>
    </Grid>
  </Grid>
</common:LayoutAwarePage>

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