So I need to write a program that assigns characters (11,111,1111,22,222,2222) in 4 one dimensional arrays without any of them stacking up against each other. I have to do this in the console application(.net framework). My code is like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace program
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
char[] road0 = new char[53];
for (int i = 0; i < road0.Length; i++)
{
road0[i] = '.';
}
char[] road1 = new char[53];
for (int i = 0; i < road1.Length; i++)
{
road1[i] = '#';
}
char[] road2 = new char[53];
for (int i = 0; i < road2.Length; i++)
{
road2[i] = '.';
}
char[] road3 = new char[53];
for (int i = 0; i < road3.Length; i++)
{
road3[i] = '.';
}
char[] road4 = new char[53];
for (int i = 0; i < road4.Length; i++)
{
road4[i] = '.';
}
char[] road5 = new char[53];
for (int i = 0; i < road5.Length; i++)
{
road5[i] = '.';
}
Console.CursorVisible = false;
int type = 0;
int road = 0;
int length = 0;
int position1 = 0;
for (int i = 0; i < 11; i++)
{
type = 1;
road = random.Next(2, 6);
length = random.Next(2, 5);
position1 = random.Next(0, 48);
if (road2[position1 + (length + 1)] == '.' && road3[position1 + (length + 1)] == '.' && road4[position1 + (length + 1)] == '.' && road5[position1 + (length + 1)] == '.')
{
for (int k = 1; k <= length; k++)
{
if (road == 2 && type == 1)
{
road2[position1 + k] = '1';
}
else if (road == 3 && type == 1)
{
road3[position1 + k] = '1';
}
else if (road == 4 && type == 1)
{
road4[position1 + k] = '1';
}
else if (road == 5 && type == 1)
{
road5[position1 + k] = '1';
}
}
}
}
for (int j = 0; j < 2; j++)
{
int position2 = 0;
for (int i = 0; i < length; i++)
{
if (position1 != position2)
{
if (road2[position2 + (length + 1)] == '.' || road3[position2 + (length + 1)] == '.' || road4[position2 + (length + 1)] == '.' || road5[position2 + (length + 1)] == '.')
{
type = 2;
road = random.Next(2, 6);
length = random.Next(2, 5);
position2 = random.Next(0, 48);
for (int k = 1; k <= length; k++)
{
if (road == 2 && type == 2)
{
if (road2[position2 + k] == '.' && road2[position2 - k] == '.')
{
road2[position2 + k] = '2';
}
}
else if (road == 3 && type == 2)
{
if (road3[position2 + k] == '.' && road3[position2 - k] == '.')
{
road3[position2 + k] = '2';
}
}
else if (road == 4 && type == 2)
{
if (road4[position2 + k] == '.' && road4[position2 - k] == '.')
{
road4[position2 + k] = '2';
}
}
else if (road == 5 && type == 2)
{
if (road5[position2 + k] == '.' && road5[position2 - k] == '.')
{
road5[position2 + k] = '2';
}
}
}
}
}
}
}
Console.SetCursorPosition(0, 0);
Console.WriteLine(road1);
Console.WriteLine(road2);
Console.WriteLine(road3);
Console.WriteLine(road4);
Console.WriteLine(road5);
Console.WriteLine(road0);
Console.ReadLine();
}
}
}
I don't understand where I am doing this wrong. The program sometimes assigns numbers like '111111, 112221, 1222, 211, 222222' and I couldn't solve how to make it stop assigning those numbers.
Aucun commentaire:
Enregistrer un commentaire