public void Remove(figure fig) { int clrnum = 0; if (!fig.color) { clrnum = 1; } fig.Height = 80; fig.Width = 80; if (fig.type == "pawn" || (string)fig.other == "pawn") { if (pawnY[clrnum] == 0) { pawnY[clrnum] = 1; } else { pawnY[clrnum] = 0; pawnX[clrnum]++; } pawnGR[clrnum].Children.Remove(fig); } else if (fig.type == "queen") { GR[clrnum].Children.Remove(fig); } else { if (Grid.GetRow(fig) == 0) { types[clrnum, Grid.GetColumn(fig)] = null; } GR[clrnum].Children.Remove(fig); } }
public void Img_MouseDown(object sender, MouseButtonEventArgs e) { if ((GameLogic.blockMode == 1 && ((figure)sender).color) || (GameLogic.blockMode == 2 && !((figure)sender).color)) { figure fig = (figure)sender; DragDrop.DoDragDrop(fig, fig, DragDropEffects.Move); } }
static public figure[] pawnPrep() { figure[] pawn = new figure[8]; for (int i = 0; i < 8; i++) { pawn[i] = new figure(imgs.pawn, "pawn", false); } return(pawn); }
public void Add(figure fig) { int clrnum = 0; if (!fig.color) { clrnum = 1; } Type t = null; if (fig.other != null) { t = fig.other.GetType(); } if (fig.type == "pawn" || (t != null && t.Equals(typeof(string)) && (string)fig.other == "pawn")) { fig.Height = 10; fig.Width = 10; pawnGR[clrnum].Children.Add(fig); Grid.SetRow(fig, pawnY[clrnum]); Grid.SetColumn(fig, pawnX[clrnum]); if (pawnY[clrnum] == 1) { pawnY[clrnum] = 0; } else { pawnY[clrnum] = 1; pawnX[clrnum]--; } } else if (fig.type == "queen") { fig.Height = 20; fig.Width = 20; GR[clrnum].Children.Add(fig); Grid.SetColumn(fig, 4); Grid.SetRow(fig, 0); } else { fig.Height = 20; fig.Width = 20; for (int i = 0; i < 3; i++) { if (types[clrnum, i] == null) { types[clrnum, i] = fig.type; GR[clrnum].Children.Add(fig); Grid.SetColumn(fig, i); Grid.SetRow(fig, 0); break; } if (types[clrnum, i] == fig.type) { GR[clrnum].Children.Add(fig); Grid.SetColumn(fig, i); Grid.SetRow(fig, 1); break; } } } }
public void Drop(object sender, DragEventArgs e) { e.Data.GetFormats(); int[] coord = new int[] { Grid.GetColumn((UIElement)sender), Grid.GetRow((UIElement)sender) }; figure fig = (figure)e.Data.GetData("Шахматы.figure"); int[] oldcoord = chess.getPos(fig, grid); int[] move = makeMove(fig, coord); if (move[0] != -1) { if (handler != null) { Thread th = new Thread(new ParameterizedThreadStart(chatSend)); th.IsBackground = true; if (inverted) { oldcoord[1] = chess.ArrayInv[oldcoord[1]]; coord[1] = chess.ArrayInv[coord[1]]; } if (move[1] != -1) { th.Start("s" + oldcoord[0] + "" + oldcoord[1] + "" + coord[0] + "" + coord[1] + "" + move[1]); } else { th.Start("s" + oldcoord[0] + "" + oldcoord[1] + "" + coord[0] + "" + coord[1]); } blockMode = 0; } else { if (!fig.color) { blockMode = 1; if (move[0] == 0) { textLog.Text = "Ход белых"; } } else { blockMode = 2; if (move[0] == 0) { textLog.Text = "Ход черных"; } } } } //Image img = (Image)e.Data.GetData(forma); //Grid.SetColumn(img, Grid.GetColumn(rect)); //Grid.SetRow(img, Grid.GetRow(rect)); }
static public void setPos(figure Figure, int[] coord, Grid gr, bool posChange = true, bool other = true) { if (posChange) { int[] oldCoord = getPos(Figure, gr); GameLogic.gl.figPosAr[oldCoord[0], oldCoord[1]] = null; if (other) { sp.Play(); } } else { if (other) { gr.Children.Add(Figure); } } Grid.SetColumn(Figure, coord[0]); Grid.SetRow(Figure, coord[1]); GameLogic.gl.figPosAr[coord[0], coord[1]] = Figure; }
int[] makeMove(figure fig, int[] coord, bool fast = false) { int[] returnNum = { -1, -1 }; int[] oldcoord; figure oldfig = figPosAr[coord[0], coord[1]]; if (!fast) { oldcoord = chess.getPos(fig, grid); bool ok = false; foreach (MoveList s in moveList) { if (fig == s.Figure) { for (int i = 0; i < s.X.Count(); i++) { if (s.X[i] == coord[0] && s.Y[i] == coord[1]) { chess.setPos(s.Figure, coord, grid); ok = true; break; } } break; } } if (!ok) { return(returnNum); } if (fig.type == "pawn" && ((fig.color && coord[1] == 1) || (!fig.color && coord[1] == 8))) { ChooseChessWin ccw = new ChooseChessWin(fig.color); ccw.Owner = thisWin; ccw.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; ccw.ShowDialog(); chess.PawnTrans(fig, ccw.numSelect); returnNum[1] = ccw.numSelect; } } else { oldcoord = new int[] { coord[2], coord[3] }; chess.setPos(fig, coord, grid); } StrokeData sd = new StrokeData(); sd.oldOther = fig.other; if (fig.type == "king") { if (!(bool)fig.other) { fig.other = true; if (coord[0] == 3) { sd.CustomDeath = true; if (fig.color) { int pos = 8; if (inverted) { pos = chess.ArrayInv[pos]; } int[] CastlePos = chess.getPos(chess.white.castle[0], grid); if (inverted) { CastlePos[1] = chess.ArrayInv[CastlePos[1]]; } sd.DeathX = CastlePos[0]; sd.DeathY = CastlePos[1]; chess.setPos(chess.white.castle[0], new int[] { 4, pos }, grid, true, false); sd.FigureDeath = chess.white.castle[0]; } else { int pos = 1; if (inverted) { pos = chess.ArrayInv[pos]; } int[] CastlePos = chess.getPos(chess.black.castle[0], grid); if (inverted) { CastlePos[1] = chess.ArrayInv[CastlePos[1]]; } sd.DeathX = CastlePos[0]; sd.DeathY = CastlePos[1]; chess.setPos(chess.black.castle[0], new int[] { 4, pos }, grid, true, false); sd.FigureDeath = chess.black.castle[0]; } } else if (coord[0] == 7) { sd.CustomDeath = true; if (fig.color) { int pos = 8; if (inverted) { pos = chess.ArrayInv[pos]; } int[] CastlePos = chess.getPos(chess.white.castle[1], grid); if (inverted) { CastlePos[1] = chess.ArrayInv[CastlePos[1]]; } sd.DeathX = CastlePos[0]; sd.DeathY = CastlePos[1]; chess.setPos(chess.white.castle[1], new int[] { 6, pos }, grid, true, false); sd.FigureDeath = chess.white.castle[1]; } else { int pos = 1; if (inverted) { pos = chess.ArrayInv[pos]; } int[] CastlePos = chess.getPos(chess.black.castle[1], grid); if (inverted) { CastlePos[1] = chess.ArrayInv[CastlePos[1]]; } sd.DeathX = CastlePos[0]; sd.DeathY = CastlePos[1]; chess.setPos(chess.black.castle[1], new int[] { 6, pos }, grid, true, false); sd.FigureDeath = chess.black.castle[1]; } } } } if ((fig.color && (fig == chess.white.castle[0] || fig == chess.white.castle[1])) || (!fig.color && (fig == chess.black.castle[0] || fig == chess.black.castle[1])) && !(bool)fig.other) { fig.other = true; } if (inverted) { sd.oldY = chess.ArrayInv[oldcoord[1]]; sd.Y = chess.ArrayInv[coord[1]]; } else { sd.oldY = oldcoord[1]; sd.Y = coord[1]; } sd.X = coord[0]; sd.oldX = oldcoord[0]; sd.Figure = fig; if (oldfig != null) { //figlist.Remove((figure)sender); grid.Children.Remove(oldfig); dg.Add(oldfig); sd.FigureDeath = oldfig; sd.DeathX = coord[0]; if (inverted) { sd.DeathY = chess.ArrayInv[coord[1]]; } else { sd.DeathY = coord[1]; } figLiveList.Remove(oldfig); } else if (fig.type == "pawn") { int z = -1; if (fig.color) { z = 1; } if (StrokeHis.Count != 0 && StrokeHis[StrokeHis.Count - 1].Figure == figPosAr[coord[0], coord[1] + z] && StrokeHis[StrokeHis.Count - 1].Figure.type == "pawn") { grid.Children.Remove(figPosAr[coord[0], coord[1] - 1]); dg.Add(figPosAr[coord[0], coord[1] - 1]); sd.FigureDeath = figPosAr[coord[0], coord[1] - 1]; sd.DeathX = coord[0]; if (inverted) { sd.DeathY = chess.ArrayInv[coord[1] + z]; } else { sd.DeathY = coord[1] + z; } figLiveList.Remove(figPosAr[coord[0], coord[1] - 1]); } } StrokeHis.Add(sd); bool check = false; IEnumerable <figure> figColorList = figLiveList.Where(figure => figure.color == fig.color); if (fig.color) { foreach (figure s in figColorList) { if (moves.checkMoveToCheck(s, chess.getPos(chess.black.king, grid), chess.getPos(s, grid))) { check = true; if (handler == null) { textLog.Text = "Чёрным объявлен шах"; } else { textLog.Text = "Вам объявлен\nшах"; } break; } } } else { foreach (figure s in figColorList) { if (moves.checkMoveToCheck(s, chess.getPos(chess.white.king, grid), chess.getPos(s, grid))) { check = true; if (handler == null) { textLog.Text = "Белым объявлен\nшах"; } else { textLog.Text = "Вам объявлен\nшах"; } break; } } } returnNum[0] = 0; if (handler == null || fast) { moves.refreshMoveList(fig.color); } if (moveList.Count == 0) { if (check) { textLog.Text = "МАТ"; } else { textLog.Text = "ПАТ"; } returnNum[0] = 1; } if (check) { returnNum[0] = 1; } return(returnNum); }
static public void PawnTrans(figure fig, int num) { switch (num) { case 0: { fig.type = "knight"; if (fig.color) { fig.Source = white.imgs.knight; } else { fig.Source = black.imgs.knight; } break; } case 1: { fig.type = "queen"; if (fig.color) { fig.Source = white.imgs.queen; } else { fig.Source = black.imgs.queen; } break; } case 2: { fig.type = "castle"; if (fig.color) { fig.Source = chess.white.imgs.castle; } else { fig.Source = chess.black.imgs.castle; } break; } case 3: { fig.type = "bishop"; if (fig.color) { fig.Source = chess.white.imgs.bishop; } else { fig.Source = chess.black.imgs.bishop; } break; } } fig.other = "pawn"; }
public bool checkMoveToCheck(figure fig, int[] coord, int[] position) { if (GL.figPosAr[coord[0], coord[1]].color == fig.color) { return(false); } switch (fig.type) { case "pawn": switch (fig.color) { case true: { if (((coord[0] == position[0] - 1 || coord[0] == position[0] + 1) && coord[1] == position[1] - 1 && GL.figPosAr[coord[0], coord[1]] != null)) { return(true); } //if (((coord[0] == position[0] - 1 || coord[0] == position[0] + 1) && coord[1] == position[1] - 1 // && GL.figPosAr[coord[0], coord[1]] != null && !GL.figPosAr[coord[0], coord[1]].color) // || (coord[0] == position[0] && GL.figPosAr[coord[0], coord[1]] == null && coord[1] == position[1] - 1) || (position[1] == 7 && coord[1] == position[1] - 2)) ok = true; break; } case false: { if (((coord[0] == position[0] + 1 || coord[0] == position[0] - 1) && coord[1] == position[1] + 1 && GL.figPosAr[coord[0], coord[1]] != null)) { return(true); } break; } } break; case "bishop": { if ((coord[1] + coord[0] - position[0] != position[1] && coord[1] - coord[0] + position[0] != position[1])) { return(false); } int i = position[0]; if (coord[0] > position[0]) { if (coord[1] > position[1]) { for (int j = position[1]; j < 9; j++, i++) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } else { for (int j = position[1]; j > 0; j--, i++) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } } else { if (coord[1] > position[1]) { for (int j = position[1]; j < 9; j++, i--) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } else { for (int j = position[1]; j > 0; j--, i--) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } } break; } case "knight": { if ((((coord[0] == position[0] + 1 || coord[0] == position[0] - 1) && (coord[1] == position[1] + 2 || coord[1] == position[1] - 2)) || ((coord[0] == position[0] + 2 || coord[0] == position[0] - 2) && (coord[1] == position[1] + 1 || coord[1] == position[1] - 1))) ) { return(true); } break; } case "castle": { if ((coord[1] != position[1] && coord[0] != position[0])) { return(false); } if (coord[0] > position[0]) { for (int j = position[0]; j < 9; j++) { if (coord[0] == j) { return(true); } if (GL.figPosAr[j, position[1]] != null && GL.figPosAr[j, position[1]] != fig) { return(false); } } } else if (coord[0] < position[0]) { for (int j = position[0]; j > 0; j--) { if (coord[0] == j) { return(true); } if (GL.figPosAr[j, position[1]] != null && GL.figPosAr[j, position[1]] != fig) { return(false); } } } else if (coord[1] > position[1]) { for (int j = position[1]; j < 9; j++) { if (coord[1] == j) { return(true); } if (GL.figPosAr[position[0], j] != null && GL.figPosAr[position[0], j] != fig) { return(false); } } } //if(coord[1] < position[1]) else { for (int j = position[1]; j > 0; j--) { if (coord[1] == j) { return(true); } if (GL.figPosAr[position[0], j] != null && GL.figPosAr[position[0], j] != fig) { return(false); } } } break; } case "queen": { if ((coord[1] != position[1] && coord[0] != position[0]) && (coord[1] + coord[0] - position[0] != position[1] && coord[1] - coord[0] + position[0] != position[1])) { return(false); } int i = position[0]; if (coord[0] > position[0]) { if (coord[1] > position[1]) { for (int j = position[1]; j < 9; j++, i++) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } else if (coord[1] < position[1]) { for (int j = position[1]; j > 0; j--, i++) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } else { for (int j = position[0]; j < 9; j++) { if (coord[0] == j) { return(true); } if (GL.figPosAr[j, position[1]] != null && GL.figPosAr[j, position[1]] != fig) { return(false); } } } } else if (coord[0] < position[0]) { if (coord[1] > position[1]) { for (int j = position[1]; j < 9; j++, i--) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } else if (coord[1] < position[1]) { for (int j = position[1]; j > 0; j--, i--) { if (coord[0] == i && coord[1] == j) { return(true); } if (GL.figPosAr[i, j] != null && GL.figPosAr[i, j] != fig) { return(false); } } } else { for (int j = position[0]; j > 0; j--) { if (coord[0] == j) { return(true); } if (GL.figPosAr[j, position[1]] != null && GL.figPosAr[j, position[1]] != fig) { return(false); } } } } else if (coord[1] > position[1]) { for (int j = position[1]; j < 9; j++) { if (coord[1] == j) { return(true); } if (GL.figPosAr[position[0], j] != null && GL.figPosAr[position[0], j] != fig) { return(false); } } } //if(coord[1] < position[1]) else { for (int j = position[1]; j > 0; j--) { if (coord[1] == j) { return(true); } if (GL.figPosAr[position[0], j] != null && GL.figPosAr[position[0], j] != fig) { return(false); } } } break; } case "king": { if (-2 < coord[0] - position[0] && coord[0] - position[0] < 2 && -2 < coord[1] - position[1] && coord[1] - position[1] < 2) { return(true); } break; } } return(false); }
public void refreshMoveList(bool clr) { GL.moveList = new List <MoveList>(); GL.moveList = getMoveList(!clr); if (clr) { foreach (MoveList s in GL.moveList) { GL.figPosAr[s.oldX, s.oldY] = null; for (int i = 0; i < s.X.Count(); i++) { figure tempFigure = GL.figPosAr[s.X[i], s.Y[i]]; if (tempFigure != null) { GL.figLiveList.Remove(tempFigure); } GL.figPosAr[s.X[i], s.Y[i]] = s.Figure; bool ok = true; IEnumerable <figure> figColorList = GL.figLiveList.Where(figure => figure.color); foreach (figure k in figColorList) { int[] sc = { s.X[i], s.Y[i] }; if (true /*k != s.Figure*/) { sc = chess.getPos(k, GL.grid); } int[] kingPos = chess.getPos(chess.black.king, GL.grid); if (s.Figure.type == "king") { kingPos[0] = s.X[i]; kingPos[1] = s.Y[i]; } if (checkMoveToCheck(k, kingPos, sc)) // Ходы результат которых приведет к шаху // Ходы результат которых приведет к шаху { if (tempFigure != null) { GL.figLiveList.Add(tempFigure); } GL.figPosAr[s.X[i], s.Y[i]] = tempFigure; s.X.RemoveAt(i); s.Y.RemoveAt(i); i--; ok = false; break; } } if (ok) { GL.figPosAr[s.X[i], s.Y[i]] = tempFigure; if (tempFigure != null) { GL.figLiveList.Add(tempFigure); } } } GL.figPosAr[s.oldX, s.oldY] = s.Figure; } } else { foreach (MoveList s in GL.moveList) { GL.figPosAr[s.oldX, s.oldY] = null; for (int i = 0; i < s.X.Count(); i++) { figure tempFigure = GL.figPosAr[s.X[i], s.Y[i]]; if (tempFigure != null) { GL.figLiveList.Remove(tempFigure); } GL.figPosAr[s.X[i], s.Y[i]] = s.Figure; bool ok = true; IEnumerable <figure> figColorList = GL.figLiveList.Where(figure => !figure.color); foreach (figure k in figColorList) { int[] sc = { s.X[i], s.Y[i] }; if ((true /*k != s.Figure*/)) { sc = chess.getPos(k, GL.grid); } int[] kingPos = chess.getPos(chess.white.king, GL.grid); if (s.Figure.type == "king") { kingPos[0] = s.X[i]; kingPos[1] = s.Y[i]; } if (checkMoveToCheck(k, kingPos, sc)) // Ходы результат которых приведет к шаху { if (tempFigure != null) { GL.figLiveList.Add(tempFigure); } GL.figPosAr[s.X[i], s.Y[i]] = tempFigure; s.X.RemoveAt(i); s.Y.RemoveAt(i); i--; ok = false; break; } } if (ok) { GL.figPosAr[s.X[i], s.Y[i]] = tempFigure; if (tempFigure != null) { GL.figLiveList.Add(tempFigure); } } } GL.figPosAr[s.oldX, s.oldY] = s.Figure; } } for (int i = 0; i < GL.moveList.Count; i++) { if (GL.moveList[i].X.Count == 0) { GL.moveList.RemoveAt(i); i--; } } }