// возвращает коня на один ход назад private void backMoveButton_Click(object sender, EventArgs e) { try { if (count == 1) { RemoveSolve(); } else { foreach (var i in cellsList) { if (i.Text == count.ToString()) { string cellName = i.Name; var cellIndex = cellName.Where(j => Char.IsDigit(j)).ToArray(); int x = int.Parse(cellIndex[0].ToString()); int y = int.Parse(cellIndex[1].ToString()); i.Text = ""; count--; cells[y, x] = 0; break; } } foreach (var i in cellsList) { if (i.Text == count.ToString()) { RemoveKnight(); ReturnColor(); i.BackgroundImage = knightImage; i.BackgroundImageLayout = ImageLayout.Stretch; i.Text = count.ToString(); string cellName = i.Name; var cellIndex = cellName.Where(j => Char.IsDigit(j)).ToArray(); int x = int.Parse(cellIndex[0].ToString()); int y = int.Parse(cellIndex[1].ToString()); WarnsdorfsRule.Vector2 cell = new WarnsdorfsRule.Vector2(x, y); List <WarnsdorfsRule.Vector2> neighbours = game.GetNeighbours(cell, cells); checkSolve = false; string cellString = "cell"; foreach (var n in neighbours) { cellString += n.x.ToString() + n.y.ToString(); Button b = (Button)Controls.Find(cellString, true)[0]; if (i.Text != "") { b.BackColor = Color.LightGreen; b.Enabled = true; } cellString = "cell"; } EnabledButtons(); break; } } } } catch { MessageBox.Show("Ошибка!", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void cell00_Click(object sender, EventArgs e) { // нахождение нажатой кнопки string cellName = (sender as Button).Name; Button cellXY = (Button)Controls.Find(cellName, true)[0]; // вычисление координатов клетки var cellIndex = cellName.Where(i => Char.IsDigit(i)).ToArray(); int x = int.Parse(cellIndex[0].ToString()); int y = int.Parse(cellIndex[1].ToString()); RemoveKnight(); ReturnColor(); cells[y, x] = ++count; // "установка" коня на клетку cellXY.BackgroundImage = knightImage; cellXY.BackgroundImageLayout = ImageLayout.Stretch; cellXY.Text = count.ToString(); string cellString = "cell"; WarnsdorfsRule.Vector2 cell = new WarnsdorfsRule.Vector2(x, y); List <WarnsdorfsRule.Vector2> neighbours = game.GetNeighbours(cell, cells); if (neighbours.Count == 0 && count != N * N) { EnabledButtons(); checkSolve = true; DialogResult result = MessageBox.Show("Хотите начать заново?", "Вы проиграли!", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { RemoveSolve(); } } else if (neighbours.Count == 0 && count == N * N) { EnabledButtons(); checkSolve = true; DialogResult result = MessageBox.Show("Хотите начать заново?", "Вы выиграли!", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { RemoveSolve(); } } else { checkSolve = false; // подсветка клеток, на которые можно сделать ход конём foreach (var i in neighbours) { cellString += i.x.ToString() + i.y.ToString(); Button b = (Button)Controls.Find(cellString, true)[0]; if (cellXY.Text != "") { b.BackColor = Color.LightGreen; b.Enabled = true; } cellString = "cell"; } EnabledButtons(); } }