Click here to Skip to main content
15,886,693 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.
#region File and License Information
/*
<File>
	<Copyright>Copyright © 2007, Daniel Vaughan. All rights reserved.</Copyright>
	<License see="prj:///Documentation/License.txt"/>
	<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
	<CreationDate>2008-06-09 11:51:22Z</CreationDate>
	<LastSubmissionDate>$Date: $</LastSubmissionDate>
	<Version>$Revision: $</Version>
</File>
*/
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Orpius.Sokoban.Commands;
using Orpius.Sokoban.Controls;

namespace Orpius.Sokoban
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow
	{
		readonly CommandManager commandManager = new CommandManager();

		/// <summary>
		/// Gets the game that is defined as a window resource 
		/// in the XAML as <Sokoban:Game x:Key="sokobanGame"/>.
		/// </summary>
		/// <value>The game intance.</value>
		Game Game
		{
			get
			{
				return (Game)TryFindResource("sokobanGame");
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether the press any key
		/// message is displayed.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if the press any key message is displayed; 
		/// otherwise, <c>false</c>.
		/// </value>
		bool ContinuePromptVisible
		{
			get
			{
				return Border_PressAnyKey.Visibility == Visibility.Visible;
			}
			set
			{
				Border_PressAnyKey.Visibility = value ? Visibility.Visible : Visibility.Hidden;
			}
		}

		public MainWindow()
		{
			InitializeComponent();
		}

		void Window_Loaded(object sender, RoutedEventArgs e)
		{
			Game.PropertyChanged += game_PropertyChanged;

			try
			{
				/* Load and start the first level of the game. */
				Game.Start();
			}
			catch (Exception ex)
			{
				MessageBox.Show("Problem loading game. " + ex.Message);
			}
		}

		void game_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			switch (e.PropertyName)
			{
				case "GameState":
					UpdateGameDisplay();
					break;
			}
		}

		/// <summary>
		/// We set feedback messages and so one here,
		/// using the game's <see cref="GameState"/>.
		/// </summary>
		void UpdateGameDisplay()
		{
			switch (Game.GameState)
			{
				case GameState.Loading:
					FeedbackControl1.Message = new FeedbackMessage { Message = "Loading..." };
					ContinuePromptVisible = false;
					break;
				case GameState.GameOver:
					FeedbackControl1.Message = new FeedbackMessage { Message = "Game Over" };
					ContinuePromptVisible = true;
					break;
				case GameState.Running:
					ContinuePromptVisible = false;
					FeedbackControl1.Message = new FeedbackMessage();
					// Uncomment when/if pause is implemented.
					//if (gameState == GameState.Loading)
					//{
						InitialiseLevel();
					//}
					break;
				case GameState.LevelCompleted:
					FeedbackControl1.Message = new FeedbackMessage { Message = "Level Completed!" };
					MediaElement_LevelComplete.Position = TimeSpan.MinValue;
					MediaElement_LevelComplete.Play();
					ContinuePromptVisible = true;
					break;
				case GameState.GameCompleted:
					FeedbackControl1.Message = new FeedbackMessage { Message = "Well done. \nGame completed! \nEmail dbvaughan \nAT g mail dot com" };
					MediaElement_GameComplete.Position = TimeSpan.MinValue;
					MediaElement_GameComplete.Play();
					break;
			}
		}

		/// <summary>
		/// Handles the KeyDown event of the Window control.
		/// Put this into another class later.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> 
		/// instance containing the event data.</param>
		void Window_KeyDown(object sender, KeyEventArgs e)
		{
			CommandBase command = null;
			Level level = Game.Level;

			if (Game != null)
			{
				if (Game.GameState == GameState.Running)
				{
					switch (e.Key)
					{
						case Key.Up:
							command = new MoveCommand(level, Direction.Up);
							break;
						case Key.Down:
							command = new MoveCommand(level, Direction.Down);
							break;
						case Key.Left:
							command = new MoveCommand(level, Direction.Left);
							break;
						case Key.Right:
							command = new MoveCommand(level, Direction.Right);
							break;
						case Key.Z:
							if (Keyboard.Modifiers == ModifierKeys.Control)
							{
								commandManager.Undo();
							}
							break;
						case Key.Y:
							if (Keyboard.Modifiers == ModifierKeys.Control)
							{
								commandManager.Redo();
							}
							break;
					}
				}
				else
				{
					switch (Game.GameState)
					{
						case GameState.GameOver:
							Game.Start();
							break;
						case GameState.LevelCompleted:
							Game.GotoNextLevel();
							break;
					}
				}
			}
			if (command != null)
			{
				commandManager.Execute(command);
			}
		}

		/// <summary>
		/// Initialises the game grid using the level.
		/// </summary>
		void InitialiseLevel()
		{
			commandManager.Clear();

			grid_Game.Children.Clear();
			grid_Game.RowDefinitions.Clear();
			grid_Game.ColumnDefinitions.Clear();

			for (int i = 0; i < Game.Level.RowCount; i++)
			{
				grid_Game.RowDefinitions.Add(new RowDefinition());
			}

			for (int i = 0; i < Game.Level.ColumnCount; i++)
			{
				grid_Game.ColumnDefinitions.Add(new ColumnDefinition());
			}

			for (int row = 0; row < Game.Level.RowCount; row++)
			{
				for (int column = 0; column < Game.Level.ColumnCount; column++)
				{
					Cell cell = Game.Level[row, column];
					cell.PropertyChanged += cell_PropertyChanged;
					
					Button button = new Button();
					button.Focusable = false;
					/* The cell becomes the datacontext of the button,
					 * and it gets used a lot in the XAML. */
					button.DataContext = cell; 
					button.Padding = new Thickness(0, 0, 0, 0);
					button.Style = (Style)Resources["Cell"];
					button.Click += Cell_Click;
					
					Grid.SetColumn(button, column);
					Grid.SetRow(button, row);
					grid_Game.Children.Add(button);
				}
			}

			textBox_LevelCode.Text = Game.LevelCode;
			label_LevelNumber.Content = Game.Level.LevelNumber + 1 + "/" + Game.LevelCount;
			grid_Main.DataContext = Game.Level;
			/* Rewing the intro clip and play it. Is there a better way? */
			mediaElement_Intro.Position = TimeSpan.MinValue;
			mediaElement_Intro.Play();
			grid_Game.Focus();
		}

		void Cell_Click(object sender, RoutedEventArgs e)
		{
			/* When the user clicks a cell, we want to 
			 have the actor move there. */
			Button button = (Button)e.Source;
			Cell cell = (Cell)button.DataContext;
			CommandBase command;

			if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
			{
				command = new PushCommand(Game.Level, cell.Location);
			}
			else
			{
				command = new JumpCommand(Game.Level, cell.Location);
			}
			commandManager.Execute(command);
		}

		void cell_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			Cell cell = (Cell)sender;
			if (e.PropertyName == "CellContents")
			{
				if (cell.CellContents is Actor)
				{
					mediaElement_Footstep.Position = TimeSpan.MinValue;
					mediaElement_Footstep.Play();
				}
				else if (cell.CellContents is Treasure)
				{
					if (cell is GoalCell)
					{
						mediaElement_DingDong.Position = TimeSpan.MinValue;
						mediaElement_DingDong.Play();
					}
					else
					{
						mediaElement_TreasurePush.Position = TimeSpan.MinValue;
						mediaElement_TreasurePush.Play();
					}
				}
			}
		}

		void textBox_LevelCode_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Return)
			{
				if (Game.TryGotoToLevel(textBox_LevelCode.Text.Trim()))
				{
					/* Do some extra level change processing here. */
				}
				UIElement focusedElement = Keyboard.FocusedElement as UIElement;
				if (focusedElement != null)
				{
					focusedElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
				}
				e.Handled = true;
			}
		}

		void button_RestartLevel_Click(object sender, RoutedEventArgs e)
		{
			Game.RestartLevel();
		}

		void textBox_LevelCode_GotFocus(object sender, RoutedEventArgs e)
		{
			textBox_LevelCode.Text = string.Empty;
		}

		void textBox_LevelCode_LostFocus(object sender, RoutedEventArgs e)
		{
			textBox_LevelCode.Text = Game.LevelCode;
		}

		void FeedbackControl1_Click(object sender, FeedbackEventArgs e)
		{
			switch (Game.GameState)
			{
				case GameState.LevelCompleted:
					Game.GotoNextLevel();
					break;
			}
		}

	}
}

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