Reputation: 35
Programming novice here. I am trying to generate random coordinates for my points But I am getting same numbers like (1,1) , (2,2) (5,5) etc
I put the random generator out of loop but that didn't help.
My Output looks like this
Point#0(4,4) Point#1(8,8) Point#2(9,9) Point#3(1,1) Point#4(0,0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dotsGrids
{
class Program
{
static void Main(string[] args)
{
Grid grid = new Grid(9,5);
Console.WriteLine(grid.RenderGrid());
Console.WriteLine("END.");
Console.ReadKey();
}
}
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Grid
{
static Random r = new Random();
int size;
int tempx, tempy;
Point[] p;
public Grid(int size,int numOfPoints)
{
this.size = size;
p = new Point[numOfPoints];
for (int i = 0; i < numOfPoints; i++)
{
tempx = r.Next(10);
tempy = r.Next(10);
p[i] = new Point(tempx,tempy);
}
}
public string RenderGrid()
{
string s = " ";
string sum = "";
bool flag = true;
for(int i=1;i<= size; i++)
{
s += " " + i;
}
s += "\n";
for (int y=1;y<= size; y++)
{
s += y;
for (int x = 1;x<= size; x++)
{
for (int j = 0; j < p.Length; j++)
{
if (p[j].x == x && p[j].y == y)
{
if(p[j].x + p[j].y+"" != sum)//Prevent additional * if same coordinates
{
s += " *";
}
sum += p[j].x + p[j].y;
flag = false;
}
}
if(flag) s += " -";
flag = true;
}
s += "\n";
}
s += "\n"; s += "\n"; s += "\n";
for (int i = 0; i < p.Length; i++)
{
s += "Point#" + i + "(" + p[i].x + "," + p[i].x + ")";
s += "\n";
}
return s;
}
}
}
Upvotes: 0
Views: 724
Reputation: 1389
I think that you need to change this line:
s += "Point#" + i + "(" + p[i].x + "," + p[i].x + ")";
to this:
s += "Point#" + i + "(" + p[i].x + "," + p[i].y + ")";
You were printing out the x-coordinate both times, instead of the x and the y coordinates.
Upvotes: 3