Click here to Skip to main content
15,896,502 members
Articles / Web Development / HTML

Zyan Drench, A Game for Android with Wifi Support

Rate me:
Please Sign up or sign in to vote.
4.99/5 (17 votes)
28 Feb 2016MIT12 min read 47.4K   1.3K   37  
This article describes building an Android game with networking support using C#, Xamarin.Android platform and Zyan Communication Framework.
using System;
using System.Collections.Generic;
using System.Linq;

namespace Drench
{
	/// <summary>
	/// Drench game server.
	/// </summary>
	public class DrenchGameServer : DrenchGameBase, IDrenchGameServer, IDisposable
	{
		public DrenchGameServer()
		{
			UpdateStatus();
		}

		public override void NewGame()
		{
			InnerGame.NewGame();
			IsStopped = false;
			OnGameStarted();
			UpdateStatus();
		}

		private TwoPlayerGame innerGame;

		private TwoPlayerGame InnerGame
		{
			get
			{
				if (innerGame == null)
				{
					innerGame = new TwoPlayerGame();
					innerGame.GameStopped += (sender, e) => OnGameStopped(true, innerGame.CurrentStatus);
				}

				return innerGame;
			}
		}

		public override DrenchBoard Board
		{
			get { return InnerGame.Board; }
		}

		public override void MakeMove(int value)
		{
			InnerGame.MakeMove(value);
			UpdateStatus();
			OnMoved(value);
		}

		private void UpdateStatus()
		{
			if (IsStopped)
			{
				forbiddenColors = Enumerable.Range(0, DrenchBoard.ColorCount);
			}
			else if (InnerGame.CurrentPlayer == 0)
			{
				CurrentStatus = "Your turn.";
				forbiddenColors = InnerGame.ForbiddenColors;
			}
			else
			{
				CurrentStatus = "Waiting for other player...";
				forbiddenColors = Enumerable.Range(0, DrenchBoard.ColorCount);
			}

			OnGameChanged();
		}

		protected override void CheckIfStopped()
		{
			InnerGame.InternalCheckIfStopped();
		}

		private bool isStopped;

		public override bool IsStopped
		{
			get { return isStopped || InnerGame.IsStopped; }
			protected set { isStopped = value; }
		}

		public override string CurrentStatus
		{
			get { return InnerGame.CurrentStatus; }
			protected set { InnerGame.SetCurrentStatus(value); }
		}

		private IEnumerable<int> forbiddenColors;

		public override IEnumerable<int> ForbiddenColors { get { return forbiddenColors; } }

		public event EventHandler GameStarted;

		private void OnGameStarted()
		{
			var gameStarted = GameStarted;
			if (gameStarted != null)
			{
				gameStarted(null, EventArgs.Empty);
			}
		}

		public void Join()
		{
			if (IsReady)
			{
				throw new InvalidOperationException("Second player already joined the game. Try another server.");
			}

			IsReady = true;
			OnGameStarted();
		}

		public void Leave()
		{
			IsReady = false;
			IsStopped = true;
			OnGameStopped(false, "Second player has left the game.");
		}

		public void Dispose()
		{
			IsReady = false;
			IsStopped = true;
			OnGameStopped(false, "Network game is terminated by server.");
			OnDisposed();
		}

		public event EventHandler Disposed;

		private void OnDisposed()
		{
			var disposed = Disposed;
			if (disposed != null)
			{
				disposed(null, EventArgs.Empty);
			}
		}

		public bool IsReady { get; private set; }

		public event EventHandler<MoveEventArgs> Moved;

		private void OnMoved(int color)
		{
			var moved = Moved;
			if (moved != null)
			{
				Moved(null, new MoveEventArgs { Color = color });
			}
		}
	}
}

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


Written By
Software Developer (Senior) ULTIMA Businessware
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions