Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

Reversi in C#

Rate me:
Please Sign up or sign in to vote.
4.94/5 (188 votes)
26 Sep 200513 min read 769.4K   27K   300  
The game of Reversi in C#.
using System;
using System.Drawing;
using System.Xml;

namespace Reversi
{
	/// <summary>
	/// Summary description for ProgramSettings.
	/// </summary>
	public class ProgramSettings
	{
		// The file name used to save the program settings.
		private string fileName;

		// An Xml document used to store and save program settings.
		private XmlDocument document;

		public ProgramSettings(string fileName)
		{
			//
			// TODO: Add constructor logic here
			//

			// Assign the file name.
			this.fileName = fileName;

			// If the file already exists, load it. Otherwise, create a new document.
			this.document = new XmlDocument();
			try
			{
				document.Load(this.fileName);
			}
			catch (Exception ex)
			{
				// Create a new XML document and set the root node.
				document = new XmlDocument();
				document.AppendChild(document.CreateElement("ProgramSettings"));
			}
		}

		//
		// Saves the settings XML file.
		//
		public void Save()
		{
			this.document.Save(this.fileName);
		}

		//
		// Reads a value from the settings file.
		//
		public string GetValue(string section, string name)
		{
			try
			{
				return this.document.DocumentElement.SelectSingleNode(section + "/" + name).InnerText;
			}
			catch (Exception ex)
			{
				return "";
			}
		}

		//
		// Writes a value to the settings file.
		//
		public void SetValue(string section, string name, string value)
		{
			// If the section does not exist, create it.
			XmlNode sectionNode = this.document.DocumentElement.SelectSingleNode(section);
			if (sectionNode == null)
				sectionNode = this.document.DocumentElement.AppendChild(this.document.CreateElement(section));
			// If the node does not exist, create it.
			XmlNode node = sectionNode.SelectSingleNode(name);
			if (node == null)
				node = sectionNode.AppendChild(this.document.CreateElement(name));

			// Set the value.
			node.InnerText = value;
		}
	}
}

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions