using System.Collections.Generic;
public static void Main()
List<List<char?>> board = GetEmptyBoard(boardSize);
var wordWasPlacedHorizontally = board.AddWordHorizontally(word, rowIndex, columnIndex, boardSize);
if (wordWasPlacedHorizontally)
board = GetEmptyBoard(boardSize);
var wordWasPlacedVertically = board.AddWordVertically(word, rowIndex, columnIndex, boardSize);
if (wordWasPlacedVertically)
private static List<List<char?>> GetEmptyBoard(int boardSize)
List<List<char?>> board = new();
for (var rowIndex = 0; rowIndex < boardSize; rowIndex++)
var row = new List<char?>();
for (var columnIndex = 0; columnIndex < boardSize; columnIndex++)
public static class Helpers
public static bool AddWordHorizontally(this List<List<char?>> board, string word, int rowIndex, int columnIndex, int boardSize)
Console.WriteLine($"Trying to place {word} horizontally on the board...");
if (string.IsNullOrWhiteSpace(word))
Console.WriteLine("Please provide an actual word.");
var wordLength = word.Length;
if (wordLength > boardSize)
Console.WriteLine($"You cannot place a word that is longer than {boardSize} letters. '{word}' contains {wordLength} letters.");
if (rowIndex >= boardSize)
Console.WriteLine($"A row index of {rowIndex} is too large for this board! Please use a row index < {boardSize}.");
if (columnIndex >= boardSize)
Console.WriteLine($"A column index of {columnIndex} is too large for this board! Please use a column index < {boardSize}.");
var columnIndexOfFinalLetter = columnIndex + wordLength;
if (columnIndexOfFinalLetter > boardSize)
Console.WriteLine($"{word} is too long to fit horizontally on the board when the first letter is put in column with index {columnIndex}.");
for (var i = 0; i < wordLength; i++)
board[rowIndex][i + columnIndex] = word[i];
Console.WriteLine("Success!");
public static bool AddWordVertically(this List<List<char?>> board, string word, int rowIndex, int columnIndex, int boardSize)
Console.WriteLine($"Trying to place {word} vertically on the board...");
if (string.IsNullOrWhiteSpace(word))
Console.WriteLine("Please provide an actual word.");
var wordLength = word.Length;
if (wordLength > boardSize)
Console.WriteLine($"You cannot place a word that is longer than {boardSize} letters. '{word}' contains {wordLength} letters.");
if (rowIndex >= boardSize)
Console.WriteLine($"A row index of {rowIndex} is too large for this board! Please use a row index < {boardSize}.");
if (columnIndex >= boardSize)
Console.WriteLine($"A column index of {columnIndex} is too large for this board! Please use a column index < {boardSize}.");
if (rowIndex + wordLength > boardSize)
Console.WriteLine($"{word} is too long to fit vertically on the board when the first letter is put in row with index {rowIndex}.");
for (var i = 0; i < wordLength; i++)
board[i + rowIndex][columnIndex] = word[i];
Console.WriteLine("Success!");
public static void Print(this List<List<char?>> matrix)
var boardBorder = "\n " + string.Concat(Enumerable.Repeat('-', 4 * matrix.Count - 1));
Console.WriteLine(boardBorder);
for (var rowIndex = 0; rowIndex < matrix.Count; rowIndex++)
var row = matrix[rowIndex];
foreach (var cellValue in row)
Console.Write(cellValue ?? ' ');
Console.WriteLine(boardBorder);
for (var i = 0; i < matrix.Count; i++)