I have a double dimension string array that holds each buttons specific coordinates
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);
}
}
When I attack using the event handler on my buttons
private void AttackHandler(object sender, EventArgs e)
{
Button clickedButton;
string tagValue = "";
clickedButton = (Button)sender;
tagValue = (string)clickedButton.Tag;
theSea.attackLocation(tagValue);
}
It’s obviously sending a string like 0 – 1 or 8 – 4 whatever the button’s coordinates are.
When I pass that string to the attackLocation method in my Sea class I want to be able to extract those two numbers to reference them with the array in my Sea class to see if there is a boat there. I Need those X and Y values back to reference the exact same location in another array basically. So I can do something like.
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.");
}
You can use String.Split to extract the hyphen separated values and apply String.Trim on them to remove the spaces before we pass it to int.Parse to convert string to number.