Click here to Skip to main content
15,885,931 members
Articles / Desktop Programming / WPF

WPF Alien Sokoban

Rate me:
Please Sign up or sign in to vote.
4.88/5 (44 votes)
16 Jun 2008BSD9 min read 125.4K   3.4K   78  
A fun implementation of the game Sokoban, written to showcase some features of WPF, C# 3.0, Expression Design, and Visual Studio 2008.
<Window x:Class="Orpius.Sokoban.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Orpius.Sokoban.Controls;assembly="
    xmlns:WpfSokoban="clr-namespace:Orpius.Sokoban;assembly="
	xmlns:Sokoban="clr-namespace:Orpius.Sokoban;assembly=Orpius.Sokoban"
    Title="Alien Sokoban" Height="600" Width="800" MinWidth="650" MinHeight="400" KeyDown="Window_KeyDown" FontFamily="Tahoma" Icon="Images/AppIcon.png">

	<Window.Resources>
		<!-- Game instance used throughout. -->
		<Sokoban:Game x:Key="sokobanGame"/>
				
		<!-- Used by the Restart Level button. -->
		<ControlTemplate x:Key="ToolBarButton" TargetType="{x:Type Button}">
			<Grid>
				<Border x:Name="border" Background="#FFFFFF" BorderBrush="#FFFFFF" BorderThickness="2" CornerRadius="5,5,5,5" Width="Auto" Height="Auto" RenderTransformOrigin="0.5,0.5">
					<Border.RenderTransform>
						<TransformGroup>
							<ScaleTransform ScaleX="1" ScaleY="1"/>
							<SkewTransform AngleX="0" AngleY="0"/>
							<RotateTransform Angle="0"/>
							<TranslateTransform X="0" Y="0"/>
						</TransformGroup>
					</Border.RenderTransform>
				</Border>
				<ContentControl Content="{TemplateBinding Content}" Width="Auto" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" FontFamily="Tahoma" FontSize="15"/>
			</Grid>
		</ControlTemplate>

		<!-- Displays the Treasre or Actor icon. -->
		<Style x:Key="CellContentStyle" TargetType="{x:Type Rectangle}">
			<Style.Triggers>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell contains the Actor. -->
						<Condition Binding="{Binding Path=CellContents.Name}" Value="Actor" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Fill" Value="{StaticResource PlayerCellContentBrush}"/>					
				</MultiDataTrigger>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell contains a treasure. -->
						<Condition Binding="{Binding Path=CellContents.Name}" Value="Treasure" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Width" Value="20"/>
					<Setter Property="Height" Value="20"/>
					<Setter Property="Fill" Value="{StaticResource TreasureCellContentBrush}"/>
				</MultiDataTrigger>
			</Style.Triggers>
		</Style>
		
		<!-- Displays the appropriate cell type. -->
		<Style x:Key="CellStyle" TargetType="{x:Type Rectangle}">
			<Style.Triggers>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell is a wall. -->
						<Condition Binding="{Binding Path=Name}" Value="Wall" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Fill" Value="{StaticResource WallCellBrush}"/>
				</MultiDataTrigger>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell is a floor cell. -->
						<Condition Binding="{Binding Path=Name}" Value="Floor" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Fill" Value="{StaticResource FloorCellBrush}"/>
				</MultiDataTrigger>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell is an outer empty space cell. -->
						<Condition Binding="{Binding Path=Name}" Value="Space" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Fill" Value="Transparent"/>
				</MultiDataTrigger>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell is a goal. -->
						<Condition Binding="{Binding Path=Name}" Value="Goal" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Fill" Value="{StaticResource GoalCellContentBrush}"/>
				</MultiDataTrigger>
				<MultiDataTrigger>
					<MultiDataTrigger.Conditions>
						<!-- If the cell is a goal with a treasure in it. -->
						<Condition Binding="{Binding Path=Name}" Value="Goal" />
						<Condition Binding="{Binding Path=CellContents.Name}" Value="Treasure" />
					</MultiDataTrigger.Conditions>
					<Setter Property="Fill" Value="{StaticResource GoalActiveCellContentBrush}"/>
				</MultiDataTrigger>
			</Style.Triggers>
		</Style>

		<!-- All cells are styled here. -->
		<Style x:Key="Cell" TargetType="{x:Type Button}">
			<Setter Property="Template">
				<Setter.Value>
					<ControlTemplate TargetType="{x:Type Button}">
						<Grid>
							<!-- The cell, -->
							<Rectangle Width="40" Height="40" Style="{DynamicResource CellStyle}" />
							<!-- and its content. -->
							<Rectangle Style="{DynamicResource CellContentStyle}"/>
						</Grid>
					</ControlTemplate>
				</Setter.Value>
			</Setter>
		</Style>

		<!-- Audio clips. -->
		<Style x:Key="AudioClip" TargetType="{x:Type MediaElement}">
			<Setter Property="Width" Value="0"/>
			<Setter Property="Height" Value="0"/>
			<Setter Property="LoadedBehavior" Value="Manual"/>
			<Setter Property="UnloadedBehavior" Value="Stop"/>
		</Style>

		<!-- Center labels. -->
		<Style x:Key="CenterLabels" TargetType="{x:Type Label}">
			<Setter Property="Foreground" Value="White"/>
			<Setter Property="FontSize" Value="25"/>
			<Setter Property="VerticalAlignment" Value="Center"/>
		</Style>
	</Window.Resources>

    <Grid>     
        <DockPanel Margin="10,10,10,10">
            <Grid Name="grid_Main">
                <Grid.RowDefinitions>
                    <RowDefinition Height="100" />
                    <RowDefinition Height="60" />
					<RowDefinition Height="5" />
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
				<Border Grid.Row="0" BorderBrush="#919292" CornerRadius="12" BorderThickness="5" Background="#FF9A00">
					<Rectangle Fill="{StaticResource BannerBrush}" Height="80" Width="500">
					</Rectangle>
				</Border>
				<Border Grid.Row="1" BorderBrush="#919292" CornerRadius="12" BorderThickness="5">
					<Border.Background>
						<LinearGradientBrush EndPoint="0.5,-1.389" StartPoint="0.5,2.389" SpreadMethod="Pad">
							<GradientStop Color="#FF515153" Offset="1"/>
							<GradientStop Color="#FFE4E4E4" Offset="0"/>
							<GradientStop Color="#FF6E6E6E" Offset="0.74"/>
						</LinearGradientBrush>
					</Border.Background>
					<Grid DataContext="{StaticResource sokobanGame}">
						<StackPanel Height="40" Name="stackPanel1" Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Width="Auto"  Orientation="Horizontal">
							<Label VerticalAlignment="Center" FontSize="15" Foreground="White">Level Code:</Label>
							<TextBox Height="21" Name="textBox_LevelCode" Width="60" MaxLength="5" AcceptsReturn="True" VerticalAlignment="Center" HorizontalContentAlignment="Center" KeyUp="textBox_LevelCode_KeyUp" GotFocus="textBox_LevelCode_GotFocus" LostFocus="textBox_LevelCode_LostFocus" IsTabStop="False" FontSize="14" />
						</StackPanel>
						<StackPanel Margin="0,0,10,0" VerticalAlignment="Center" HorizontalAlignment="Center" Width="Auto"  Orientation="Horizontal">
							<Label Style="{StaticResource CenterLabels}">Level</Label>
							<Label Name="label_LevelNumber" Style="{StaticResource CenterLabels}">0</Label>
							<Label Style="{StaticResource CenterLabels}"> : </Label>
							<Label Name="label_Moves" Style="{StaticResource CenterLabels}" Content="{Binding Level.Actor.MoveCount}"/>
							<Label Style="{StaticResource CenterLabels}">Moves</Label>							
						</StackPanel>
						<StackPanel Margin="0,0,10,0" VerticalAlignment="Center" HorizontalAlignment="Right" Width="Auto"  Orientation="Horizontal">
							<Button Height="23" Name="button_RestartLevel" Width="100" Click="button_RestartLevel_Click" IsTabStop="False" Template="{StaticResource ToolBarButton}" Focusable="False">Restart Level</Button>
						</StackPanel>
					</Grid>
				</Border>
                <Border Grid.Row="3" Padding="20" BorderBrush="#919292" CornerRadius="12" BorderThickness="0" Background="Transparent">
                    <Viewbox Stretch="Uniform">                 
                        <Grid Name="grid_Game">                    
                        </Grid>
                    </Viewbox>
                </Border>
				<Controls:FeedbackControl Grid.Row="3" x:Name="FeedbackControl1" Margin="10,10,10,10"/>
                <Border Name="Border_PressAnyKey" Visibility="Hidden" Grid.Row="3" BorderBrush="#919292" CornerRadius="8" BorderThickness="0" Margin="10,10,10,10" VerticalAlignment="Top" Height="50">
					<Border.Background>
						<SolidColorBrush Color="#A0EBE5" Opacity=".1"/>
					</Border.Background>
					<Label Content="Press any key to continue." FontSize="30" />
                </Border>                
            </Grid>
        </DockPanel>
		<!-- Audio clips. -->
        <MediaElement Name="mediaElement_Intro" Source="../../Audio/Intro.wav" Style="{StaticResource AudioClip}"/>
        <MediaElement Name="mediaElement_TreasurePush" Source="../../Audio/TreasurePush.wav" Style="{StaticResource AudioClip}"/>
        <MediaElement Name="mediaElement_DingDong" Source="../../Audio/DingDong.wav" Style="{StaticResource AudioClip}"/>
        <MediaElement Name="mediaElement_Footstep" Source="../../Audio/Footstep.wav" Style="{StaticResource AudioClip}"/>
        <MediaElement Name="MediaElement_LevelComplete" Source="../../Audio/Movement_from_NEO_Sounds.mp3" Style="{StaticResource AudioClip}"/>
		<MediaElement Name="MediaElement_GameComplete" Source="../../Audio/DullClapping.mp3" Style="{StaticResource AudioClip}"/>
    </Grid>

</Window>

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 BSD License


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions