Ejercicio
Matriz bidimensional como búfer para pantalla
Objetivo
Cree un programa de C# que declare una matriz bidimensional de caracteres de 70x20, "dibuje" 80 letras (X, por ejemplo) en posiciones aleatorias y muestre el contenido de la matriz en la pantalla.
Código de Ejemplo
using System; // Importing the System namespace for accessing Console and Random
class Program
{
static void Main()
{
// Declare a 70x20 two-dimensional array to represent the screen buffer
char[,] screenBuffer = new char[20, 70]; // 20 rows and 70 columns
// Fill the screen buffer with empty spaces initially
for (int row = 0; row < screenBuffer.GetLength(0); row++)
{
for (int col = 0; col < screenBuffer.GetLength(1); col++)
{
screenBuffer[row, col] = ' '; // Filling the array with empty spaces
}
}
// Create an instance of Random to generate random positions for the letters
Random rand = new Random();
// Draw 80 random 'X' characters in random positions on the screen buffer
for (int i = 0; i < 80; i++)
{
int row = rand.Next(0, 20); // Random row between 0 and 19
int col = rand.Next(0, 70); // Random column between 0 and 69
screenBuffer[row, col] = 'X'; // Place 'X' in the randomly chosen position
}
// Display the content of the screen buffer (the array)
for (int row = 0; row < screenBuffer.GetLength(0); row++)
{
for (int col = 0; col < screenBuffer.GetLength(1); col++)
{
Console.Write(screenBuffer[row, col]); // Print each character in the array
}
Console.WriteLine(); // Move to the next line after each row is printed
}
}
}