Home > front end >  Console doesnt display but the first row
Console doesnt display but the first row

Time:11-17

We are trying to write 3 arrays on console

like this;

DDDEEDUDE

EDUDEU

DUUEUEDD

randomly selecting arrays and letters code given runs completely fine but it doesn't display but the first line. I couldn't come up with a bright idea somehow. First array runs perfect but other arrays don't display.

string[] A1 = new string[15];
string[] A2 = new string[15];
string[] A3 = new string[15];
Random rastgele = new Random();
int selected_array = 0;
int gamecount_for_a1 = 0;
int gamecount_for_a2=0;
int gamecount_for_a3=0;
string which_letter_str;

while (true)
{
    int which_array = rastgele.Next(3);
    if (which_array == 0)
    {
        selected_array = 0;//A1
    }
    else if (which_array == 1)
    {
        selected_array = 1;//A2
    }
    else
    {
        selected_array = 2;//A3
    }

    int which_letter_int= rastgele.Next(3);
    if  (which_letter_int == 0)
    {
        which_letter_str = "D";
    }
    else if (which_letter_int == 1)
    {
        which_letter_str = "E";
    }
    else 
    {
        which_letter_str = "U";
    }

    if (selected_array ==0)
    {
        A1[gamecount_for_a1] = which_letter_str;
        Console.SetCursorPosition(gamecount_for_a1, 0);
        Console.Write(A1[gamecount_for_a1]);
        gamecount_for_a1  ;
    }
    else if (selected_array == 1)
    {
        A1[gamecount_for_a2] = which_letter_str;
        Console.SetCursorPosition(gamecount_for_a2, 2);
        Console.Write(A2[gamecount_for_a2]);
        gamecount_for_a2  ;
    }
    else 
    {
        A1[gamecount_for_a3] = which_letter_str;
        Console.SetCursorPosition(gamecount_for_a3, 4);
        Console.Write(A3[gamecount_for_a3]);
        gamecount_for_a3  ;
    }
    Console.ReadLine();
    if (gamecount_for_a1 == 15 || gamecount_for_a2 == 15 || gamecount_for_a3 == 15)
        break;
}




CodePudding user response:

You have two bugs in your code:

A1[gamecount_for_a2]
A1[gamecount_for_a3]

The arrays should be A2 and A3.

CodePudding user response:

You can refactor your code to the following:

    const int ArrayLength = 15;

    const int A1_Count_Index = 0;
    const int A2_Count_Index = 1;
    const int A3_Count_Index = 2;

    const int SpaceBetweenArrayDisplays = 2;

    string[] A1 = new string[ArrayLength];
    string[] A2 = new string[ArrayLength];
    string[] A3 = new string[ArrayLength];

    string[][] ArraysInPlay = new string[][] { A1, A2, A3 };
    string[] Letters = new[] { "D", "E", "U" };
    
    int[] GameCounts = new int[] { 0, 0, 0 }; //index 1--> count for A1, index 2 --> count for A2, index 3 count for A3

    Random rastgele = new Random();

    while (GameCounts[A1_Count_Index] < ArrayLength && GameCounts[A2_Count_Index] < ArrayLength && GameCounts[A3_Count_Index] < ArrayLength)
    {
        var selected_array_index = rastgele.Next(ArraysInPlay.Length);
        var letter_index = rastgele.Next(Letters.Length);

        var gameCount = GameCounts[selected_array_index];
        var cursorPosition = selected_array_index * SpaceBetweenArrayDisplays; // if selected array 0--> 0, if 1--> 2, if 2 --> 4

        var selectedArray = ArraysInPlay[selected_array_index];
        selectedArray[gameCount] = Letters[letter_index];
        Console.SetCursorPosition(gameCount, cursorPosition);
        Console.Write(selectedArray[gameCount]);
        gameCount  ;
        GameCounts[selected_array_index] = gameCount;
    }

    // move cursor to the bottom line
    Console.SetCursorPosition(0, ArraysInPlay.Length * SpaceBetweenArrayDisplays);

    Console.ReadLine();
  • Related