Wordsearch – c# Grid class

When coding in c#/csharp, I use monodevelop. Here is the wordsearch base Grid class in c#, I am coding in different languages just for fun as such, but here is the main idea for the wordsearch. I have already done a basic version in php which is at the present state in creating a grid (but without any intersection of words with others). But here is the Grid class in php, and below is the similar setup in csharp.

The main class, is where the application executes from, it is very similar to c++ in that there is a main function

public static void Main(string[] args)

which takes the string[] args as a parameter, which is what is passed in on the command line.

Anyway, here is the code.. I have added in some comments to what the code is doing.

using System;
 
namespace wordsearchcsharp
{
	class Grid {
		protected int _size = 0;
		protected char[][] _grid;
 
		// the ": this(11)" means call the Grid(int size) constructor with 11 as a parameter
		public Grid() : this(11)
		{
			// default size of a grid = 11
		}
 
		public Grid(int size)
		{
			this._size = size;
			// create a new row (Y direction)
			this._grid = new char[this._size][];
			for (int i =0; i < this._size; i++)
			{
				// create new X direction row.
				this._grid[i] = createGridRow();
			}
		}
 
		// createGridRow, this can be inherited and thus altered to create any type of character in the grid 
		public virtual char[] createGridRow()
		{
			// need to insert a good seed into the random generator.. else same characters
			Random random = new Random((int)DateTime.Now.Ticks);
 
			// create a new row (X direction) and place characters into it.
			char[] grid_insert = new char[this._size];
			for (int i = 0; i < this._size; i++)
			{
				// 0 - 25 = alphabet, and also +97 is the ascii start for the character 'a'
				grid_insert[i] = (char)(random.Next(0,25)+97);
			}
			return grid_insert;
		}
 
		// printOutGrid, will print out the grid created above.
		// i = Y direction, and j = X direction.
		public void printOutGrid()
		{
			Console.WriteLine("The Grid");
			for (int i = 0; i < this._size; i++)
			{
				Console.Write(i + " ..");
				for (int j = 0; j < this._size; j++)
				{
					Console.Write(j + " = " + this._grid[i][j]);
				}
				Console.WriteLine("");
			}
		}
 
		public int returnSize()
		{
			return this._size;
		}
 
		// insert a character into the grid at position X, Y and the character 
		public void insertCharIntoGrid(int pX, int pY, char character)
		{
			this._grid[pY][pX] = character;
		}
 
		//  return the grid reference 
		public char[][] returnGrid()
		{
			return this._grid;
		}
 
	}
 
	class MainClass
	{
		public static void Main(string[] args)
		{
			Grid grid = new Grid();
			grid.printOutGrid();
		}
	}
}

And here is the output.

The Grid
0 ..0 = m1 = y2 = p3 = d4 = d5 = y6 = g7 = p8 = m9 = i10 = t
1 ..0 = k1 = c2 = y3 = n4 = i5 = m6 = q7 = f8 = g9 = p10 = j
2 ..0 = o1 = r2 = v3 = q4 = a5 = i6 = y7 = h8 = k9 = y10 = c
3 ..0 = h1 = u2 = f3 = s4 = v5 = r6 = p7 = g8 = h9 = j10 = b
4 ..0 = n1 = j2 = y3 = u4 = v5 = q6 = n7 = b8 = y9 = w10 = g
5 ..0 = g1 = m2 = i3 = x4 = r5 = a6 = e7 = a8 = w9 = i10 = f
6 ..0 = m1 = c2 = c3 = a4 = r5 = y6 = c7 = v8 = o9 = u10 = k
7 ..0 = f1 = e2 = k3 = c4 = n5 = j6 = s7 = t8 = m9 = g10 = j
8 ..0 = l1 = t2 = f3 = e4 = n5 = i6 = q7 = p8 = e9 = t10 = p
9 ..0 = e1 = w2 = n3 = h4 = j5 = r6 = h7 = o8 = b9 = e10 = o
10 ..0 = y1 = x2 = u3 = j4 = n5 = g6 = n7 = h8 = m9 = t10 = a

I use linux and thus mono is my csharp virtual machine setup, but here is a output of the file command in linux which tells me that the wordsearchcsharp.exe if a mono assembly and thus if you ./wordsearchcsharp.exe will goto the mono virtual machine.. it will link/expand to mono wordsearchcsharp.exe on the command line.

file wordsearchcsharp.exe
wordsearchcsharp.exe: PE32 executable for MS Windows (console) Intel 80386 32-bit Mono/.Net assembly

Leave a Reply

Your email address will not be published.