Click here to Skip to main content
15,891,739 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.5K   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: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"
	Loaded="Window_Loaded" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

	<Window.Resources>
		<!-- Game instance used throughout. -->
		<Sokoban:Game x:Key="sokobanGame"/>
				
		<!-- 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="20"/>
			<Setter Property="VerticalAlignment" Value="Center"/>
		</Style>
	</Window.Resources>

    <Grid>

		<!-- Background. -->
		<Viewbox Margin="0,20,0,0" Stretch="UniformToFill">
			<Controls:BackgroundControl Opacity=".1" />
		</Viewbox>

		<!-- Content. -->
		<DockPanel Margin="8,8,12,12">
            <Grid x:Name="grid_Main">
                <Grid.RowDefinitions>
                    <RowDefinition Height="100" />
                	<RowDefinition Height="50" />
					<RowDefinition Height="5" />
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
					
            	<Border Grid.Row="0" BorderBrush="#919292" CornerRadius="20" BorderThickness="5">
            		<Border.Background>
            			<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            				<GradientStop Color="#FFC6C6C6" Offset="1"/>
            				<GradientStop Color="#FF919191" Offset="0.513"/>
            				<GradientStop Color="#FFCACACA" Offset="0"/>
            			</LinearGradientBrush>
            		</Border.Background>
            	</Border>
            	<Rectangle Stroke="{x:Null}" Margin="10,10,10,0" RadiusX="18" RadiusY="18" Opacity="0.385" Height="40" VerticalAlignment="Top">
            		<Rectangle.Fill>
            			<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            				<GradientStop Color="#FFECECEC" Offset="0"/>
            				<GradientStop Color="#62FFFFFF" Offset="1"/>
            			</LinearGradientBrush>
            		</Rectangle.Fill>
            	</Rectangle>
            	<!--<Rectangle Fill="{StaticResource BannerBrush}" Height="80" Width="500">
					</Rectangle>-->
            	<Viewbox Margin="0,5,0,5">
            		<Controls:Banner />
            	</Viewbox>
            	<Border Grid.Row="1" BorderBrush="#FFFFE63E" CornerRadius="10,10,10,10" BorderThickness="2,2,2,2" Margin="0,5,0,0">
            		<Border.Background>
            			<LinearGradientBrush EndPoint="0.5,-1.389" StartPoint="0.5,2.389" SpreadMethod="Pad">
            				<GradientStop Color="#FFFF9900" Offset="1"/>
            				<GradientStop Color="#FFFF9900" Offset="0.58"/>
            				<GradientStop Color="#FFFFFFFF" Offset="0"/>
            			</LinearGradientBrush>
            		</Border.Background>
            		<Grid DataContext="{StaticResource sokobanGame}">
            			<Rectangle Stroke="{x:Null}" Margin="5,3,5,18" RadiusX="10" RadiusY="10" Opacity="0.41">
            				<Rectangle.Fill>
            					<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            						<GradientStop Color="#FFECECEC" Offset="0"/>
            						<GradientStop Color="#FFFFFFFF" Offset="1"/>
            					</LinearGradientBrush>
            				</Rectangle.Fill>
            			</Rectangle>
            			<StackPanel Height="40" x:Name="stackPanel1" Margin="15,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Width="Auto"  Orientation="Horizontal">
            				<Label VerticalAlignment="Center" FontSize="15" Foreground="White" Content="Level Code:"/>
            				<TextBox Height="21" x: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}" Content="Level"/>
            				<Label x:Name="label_LevelNumber" Style="{StaticResource CenterLabels}" Content="0"/>
            				<Label Style="{StaticResource CenterLabels}" Content=":"/>
            				<Label x:Name="label_Moves" Style="{StaticResource CenterLabels}" Content="{Binding Path=Level.Actor.MoveCount}"/>
            				<Label Style="{StaticResource CenterLabels}" Content="Moves"/>							
            			</StackPanel>
            			<StackPanel Margin="0,0,20,0" VerticalAlignment="Center" HorizontalAlignment="Right" Width="Auto"  Orientation="Horizontal">
            				<Button Height="23" x:Name="button_RestartLevel" Width="100" Click="button_RestartLevel_Click" IsTabStop="False"  Focusable="False" Content="Restart Level" Foreground="White" BorderBrush="White">
								<Button.Background>
									<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
										<GradientStop Color="#FF9AFF95" Offset="0.21"/>
										<GradientStop Color="#FF5DD757" Offset="0.589"/>
										<GradientStop Color="#FF99FF93" Offset="1"/>
									</LinearGradientBrush>
								</Button.Background>
							</Button>
            			</StackPanel>
            		</Grid>
            	</Border>
                <Border Grid.Row="3" Padding="20" BorderBrush="#919292" CornerRadius="12" BorderThickness="0" Background="Transparent">
                    <Viewbox Stretch="Uniform">                 
                        <Grid x:Name="grid_Game"/>
                    </Viewbox>
                </Border>
				<Controls:FeedbackControl Grid.Row="3" x:Name="FeedbackControl1" Margin="10,10,10,10" Click="FeedbackControl1_Click"/>
                <Border x: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}"/>-->
		<MediaElement x:Name="mediaElement_Intro" Source="../../Audio/Intro.wav" Width="0" Height="0" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
		<MediaElement x:Name="mediaElement_TreasurePush" Source="../../Audio/TreasurePush.wav" Width="0" Height="0" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
		<MediaElement x:Name="mediaElement_DingDong" Source="../../Audio/DingDong.wav" Width="0" Height="0" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
		<MediaElement x:Name="mediaElement_Footstep" Source="../../Audio/Footstep.wav" Width="0" Height="0" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
		<MediaElement x:Name="MediaElement_LevelComplete" Source="../../Audio/Movement_from_NEO_Sounds.mp3" Width="0" Height="0" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
		<MediaElement x:Name="MediaElement_GameComplete" Source="../../Audio/DullClapping.mp3" Width="0" Height="0" LoadedBehavior="Manual" UnloadedBehavior="Stop" />
	</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