.NET Web Service
The Towers of Hanoi as .NET Web Service in C#.
The World Wide Web is more and more used for application to application communication. The programmatic interfaces made available are referred to as Web services. This Web Service was written using Microsoft's Visual Studio.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
namespace TowersOfHanoi
{
public class Solution : System.Web.Services.WebService
{
public Solution()
{
InitializeComponent();
}
#region Component Designer generated code
private IContainer components = null;
private void InitializeComponent()
{
}
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
private int maxdisks = 10;
private System.Collections.ArrayList Moves = new ArrayList();
private void doHanoi(int n, string f, string t, string u)
{
if (n <= 1)
{
Moves.Add("move " + f + " --> " + t);
}
else
{
doHanoi(n - 1, f, u, t);
Moves.Add("move " + f + " --> " + t);
doHanoi(n - 1, u, t, f);
}
}
[WebMethod]
public string[] Solve(int n)
{
if ((n < 0) || (n > maxdisks))
{
throw new Exception("hanoi: number of disk must be between 1 and 10");
}
doHanoi(n, "1", "3", "2");
return (string[])(Moves.ToArray(typeof(string)));
}
}
}