Listing 1

<!-- To show this window, do this: Window w=new Window1; w.Show(); -->
<Window
    xmlns="http://schemas.microsoft.com/2003/xaml"
    xmlns:def="Definition"
    def:Class="Hamurabi.WinFX.Window1"
    def:CodeBehind="Window1.xaml.cs"
    Text="Hamurabi.WinFX" Visible="True"
    Loaded="WindowLoaded"
    >

    <DockPanel DockPanel.Dock="Fill">

        <GridPanel Background="#98fb98" DockPanel.Dock="Bottom" Columns="1">

            <GridPanel Columns="2">
                <SimpleText ID="lblPopulation">Population</SimpleText>
                <TextBox Background="#87ceeb" IsEnabled="False"
				ID="txtPopulation">txtPopulation</TextBox>
            </GridPanel>

            <GridPanel Columns="3">
                <SimpleText ID="lblBushels">Bushels</SimpleText>
                <TextBox Background="#87ceeb" IsEnabled="False"
				ID="txtBushels">txtBushels</TextBox>
                <Button ID="btnFeed" Click="btnFeed_Click">Feed</Button>
            </GridPanel>

            <GridPanel Columns="4">
                <SimpleText ID="lblLandPrice">Land Price</SimpleText>
                <TextBox Background="#87ceeb" IsEnabled="False"
				ID="txtLandPrice">txtLandPrice</TextBox>
                <Button ID="btnBuy" Click="btnBuy_Click">Buy</Button>
                <Button ID="btnSell" Click="btnSell_Click">Sell</Button>
            </GridPanel>

            <GridPanel Columns="3">
                <SimpleText ID="lblAcres">Acres</SimpleText>
                <TextBox Background="#87ceeb" IsEnabled="False"
				ID="txtAcres">txtAcres</TextBox>
                <Button ID="btnPlant" Click="btnPlant_Click">Plant</Button>
            </GridPanel>

            <GridPanel Columns="2">
                <SimpleText ID="lblInput">Input</SimpleText>
                <TextBox ID="txtInput">0</TextBox>
            </GridPanel>

            <GridPanel Columns="1"><Button ID="btnAdvanceYear"
			Click="btnAdvanceYear_Click">Advance Year</Button></GridPanel>
            <GridPanel Columns="4">
                <Button ID="btnEmulate"
				Click="btnEmulate_Click">Emulate</Button>
                <TextBox ID="txtEmulate">plant 4 acres</TextBox>
                <Button ID="btnAccept"
				Click="btnAccept_Click">Accept</Button>
            </GridPanel>

        </GridPanel>

        <TextBox Background="#87ceeb" IsEnabled="False" DockPanel.Dock="Top"
		ID="txtOutput"></TextBox>

    </DockPanel>
</Window>

Listing 2

//This is a list of commonly used namespaces for a window.
using System;
using Srgs = System.Speech.Srgs;
using System.Speech.Recognition;

using MSAvalon.Windows;
using MSAvalon.Windows.Controls;
using MSAvalon.Windows.Documents;
using MSAvalon.Windows.Navigation;
using MSAvalon.Windows.Shapes;
using MSAvalon.Windows.Data;
using MSAvalon.Threading;

namespace Hamurabi.WinFX
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : Window
    {

		GameLogic gameLogic;
		UIContextOperationDelegate uiDelegate;
		Srgs.SrgsGrammar g;
		String command;
		private delegate void CommandDelegate(object sender, ClickEventArgs e);
		CommandDelegate cg;

		private void btnEmulate_Click(object sender, ClickEventArgs e)
		{
			g.Recognizer.EmulateRecognize(txtEmulate.Text);
		}

		private void WindowLoaded(object sender, EventArgs e)
		{
			gameLogic = new GameLogic();
			printf("Try your hand at governing ancient Samaria");
			printf("for a 10-year term of office.\r\n");
			YearlyReport(true);
			g.Recognition += new RecognitionEventHandler(RecognitionEvent);
			g.IsActive = true;
			uiDelegate = new UIContextOperationDelegate(updateScreen);	
		}

		private Srgs.SrgsGrammar CreateHamurabiGrammar()
		{
			Srgs.SrgsGrammar grammar = new Srgs.SrgsGrammar("Hamurabi");
			Srgs.OneOf topOneOf = new Srgs.OneOf();

			topOneOf.Elements.Add(createItem("Feed", gameLogic.Bushels,
			"Bushels"));
			topOneOf.Elements.Add(createItem("Plant", gameLogic.Acres, "Acres"));
			topOneOf.Elements.Add(createItem("Buy", gameLogic.Bushels, "Acres"));
			topOneOf.Elements.Add(createItem("Sell", gameLogic.Acres, "Acres"));

			Srgs.Rule rule = new Srgs.Rule("Hamurabi");

			rule.Elements.Add(topOneOf);
			grammar.Rules.Add(rule);
			grammar.Root = rule;
			return grammar;
		}

		private Srgs.Item createItem(String command, int maxUnits, String currency)
		{
	Srgs.Item item = new Srgs.Item(command, new Srgs.Tag("$.command=\"" +
			command +"\";"));
			Srgs.OneOf numberOneOf = new Srgs.OneOf();

			for (int i = 1; i <= maxUnits; i++)
			{
				string tag = String.Format("$.number=\"{0}\";", "" + i);

				numberOneOf.Elements.AddItem("" + i, tag);
			}

			item.Elements.AddOneOf(numberOneOf);
			item.Elements.AddOptionalItem(currency);
			return item;
		}


		private void RecognitionEvent(object s, RecognitionEventArgs e)
		{
			command = e.Result.Properties["command"].Value.ToString();
			txtInput.Context.Invoke(uiDelegate, e);

			if (command.Equals("plant", StringComparison.CurrentCultureIgnoreCase))
			{
				cg = new CommandDelegate(btnPlant_Click);
			}
			else if (command.Equals("feed",
			StringComparison.CurrentCultureIgnoreCase))
			{
				cg = new CommandDelegate(btnFeed_Click);
			}
			else if (command.Equals("buy",
			StringComparison.CurrentCultureIgnoreCase))
			{
				cg = new CommandDelegate(btnBuy_Click);
			}
			else
			{
				cg = new CommandDelegate(btnSell_Click);
			}
		}

		private void btnAccept_Click(object sender, ClickEventArgs e)
		{
			cg(sender, e);
		}

		private Object updateScreen(Object state)
		{
			txtInput.Text =
		(RecognitionEventArgs)state).Result.Properties["number"].Value.ToString();
			return state;
		}


		private void printf(String text)
		{
			txtOutput.Text = txtOutput.Text + (text + "\r\n");
		}


		private void YearlyReport(bool stillPlaying)
		{
			gameLogic.StartYear();
			printf("Hamurabi, I beg to report to you:");
			printf("In year " + gameLogic.CurrentYear + ", " +
			gameLogic.Starvations + " people starved, ");
			printf(gameLogic.Births + " people were born.\r\n");
			if (gameLogic.DidPlagueStrike)
			{
				printf("A horrible plague struck!  Half the people died!\r\n");
			}

			if (!stillPlaying)
			{
		printf("In your " + (gameLogic.CurrentYear - 1) + "-year term of
		office, " + gameLogic.AverageAnnualStarvations + " % of the");
		printf("population starved per year on the average, i.e.");
		printf("a total of " + gameLogic.TotalStarvations + " people were
		starved!");
		printf("You started with 18 acres per person and ended");
		printf("with " + gameLogic.AcresPerPerson + " acres per
		person.\r\n");
		switch (gameLogic.CalculatePlayerRank())
				{
					case 0 :
				printf("Due to this extreme mismanagement, you have");
				printf("not only been impeached and thrown out of");
				printf("office but you have also been declared a");
				printf("national fink!");
						break;

					case 1 :
				printf("Horrible, just horrible!  you are lucky to
				still\r\nbe alive!");
						break;

					case 2 :
				printf("Your heavy-handed performance smacks of Nero and");
				printf("Ivan IV.  The people (remaining) find you an");
				printf("unpleasant ruler and, frankly, hate your guts!!");
					break;

					case 3 :
				int tmp = (int)Math.Round((double)gameLogic.Population *
				new Random().Next(8) / 10);

				printf("Your performance could have been somewhat
				better,");
				printf("but really wasn't too bad.  " + tmp + " people
				would");
				printf("dearly like to see you assassinated, but we all");
				printf("have our trivial problems.");
						break;

					default :
				printf("A fantastic performance!!  Charlemagne,
				Disraeli,");
				printf("and Jefferson combined could not have done
				better!");
						break;
				}
				txtInput.IsEnabled = false;
				btnAdvanceYear.IsEnabled = false;
			}
			else
			{
				UpdateStatusFields();
		printf("You harvested " + gameLogic.YieldedBushels + " bushels per
		acre.");
		printf("Rats ate " + gameLogic.RatConsumption + " bushels.\r\n");
			}
			g = CreateHamurabiGrammar();
		}


		private void UpdateStatusFields()
		{
			txtPopulation.Text = "" + gameLogic.Population;
			txtAcres.Text = "" + gameLogic.Acres;
			txtBushels.Text = "" + gameLogic.Bushels;
			txtLandPrice.Text = "" + gameLogic.LandPrice;
		}


		private void btnFeed_Click(object sender, ClickEventArgs e)
		{
			int tmp = GetInput();

			try
			{
				gameLogic.FeedPeople(tmp);
				btnFeed.IsEnabled = false;
				UpdateStatusFields();
			}
			catch (InsufficientGrainException)
			{
				printf("You don't have that many bushels!");
			}
		}


		private void btnPlant_Click(object sender, ClickEventArgs e)
		{
			int tmp = GetInput();

			try
			{
				gameLogic.PlantSeed(tmp);
				UpdateStatusFields();
				btnPlant.IsEnabled = false;
			}
			catch (InsufficientLandException)
			{
				printf("You don't have that many acres!");
			}
			catch (InsufficientPeopleException)
			{
				printf("You don't have enough people to tend the fields!");
			}
			catch (InsufficientGrainException)
			{
				printf("You don't have enough seed!");
			}
		}


		private void btnBuy_Click(object sender, ClickEventArgs e)
		{
			int tmp = GetInput();

			try
			{
				gameLogic.BuyLand(tmp);
				UpdateStatusFields();
				btnBuy.IsEnabled = false;
				btnSell.IsEnabled = false;
			}
			catch (InsufficientGrainException)
			{
				printf("You don't have that much grain!");
			}
		}


		private void btnSell_Click(object sender, ClickEventArgs e)
		{
			int tmp = GetInput();

			try
			{
				gameLogic.SellLand(tmp);
				UpdateStatusFields();
				btnSell.IsEnabled = false;
				btnBuy.IsEnabled = false;
			}
			catch (InsufficientLandException)
			{
				printf("You don't have that many acres!");
			}
		}


		private int GetInput()
		{
			int tmp = Int32.Parse(txtInput.Text);

			txtInput.Text = "0";
			return tmp;
		}


		private void btnAdvanceYear_Click(object sender, ClickEventArgs e)
		{
			bool stillPlaying = gameLogic.EndYear();

			txtOutput.Text = "";
			YearlyReport(stillPlaying);
			btnFeed.IsEnabled = true;
			btnPlant.IsEnabled = true;
			btnBuy.IsEnabled = true;
			btnSell.IsEnabled = true;
		}

    }
}