0

私はC#でGameofLifeをコーディングして学習プロセスを進めています。pictureBoxユーザーが各セルをクリックしやすいように、を使用して上のグリッドを表示することができました。ではbutton1、bool変数fill_inに、クリックしたときに特定のセルに入力される値を割り当てることができます。しかし、私は現在、テキストファイルから取得した選択したセルの読み込みxと座標を実験しています。このようなテキストファイルの最初の行には、 (スペース)yという値が含まれます。どうすればこれを行うことができますか?26050

コード

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

        }
4

1 に答える 1

1

テキストファイルから値のペア「xy」を取得するときに、fill_in [x、y]をtrueに設定するとします。だからここにコードスニップがあります-そうするためです。

 while (infile.Peek() >= 0) 
 {
     string[] values = infile.ReadLine().Split(' ');
     fill_in[int.Parse(values[0]), int.Parse(values[1])] = true;
 }

基本的な考え方は単純で、ファイルがなくなるまでファイルから行を読み込みます。次に、スペースで区切られた2つのintがあるという基準に基づいて、上記の行を分割します。この方法は、テキストファイル内の不正な形式のデータをチェックせず、例外を処理しないため、非常に迅速で大雑把ですが、テキストファイルの作成に注意すれば作業は完了します。

ファイルからデータを読み取って解析することはプログラミングにおいて非常に重要なので、主題をグーグルで調べて概念を理解することをお勧めします。

于 2012-10-05T04:17:23.263 に答える