저는 C# Game of Life로 코딩하여 학습 과정을 진행할 것입니다. 나는 pictureBox
을 사용하고 위에 그리드를 표시하여 사용자가 각 셀을 쉽게 클릭 할 수있게했습니다. button1
에서 나는 bool 변수 fill_in
을 클릭 할 때 특정 셀에 채울 값을 할당 할 수 있습니다. 하지만 지금은 텍스트 파일에서 선택한 셀의 좌표 x
과 y
좌표를 실험하고 있습니다. 이러한 텍스트 파일의 첫 줄에는 260
(공백) 50
값이 있습니다. 어떤 생각을 어떻게 할 수 있습니까?텍스트 파일 값에서 x 및 y 좌표 채우기 (Game of Life- Logic)
CODE
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
}
+1 이것은 정확히 내가 찾고있는 것입니다. while 루프를 'button2_click' 이벤트에 추가했지만 텍스트 필드를로드 할 때 결과가 나타나지 않습니다. 또한 검정색으로 채우기에 해당하는 값에 따라 표시해야합니다. – CodingWonders90
아마도 그려진 사각형을 새로 고쳐야 할 것입니다. (int y = 0, y <440, y = y + 10)에 대해이 코드를 'button2_click'의 끝에 추가해보십시오. for (int x = 0; x <440; x = x + 10) { for { if (fill_in [x, y] == true) paper.FillRectangle (Brushes.Black, x, y, 10, 10); } }'또는 더 나은 아직 그것을 밖으로 별도의 함수로 끌어 그냥 단추 클릭 이벤트 처리기에서 호출하십시오. – HerrFlockig
이것은 완벽하게 작동했습니다! 감사. – CodingWonders90