private void InitializeBoard() { box = new PictureBox[8, 8]; board = new Board(); //создание шахматного поля из PictureBoxов bool row = false; int cellSize = 50; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { PictureBox pic = new PictureBox(); pic.BackColor = row ? Color.BurlyWood : Color.Bisque; pic.Location = new Point(x * (cellSize - 1) + cellSize, y * (cellSize - 1) + cellSize); pic.Size = new Size(cellSize, cellSize); pic.BorderStyle = BorderStyle.FixedSingle; pic.Tag = new Coord(y, x); Draught d = board.GetDraught(new Coord(y, x)); if (d == null) { pic.Image = Properties.Resources.empty; } else if (d.IsWhite) { pic.Image = Properties.Resources.white; } else { pic.Image = Properties.Resources.black; } pic.SizeMode = PictureBoxSizeMode.CenterImage; pic.MouseClick += Pic_MouseClick; this.Controls.Add(pic); box[y, x] = pic; row = !row; } row = !row; } }
//Основная логика игры. Обработка клика на PictureBox`ax private void Pic_MouseClick(object sender, MouseEventArgs e) { if (!IsCurrentMove) { return; } Coord coord = (sender as PictureBox).Tag as Coord; Draught draught = board.GetDraught(coord); Draught test; if (selectedDraugth != null && (test = board.GetDraught(selectedDraugth.Coordinate)) != null) { if (possibleMoves.Where(i => i.X == coord.X && i.Y == coord.Y).FirstOrDefault() != null && IsLegitMove(selectedDraugth.Coordinate, coord)) { foreach (Coord c in possibleKills) { //Логика удаления 'убитой' шашки if (Math.Abs(coord.X - c.X) == 1 && Math.Abs(coord.Y - c.Y) == 1) { box[c.X, c.Y].Image = Properties.Resources.empty; Draught d = board.GetDraught(c); board.DeleteDraught(d); IsKillingTime = true; break; } } possibleKills.Clear(); //Логика перемещения шашек box[selectedDraugth.Coordinate.X, selectedDraugth.Coordinate.Y].Image = Properties.Resources.empty; board.DeleteDraught(selectedDraugth); ClearPossibleMoves(); if (IsPlayWhite) { if (coord.X == 0 || selectedDraugth.IsSpecial) { box[coord.X, coord.Y].Image = Properties.Resources.white_queen; board.AddDraught(new Draught(true, new Coord(coord.X, coord.Y)) { IsSpecial = true }); } else { box[coord.X, coord.Y].Image = Properties.Resources.white; board.AddDraught(new Draught(true, new Coord(coord.X, coord.Y))); } } else { if (coord.X == 7 || selectedDraugth.IsSpecial) { box[coord.X, coord.Y].Image = Properties.Resources.black_queen; board.AddDraught(new Draught(false, new Coord(coord.X, coord.Y)) { IsSpecial = true }); } else { box[coord.X, coord.Y].Image = Properties.Resources.black; board.AddDraught(new Draught(false, new Coord(coord.X, coord.Y))); } } if (IsValidKicks() && IsKillingTime) { DrawPossibleMoves(); return; } IsKillingTime = false; SendMessage(); selectedDraugth = null; return; } } selectedDraugth = draught; if (draught == null || draught.IsWhite != IsPlayWhite) { ClearPossibleMoves(); return; } ClearPossibleMoves(); IsAbleMoves(draught); IsValidKicks(); DrawPossibleMoves(); }
private void IsAbleMoves(Draught d) { if (!d.IsSpecial) { Coord[] coords = new Coord[4] { new Coord(d.Coordinate.X - 1, d.Coordinate.Y - 1), new Coord(d.Coordinate.X - 1, d.Coordinate.Y + 1), new Coord(d.Coordinate.X + 1, d.Coordinate.Y - 1), new Coord(d.Coordinate.X + 1, d.Coordinate.Y + 1) }; for (int i = 0; i < 4; i++) { Draught dr = board.GetDraught(coords[i]); if (!board.CheckRange(coords[i])) { continue; } if (dr != null && dr.IsWhite == IsPlayWhite) { continue; } if (dr == null) { if (IsPlayWhite && coords[i].X < d.Coordinate.X) { possibleMoves.Add(coords[i]); } if (!IsPlayWhite && coords[i].X > d.Coordinate.X) { possibleMoves.Add(coords[i]); } continue; } } } else { int x = d.Coordinate.X - 1; int y = d.Coordinate.Y - 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null) { break; } else if (dr == null) { possibleMoves.Add(new Coord(x, y)); } x--; y--; } x = d.Coordinate.X + 1; y = d.Coordinate.Y + 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null) { break; } else if (dr == null) { possibleMoves.Add(new Coord(x, y)); } x++; y++; } x = d.Coordinate.X - 1; y = d.Coordinate.Y + 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null) { break; } else if (dr == null) { possibleMoves.Add(new Coord(x, y)); } x--; y++; } x = d.Coordinate.X + 1; y = d.Coordinate.Y - 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null) { break; } else if (dr == null) { possibleMoves.Add(new Coord(x, y)); } x++; y--; } } }
//Поиск возможных ударов. Если есть возможность забрать шашку противника - остальные ходы не допускаются private bool IsValidKicks() { possibleKills.Clear(); List <Draught> draughts = board.Draughts.Where(i => i.IsWhite == IsPlayWhite).ToList(); List <Coord> temp = new List <Coord>(); foreach (Draught d in draughts) { if (!d.IsSpecial) { Coord[] coords = new Coord[4] { new Coord(d.Coordinate.X - 1, d.Coordinate.Y - 1), new Coord(d.Coordinate.X - 1, d.Coordinate.Y + 1), new Coord(d.Coordinate.X + 1, d.Coordinate.Y - 1), new Coord(d.Coordinate.X + 1, d.Coordinate.Y + 1) }; foreach (Coord c in coords) { Draught dr = board.GetDraught(c); if (dr == null) { continue; } else if (!board.CheckRange(c)) { continue; } else if (dr.IsWhite == IsPlayWhite) { continue; } else { if (d.Coordinate.X - c.X > 0 && d.Coordinate.Y - c.Y > 0) { Draught draught = board.GetDraught(new Coord(d.Coordinate.X - 2, d.Coordinate.Y - 2)); if (draught == null) { if (board.CheckRange(new Coord(d.Coordinate.X - 2, d.Coordinate.Y - 2))) { temp.Add(new Coord(d.Coordinate.X - 2, d.Coordinate.Y - 2)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); } } } else if (d.Coordinate.X - c.X > 0 && d.Coordinate.Y - c.Y < 0) { Draught draught = board.GetDraught(new Coord(d.Coordinate.X - 2, d.Coordinate.Y + 2)); if (draught == null) { if (board.CheckRange(new Coord(d.Coordinate.X - 2, d.Coordinate.Y + 2))) { temp.Add(new Coord(d.Coordinate.X - 2, d.Coordinate.Y + 2)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); } } } else if (d.Coordinate.X - c.X < 0 && d.Coordinate.Y - c.Y > 0) { Draught draught = board.GetDraught(new Coord(d.Coordinate.X + 2, d.Coordinate.Y - 2)); if (draught == null) { if (board.CheckRange(new Coord(d.Coordinate.X + 2, d.Coordinate.Y - 2))) { temp.Add(new Coord(d.Coordinate.X + 2, d.Coordinate.Y - 2)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); } } } else if (d.Coordinate.X - c.X < 0 && d.Coordinate.Y - c.Y < 0) { Draught draught = board.GetDraught(new Coord(d.Coordinate.X + 2, d.Coordinate.Y + 2)); if (draught == null) { if (board.CheckRange(new Coord(d.Coordinate.X + 2, d.Coordinate.Y + 2))) { temp.Add(new Coord(d.Coordinate.X + 2, d.Coordinate.Y + 2)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); } } } } } } else { int x = d.Coordinate.X - 1; int y = d.Coordinate.Y - 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null && dr.IsWhite == IsPlayWhite) { break; } else if (dr != null && dr.IsWhite != IsPlayWhite) { if (d.Coordinate.X - x > 0 && d.Coordinate.Y - y > 0) { Draught draught = board.GetDraught(new Coord(x - 1, y - 1)); if (draught == null) { if (board.CheckRange(new Coord(x - 1, y - 1))) { temp.Add(new Coord(x - 1, y - 1)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); break; } } } } x--; y--; } x = d.Coordinate.X + 1; y = d.Coordinate.Y + 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null && dr.IsWhite == IsPlayWhite) { break; } else if (dr != null && dr.IsWhite != IsPlayWhite) { if (d.Coordinate.X - x < 0 && d.Coordinate.Y - y < 0) { Draught draught = board.GetDraught(new Coord(x + 1, y + 1)); if (draught == null) { if (board.CheckRange(new Coord(x + 1, y + 1))) { temp.Add(new Coord(x + 1, y + 1)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); break; } } } } x++; y++; } x = d.Coordinate.X - 1; y = d.Coordinate.Y + 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null && dr.IsWhite == IsPlayWhite) { break; } else if (dr != null && dr.IsWhite != IsPlayWhite) { if (d.Coordinate.X - x > 0 && d.Coordinate.Y - y < 0) { Draught draught = board.GetDraught(new Coord(x - 1, y + 1)); if (draught == null) { if (board.CheckRange(new Coord(x - 1, y + 1))) { temp.Add(new Coord(x - 1, y + 1)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); break; } } } } x--; y++; } x = d.Coordinate.X + 1; y = d.Coordinate.Y - 1; while (board.CheckRange(new Coord(x, y))) { Draught dr = board.GetDraught(new Coord(x, y)); if (dr != null && dr.IsWhite == IsPlayWhite) { break; } else if (dr != null && dr.IsWhite != IsPlayWhite) { if (d.Coordinate.X - x < 0 && d.Coordinate.Y - y > 0) { Draught draught = board.GetDraught(new Coord(x + 1, y - 1)); if (draught == null) { if (board.CheckRange(new Coord(x + 1, y - 1))) { temp.Add(new Coord(x + 1, y - 1)); possibleKills.Add(new Coord(dr.Coordinate.X, dr.Coordinate.Y)); break; } } } } x++; y--; } } } if (temp.Count > 0) { possibleMoves = temp; return(true); } return(false); }
public void AddDraught(Draught d) { draughts.Add(d); }
public void DeleteDraught(Draught d) { int index = draughts.FindIndex(i => i.Coordinate.X == d.Coordinate.X && i.Coordinate.Y == d.Coordinate.Y); draughts.RemoveAt(index); }