Click here to Skip to main content
15,897,371 members
Articles / Web Development / ASP.NET

Silverlight Alien Sokoban

Rate me:
Please Sign up or sign in to vote.
4.88/5 (59 votes)
11 Nov 200717 min read 134K   1K   138  
A fun Silverlight implementation of the game Sokoban. Contrasting Silverlight 1.1 and WPF, while showcasing some new features of C# 3.0, Expression Design, Expression Blend, and Visual Studio 2008.
/*	<File>
		<Author Name="Daniel Vaughan" Email="dbvaughan NOSPAM at NOSPAM gmail NOSPAM dot NOSPAM com"/>
		<CreationDate>2007/11/5 12:10</CreationDate>
		<LastSubmissionDate>$Date: $</LastSubmissionDate>
		<Version>$Revision: $</Version>
		<License>
Copyright © 2007, Daniel Vaughan
All rights reserved.
http://www.orpius.com

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Neither Daniel Vaughan, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written 
permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
		</License>
	</File>
*/

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Orpius.Sokoban.Commands;
using Orpius.Sokoban.Controls;

namespace Orpius.Sokoban
{
	/// <summary>
	/// The main page of Alien Sokoban.
	/// </summary>
	public partial class Page : Canvas
	{
		readonly CommandManager commandManager = new CommandManager();
		Canvas canvas_GameArea;
		readonly Game game = new Game(new WebSokobanService());
		GameState gameState = GameState.Loading;
//		List<MediaElement> mediaElements = new List<MediaElement>();

		Game Game
		{
			get
			{
				return game;
			}
		}

		/// <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 textBlock_PressAnyKey.Visibility == Visibility.Visible;
			}
			set
			{
				textBlock_PressAnyKey.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		public void Page_Loaded(object o, EventArgs e)
		{
			// Required to initialize variables
			InitializeComponent();

			/* Set the feedback message to empty. */
			feedbackControl.Message = new FeedbackMessage();
			/* Hide the press any key textblock. */
			ContinuePromptVisible = false;

			/* Prompt the SynchronizationContext to take note 
			 * of the current thread and to start monitoring for callbacks
			 * to the main thread. */
			SynchronizationContext.SetContext(null);

			canvas_GameArea = (Canvas)FindName("canvas_GameArea");
			/* Begin listening for game events. */
			Game.PropertyChanged += new PropertyChangedEventHandler(game_PropertyChanged);
			
			try
			{
				/* Load and start the first level of the game. */
				Game.Start();
			}
			catch (Exception ex)
			{
				/* Handle this. */
				Console.WriteLine(ex.StackTrace);
				throw;
			}
			/* Ready to listen for keyboard events. */
			KeyDown += new KeyboardEventHandler(Page_KeyDown);
			feedbackControl.Click += new EventHandler<FeedbackEventArgs>(feedbackControl_Click);
		}

		void feedbackControl_Click(object sender, FeedbackEventArgs e)
		{
			if (Game.GameState == GameState.LevelCompleted)
			{
				Game.GotoNextLevel();
			}
		}

		void PlayAudioClip(MediaElement mediaElement)
		{
			mediaElement.Position = new TimeSpan(0);
			mediaElement.Play();
		}

		void Page_KeyDown(object sender, KeyboardEventArgs e)
		{
			if (textBox_LevelCode.HasFocus)
			{
				return;
			}
			CommandBase command = null;
			Level level = Game.Level;
			Key key = (Key)e.Key;
			if (Game != null)
			{
				if (Game.GameState == GameState.Running)
				{
					switch (key)
					{
						case Key.I:
							command = new MoveCommand(level, Direction.Up);
							break;
						case Key.K:
							command = new MoveCommand(level, Direction.Down);
							break;
						case Key.J:
							command = new MoveCommand(level, Direction.Left);
							break;
						case Key.L:
							command = new MoveCommand(level, Direction.Right);
							break;
						case Key.Z:
							if (e.Ctrl)
							{
								commandManager.Undo();
							}
							break;
						case Key.Y:
							if (e.Ctrl)
							{
								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);
			}
		}

		void game_PropertyChanged(object sender, 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:
					feedbackControl.Message = new FeedbackMessage() { Message = "Loading..." };
					ContinuePromptVisible = false;
					break;
				case GameState.GameOver:
					feedbackControl.Message = new FeedbackMessage() { Message = "Game Over" };
					ContinuePromptVisible = true;
					break;
				case GameState.Running:
					ContinuePromptVisible = false;
					feedbackControl.Message = new FeedbackMessage();
					if (gameState == GameState.Loading)
					{
						InitialiseLevel();
					}
					break;
				case GameState.LevelCompleted:
					feedbackControl.Message = new FeedbackMessage() { Message = "Level Completed!" };
					PlayAudioClip(mediaElement_LevelComplete);
					ContinuePromptVisible = true;
					break;
				case GameState.GameCompleted:
					feedbackControl.Message = new FeedbackMessage() { Message = "Well done. \nGame completed! \nPlease email \ndbvaughan \nAT g mail dot com" };
					PlayAudioClip(mediaElement_GameComplete);
					break;
			}
			gameState = game.GameState;
		}

		/// <summary>
		/// Initialises the game grid using the level.
		/// </summary>
		void InitialiseLevel()
		{
			commandManager.Clear();
			canvas_GameArea.Children.Clear();
			/* Calculate cell size and offset. */
			double cellWidthMax = 780.0 / game.Level.ColumnCount;
			double cellHeightMax = 630.0 / game.Level.RowCount;
			double cellSize = cellWidthMax < cellHeightMax ? cellWidthMax : cellHeightMax;
			double height = game.Level.RowCount * cellSize;
			double width = game.Level.ColumnCount * cellSize;
			double leftStart = (780.0 - width) / 2;
			double left = leftStart;
			double top = (630.0 - height) / 2;

			/* Add CellControls to represent each Game Cell. */
			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 += new PropertyChangedEventHandler(cell_PropertyChanged);
					CellControl cellControl = new CellControl(cell);
					cellControl.Left = left;
					cellControl.Top = top;
					cellControl.SetSize(new Size(cellSize, cellSize));
					left += cellSize;

					canvas_GameArea.Children.Add(cellControl);
					cellControl.Click += new MouseEventHandler(cellControl_Click);
				}
				left = leftStart;
				top += cellSize;
			}

			/* Initialize the play control. */
			playControl.LevelCode = Game.LevelCode;
			playControl.LevelNumber = Game.Level.LevelNumber + 1;
			playControl.LevelCount = Game.LevelCount;
			playControl.MoveCount = 0;
			/* Play the intro audio clip. */
			PlayAudioClip(mediaElement_Intro);
			/* Listen for actor property changes. */
			game.Level.Actor.PropertyChanged += new PropertyChangedEventHandler(Actor_PropertyChanged);
			
			textBox_LevelCode.Text = Game.LevelCode;
		}

		void Actor_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			Actor actor = (Actor)sender;
			switch (e.PropertyName)
			{
				case "MoveCount":
					playControl.MoveCount = actor.MoveCount;
					break;
			}
		}

		void cellControl_Click(object sender, MouseEventArgs e)
		{
			if (textBox_LevelCode.HasFocus)
			{
				return;
			}
			/* When the user clicks a cell, we want to 
			 have the actor move there. */
			CellControl button = (CellControl)sender;
			Cell cell = button.Cell;
			CommandBase command;
			if (e.Shift)
			{
				command = new PushCommand(Game.Level, cell.Location);
			}
			else
			{
				command = new JumpCommand(Game.Level, cell.Location);
			}
			commandManager.Execute(command);
		}

		void cell_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			Cell cell = (Cell)sender;
			if (e.PropertyName == "CellContents")
			{	/* Play an audio clip depending on the state of the cell. */
				if (cell.CellContents is Actor)
				{
					PlayAudioClip(mediaElement_Footstep);
				}
				else if (cell.CellContents is Treasure)
				{
					if (cell is GoalCell)
					{
						PlayAudioClip(mediaElement_DingDong);
					}
					else
					{
						PlayAudioClip(mediaElement_TreasurePush);
					}
				}
			}
		}

		public void AudioClip_Opened(object sender, EventArgs e)
		{
//			MediaElement element = (MediaElement)sender;
//			if (!mediaElements.Contains(element))
//			{
//				mediaElements.Add(element);
//			}
		}

		protected void PlayControl_RestartLevel(object sender, EventArgs e)
		{
			Game.RestartLevel();
		}

		protected void TextBox_LevelCode_EnterKeyPressed(object sender, EventArgs e)
		{
			if (!Game.TryGotoToLevel(textBox_LevelCode.Text))
			{
				textBox_LevelCode.Text = Game.LevelCode;
			}
		}

		protected void TextBox_LevelCode_FocusGained(object sender, EventArgs e)
		{
			textBox_LevelCode.Opacity = 100;
			/* Textblock text should be selected. Our Textbox doesn't have 
			 that capability yet. */
		}

		protected void TextBox_LevelCode_FocusLost(object sender, EventArgs e)
		{
			textBox_LevelCode.Opacity = 0;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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