我正在通过使用 C# the Game of Life 编写代码来经历一个学习过程。我已经能够使用 apictureBox
并在上方显示一个网格,以便用户可以轻松地单击每个单元格。在button1
我能够为布尔变量分配fill_in
一个值,单击该值将填充某个单元格。但我现在正在尝试从文本文件中抓取选定单元格的加载x
和y
坐标。这样的文本文件将在第一行具有值:260
(space) 50
。任何想法我怎么能做到这一点?
代码
namespace life
{
public partial class Form1 : Form
{
Graphics paper;
bool[,] fill_in = new bool[450, 450];
public Form1()
{
InitializeComponent();
paper = pictureBox1.CreateGraphics();
}
//makes grid in picture box
private void drawGrid()
{
int numOfCells = 100;
int cellSize = 10;
Pen p = new Pen(Color.Blue);
paper.Clear(Color.White);
for (int i = 0; i < numOfCells; i++)
{
// Vertical
paper.DrawLine(p, i * cellSize, 0, i * cellSize, numOfCells * cellSize);
// Horizontal
paper.DrawLine(p, 0, i * cellSize, numOfCells * cellSize, i * cellSize);
}
}
// populate bool fill_in with true (alive) or false (dead)
private void clearGrid()
{
for (int x = 0; x < 450; x = x + 10)
{
for (int y = 0; y < 450; y = y + 10)
{
fill_in[x, y] = false;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
drawGrid();
clearGrid();
//randomly populate grid squares
fill_in[50, 50] = true;
fill_in[60, 50] = true;
fill_in[30, 40] = true;
fill_in[40, 40] = true;
for (int x = 0; x < 440; x = x + 10)
{
for (int y = 0; y < 440; y = y + 10)
{
if (fill_in[x, y] == true)
paper.FillRectangle(Brushes.Black, x, y, 10, 10);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openReport = new OpenFileDialog();
openReport.Filter = "Text Files | *.txt";
openReport.ShowDialog();
StreamReader infile = File.OpenText(openReport.FileName);
//Need Help/Guidance read text file coordinates and populate grid
}