Martin Dimitrov Martin Dimitrov - 2 months ago 18
C# Question

Text bombardment

this is the program I'm trying to write:


Write a program that reads a text and line width from the console. The program should distribute the text so that it fits in a table with a specific line width. Each cell should contain only 1 character. It should then read a line with numbers, holding the columns that should be bombarded.


My code looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04.Text_Bombardment
{
class Program
{
static void Main(string[] args)
{
var sentence = Console.ReadLine();
var bombing = int.Parse(Console.ReadLine());
var selected = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
Dictionary<int, bool> bombDict = new Dictionary<int, bool>();
var newSentence = sentence + new string(' ', bombing - sentence.Length % bombing); // whole row minus words left

for (int i = 0; i < selected.Length; i++)
{
bombDict.Add(selected[i], true);
}
var rows = newSentence.Length / bombing;
var cols = bombing;

var count = 0;

var arrSent = newSentence.ToCharArray();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (bombDict.ContainsKey(j) && bombDict[j] == true && arrSent[count] != ' ')
{
arrSent[count] = ' ';
try
{
if (arrSent[count + bombing] == ' ')
{
bombDict[j] = false;
}
else
{
bombDict[j] = true;
}

}
catch (IndexOutOfRangeException)
{
continue;
}
}
count++;
}
}
var finalSent = string.Join("", arrSent).TrimEnd();
Console.WriteLine(finalSent);
}
}
}


And it breaks on this sentence:

Vazov received his elementary education in hisnative town of Sopoandat Plovdiv. The son of a conservative, well-to-do merchant.
20
1 6 17 2 5 0 15


Current Output:

ov eceived i e en ry educa i n hi ative to n of opo dat Plov iv. T e s of a co serva ive well-to- o mer ha t.


Intended Output:

ov eceived i e en ry educa i n hi ative to n of opo dat Plov iv. T e s of a co serva ive well-to- o mer han .


Soo it only doesn't work on the end.
Can someone help me?
Any suggestions?




Additional notes:

For example, we read the text "Well this problem is gonna be a ride." and line width 10. We distribute the text among 4 rows with 10 columns. We read the numbers "1 3 7 9" and drop bombs on those columns in the table.
The bombs destroy the character they fall on + all the neighbouring characters below it.

Note: Empty spaces below destroyed characters stop the bombs (see column 7).
Finally, we print the bombarded text on the console: "W l th s p o lem i o na be a r de."
Note: The empty cells in the table after the text should NOT be printed.

enter image description here

Answer

Your solution is very difficult to understand, keep it simple give names to the variables that you can understand easily.

I modified your code, hope it helps you:

static void Main()
{
    string sentence = "Vazov received his elementary education in hisnative town of Sopoandat Plovdiv. The son of a conservative, well-to-do merchant.";
    int numberOfColumns = int.Parse("20");
    int[] bombs = "1 6 17 2 5 0 15".Split(' ').Select(int.Parse).ToArray();

    // we need to convert to decimal, otherwise C# will ignore decimal part. 
    //example: 127/20 = 6.35, so we need 7 rows. if we don't convert to decimal we have 6
    // the Ceiling says, always round up. so even 6.1 will be rounded to 7
    int numberOfRows = (int)Math.Ceiling(sentence.Length / Convert.ToDecimal(numberOfColumns));

    char[,] array = new char[numberOfRows, numberOfColumns];

    int sentencePointer = 0;
    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
    {
        for (int colIndex = 0; colIndex < numberOfColumns; colIndex++)
        {
            // if you want to print the grid with the full text, just comment the 3 lines below,
            //and keep only "array[rowIndex, colIndex] = sentence[sentencePointer];"
            if (bombs.Contains(colIndex) && sentence[sentencePointer] != ' ') // only if it has a character
                array[rowIndex, colIndex] = '*'; // * represents a bomb
            else
                array[rowIndex, colIndex] = sentence[sentencePointer];

            sentencePointer++; // move next character
            if (sentencePointer >= sentence.Length)
                break; // we reach the end of the sentence.
        }
    }

    PrintGrid(array, numberOfRows, numberOfColumns);

    // just give some space to print the final sentence
    Console.WriteLine("");
    Console.WriteLine("");
    Console.WriteLine("");

    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
    {
        for (int colIndex = 0; colIndex < numberOfColumns; colIndex++)
        {
            Console.Write(array[rowIndex,colIndex]);
        }
    }

    Console.ReadKey();
}

private static void PrintGrid(char[,] array, int numberOfRows, int numberOfColumns)
{
    Console.WriteLine(new string('-', numberOfColumns * 2));

    for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
    {
        Console.Write("|");
        for (int colIndex = 0; colIndex < numberOfColumns; colIndex++)
        {
            Console.Write(array[rowIndex, colIndex]);
            Console.Write("|");
        }
        Console.WriteLine("");
    }
}