-2

各ボタン固有の座標を保持する2次元の文字列配列があります

string[,] gridPass = new string[20, 20];


    private void Form1_Load(object sender, EventArgs e)
    {
        foreach (int row in Enumerable.Range(0, 20))
        {
            foreach (int col in Enumerable.Range(0, 20))
            {

                Button b = new Button();
                b.Size = new System.Drawing.Size(30, 30);
                b.Location = new Point(row * 30, col * 30);
                gridPass[row, col] = row.ToString() + " - " + col.ToString();
                b.Tag =  gridPass[row, col];
                b.Text = gridPass[row, col];
                this.Controls.Add(b);
                b.Click += new EventHandler(AttackHandler);
            }


        }

ボタンのイベント ハンドラーを使用して攻撃するとき

private void AttackHandler(object sender, EventArgs e)
        {
            Button clickedButton;
            string tagValue = "";

            clickedButton = (Button)sender;
            tagValue = (string)clickedButton.Tag;
            theSea.attackLocation(tagValue);

        }

ボタンの座標が何であれ、明らかに 0 - 1 または 8 - 4 のような文字列を送信しています。その文字列を Sea クラスの attackLocation メソッドに渡すときに、これら 2 つの数値を抽出して、Sea クラスの配列でそれらを参照し、そこにボートがあるかどうかを確認できるようにしたいと考えています。基本的に別の配列のまったく同じ場所を参照するには、これらの X 値と Y 値が必要です。だから私は何かをすることができます。

public void attackLocation(string attackCoords)
    {
        MessageBox.Show("Attacking " + attackCoords);
        x = however to convert it back;
        y = however to convert it back;
        foreach (Ship s in shipList)
        {
            if (grid[x,y] == 0)
            {
                MessageBox.Show("Attacked this block before."); 
            }
4

3 に答える 3

3

行と列の値を保持するクラスを作成し、Tagそのオブジェクトに を設定します。その後、文字列変換を行う必要はありません。

class SeaPoint
{
  public int Row { get; set; }
  public int Column { get; set; }
}

ロード中:

        foreach (int col in Enumerable.Range(0, 20))
        {
            Button b = new Button();
            b.Size = new System.Drawing.Size(30, 30);
            b.Location = new Point(row * 30, col * 30);
            gridPass[row, col] = row.ToString() + " - " + col.ToString();
            b.Tag =  new SeaPoint() { Row = row, Column = col }; // <---  Changed.
            b.Text = gridPass[row, col];
            this.Controls.Add(b);
            b.Click += new EventHandler(AttackHandler);
        }

そして AttackHandler:

private void AttackHandler(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    var seaPoint = (SeaPoint)clickedButton.Tag; // <-- Changed
    theSea.attackLocation(seaPoint);  // rewrite attackLocation to accept a SeaPoint.
}
于 2013-01-24T05:20:29.963 に答える
0

String.Splitを使用してハイフンで区切られた値を抽出し、それらにString.Trimを適用してスペースを削除してから、それをint.Parseに渡して文字列を数値に変換できます。

//b.Tag =  "0 - 1";    
string []arr = b.Tag.ToString().Split('-');    
int num1 = int.Parse(arr[0].Trim());
int num2 = int.Parse(arr[1].Trim());
于 2013-01-24T05:17:59.763 に答える
0

次の正規表現を作成します。

new Regex(@"(\d+) - (\d+)")

数値を抽出する文字列に対して正規表現の Match を使用します。

http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

これは、2 つのグループを含む Match オブジェクトを返します (グループとキャプチャの違いを正しく覚えていると仮定します...)。グループの値は、2 つの整数の文字列表現になります。int.Parse() それら。

于 2013-01-24T05:19:21.077 に答える