// ---------------------------------------------------------- // Using Directives // ---------------------------------------------------------- using System.Collections.Generic; using System.IO; using System; // ---------------------------------------------------------- // RC - A row and a column // ---------------------------------------------------------- public class RC { public RC(int row, int column) { Row = row; Column = column; } public int Row { get; private set; } public int Column { get; private set; } } // ---------------------------------------------------------- // Board // ---------------------------------------------------------- public class Board { public const int SIZE = 10; public const char EMPTY = '.'; // ---------------------------------------------------------- // Board - Create a square board using the specified 'size' // ---------------------------------------------------------- public Board(int size = SIZE) { Initialize(size, out mSize, out mGrid); } // ---------------------------------------------------------- // Reset - Clear the board // ---------------------------------------------------------- public void Reset() { Initialize(mSize, out mSize, out mGrid); } // ---------------------------------------------------------- // PlaceString - Returns true if 's' can be placed on the // board, false otherwise // ---------------------------------------------------------- public bool PlaceString(string s) { Random r = new Random(); int row = r.Next(mSize); int column = r.Next(mSize); int placed = 0; foreach(char c in s) { if(placed++ > 0) { var list = GetLocations(row, column); if(list.Count < 1) break; int index = r.Next(list.Count); row = list[index].Row; column = list[index].Column; } mGrid[row, column] = c; } return(placed == s.Length); } // ---------------------------------------------------------- // ToString - Return the board as a string // ---------------------------------------------------------- public override string ToString() { string line = "+"; for(int i = 0; i < mSize; i ++) line += "---+"; string output = line + "\n"; for(int i = 0; i < mSize; i ++) { for(int j = 0; j < mSize; j ++) output += (j == 0 ? "|" : "") + " " + mGrid[i, j] + " " + "|"; output += "\n"; if(i + 1 == mSize) output += line; } return(output); } // ---------------------------------------------------------- // Show - Display the board to the console // ---------------------------------------------------------- public void Show() { Console.WriteLine(ToString()); } // ---------------------------------------------------------- // GetLocations - Returns a list of row/column pairs, the // possible moves from (row, column) // ---------------------------------------------------------- private List GetLocations(int row, int column) { // Legal moves are: left, right, down, up RC[] offsets = { new RC(-1, 0), new RC( 1, 0), new RC( 0, -1), new RC( 0, 1) }; int rows = mGrid.GetLength(0); int columns = mGrid.GetLength(1); var list = new List(); for(int i = 0; i < offsets.Length; i ++) { int r = row + offsets[i].Row; int c = column + offsets[i].Column; if(r >= 0 && r < rows && c >= 0 && c < columns && mGrid[r, c] == EMPTY ) { list.Add(new RC(r, c)); } } return(list); } // ---------------------------------------------------------- // Initialize - Create our grid and fill with EMPTY chars // ---------------------------------------------------------- private static void Initialize(int theSize, out int size, out char[,] grid) { size = theSize; grid = new char[size, size]; for(int i = 0; i < size; i ++) for(int j = 0; j < size; j ++) grid[i, j] = EMPTY; } // ---------------------------------------------------------- // Member Variables // ---------------------------------------------------------- private char[,] mGrid; private int mSize; } // ---------------------------------------------------------- // StringPath // ---------------------------------------------------------- public class StringPath { public const string LINE = "==============================="; // ---------------------------------------------------------- // Banner - Show 's' with a line above and below it // ---------------------------------------------------------- public static void Banner(string s) { Console.WriteLine(LINE + "\n" + s + "\n" + LINE + "\n"); } // ----------------------------- // Main // ----------------------------- public static void Main() { string theString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Board b = new Board(); Banner( "Result: " + (b.PlaceString(theString) ? "Success" : "Failure") ); b.Show(); Console.Read(); } }