Click here to Skip to main content
15,885,546 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.1K   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;
using System.Threading.Tasks;

namespace Drench
{
	/// <summary>
	/// Player versus Computer game with an expert AI.
	/// </summary>
	public class ComputerGameExpert : ComputerGameBase
	{
		private class TestBoard : DrenchBoard
		{
			public int TestColor(int x, int y, int value)
			{
				// calculate how much pixels will be painted
				SetColor(BoardSize - 1, BoardSize - 1, value);
				return SetColor(BoardSize - 1, BoardSize - 1, (value + 1) % DrenchBoard.ColorCount);
			}
		}

		private ICollection<Tuple<int, int>> CalculateColors(int x, int y)
		{
			var colors = new HashSet<int>(Enumerable.Range(0, DrenchBoard.ColorCount));
			colors.Remove(Board[0, 0]);
			colors.Remove(Board[-1, -1]);

			var testBoard = new TestBoard();
			var counts = new List<Tuple<int, int>>();
			foreach (var color in colors.ToArray())
			{
				testBoard.CopyFrom(this.Board);
				var count = testBoard.TestColor(x, y, color);
				counts.Add(Tuple.Create(color, count));
			}

			return counts;
		}

		protected override int CalculateOptimalColor()
		{
			// my and their options for the current move
			var myColors = CalculateColors(BoardSize - 1, BoardSize - 1);
			var theirColors = CalculateColors(0, 0);

			// select the best color of two sets, with a slight priority of myColors
			const int MyPriority = 1;
			var allColors = myColors.Select(c => new { Color = c.Item1, Count = c.Item2 + MyPriority }).Concat(
				theirColors.Select(c => new { Color = c.Item1, Count = c.Item2 }));

			var query =
				from c in allColors 
				orderby c.Count descending
				select c.Color;

			return query.First();
		}
	}
}

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