public bool checkCheck(BoardGen board) { bool check = false; Piece piece = null; //Need to check all pieces on board because discovered check exists foreach (Piece y in board.getPieces()) { List <Panel> possibleMoves = new List <Panel>(); possibleMoves = y.getMoves(); if (y.getMoves() != null) { foreach (Panel x in possibleMoves) { foreach (Piece c in board.getPieces()) { if (c.getPanel() == x) { piece = c; if (piece.getType().Contains("King") && (y.getType().Substring(0, 1) != piece.getType().Substring(0, 1))) { typeOfCheck = (piece.getType().Substring(0, 1) == "W") ? "W" : "B"; return(true); } } } } } //Checks if any piece has a possible move that contains "King", //if it does it is check } return(check); }
public override void setMoves(BoardGen board, bool checkUp, bool suicide) { possibleMoves = new List <Panel>(); List <Panel> panelsRemove = new List <Panel>(); Piece piece = null; Panel tempPan = null; capturePossible = false; //A king can move 1 sqaure in any direction. foreach (Panel x in board.getPanels()) { foreach (Piece c in board.getPieces()) { if (c.getPanel() == x) { piece = c; } } if (x.Location.X == getPanel().Location.X) { if (x.Location.Y == getPanel().Location.Y + 40 || x.Location.Y == getPanel().Location.Y - 40) { possibleMoves.Add(x); } } if (x.Location.X == getPanel().Location.X + 40) { if (x.Location.Y == getPanel().Location.Y + 40 || x.Location.Y == getPanel().Location.Y - 40 || x.Location.Y == getPanel().Location.Y) { possibleMoves.Add(x); } } if (x.Location.X == getPanel().Location.X - 40) { if (x.Location.Y == getPanel().Location.Y + 40 || x.Location.Y == getPanel().Location.Y - 40 || x.Location.Y == getPanel().Location.Y) { possibleMoves.Add(x); } } /*Similarly to other pieces this section gets all moves that would * be possible for the king on an empty board*/ if (piece != null) { if (piece.getType().Substring(0, 1) == getType().Substring(0, 1) && board.getPanelsInUse().Contains(x)) { panelsRemove.Add(x); } else { } } /*And this just adds all the blocked sqaures to a list of not * possible moves*/ } foreach (Panel x in panelsRemove) { possibleMoves.Remove(x); } bool castle1 = false; bool castle2 = false; if (!getMoved()) { if (!board.getPanelsInUse().Contains(board.getPanels()[5, getType().Substring(0, 1) == "W" ? 7 : 0]) && !board.getPanelsInUse().Contains(board.getPanels()[6, getType().Substring(0, 1) == "W" ? 7 : 0])) { possibleMoves.Add(board.getPanels()[6, getType().Substring(0, 1) == "W" ? 7 : 0]); castle1 = true; } if (!board.getPanelsInUse().Contains(board.getPanels()[1, getType().Substring(0, 1) == "W" ? 7 : 0]) && !board.getPanelsInUse().Contains(board.getPanels()[2, getType().Substring(0, 1) == "W" ? 7 : 0]) && !board.getPanelsInUse().Contains(board.getPanels()[3, getType().Substring(0, 1) == "W" ? 7 : 0])) { possibleMoves.Add(board.getPanels()[2, getType().Substring(0, 1) == "W" ? 7 : 0]); castle2 = true; } } if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess) { Piece toRem = null; bool removed = false; tempPan = getPanel(); foreach (Panel x in possibleMoves) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece z in board.getPieces().ToList()) { if (z.getPanel() == x) { toRem = z; removed = true; board.RemovePiece(z); } } } setPanel(x); board.setPanelsInUse(); foreach (Piece y in board.getPieces().ToList()) { if (!y.getType().Contains(getType())) { y.setMoves(board, false, suicide); } } if (checkCheck(board)) { if (castle1 && x == board.getPanels()[5, getType().Substring(0, 1) == "W" ? 7 : 0]) { panelsRemove.Add(board.getPanels()[6, getType().Substring(0, 1) == "W" ? 7 : 0]); } if (castle2 && x == board.getPanels()[3, getType().Substring(0, 1) == "W" ? 7 : 0]) { panelsRemove.Add(board.getPanels()[2, getType().Substring(0, 1) == "W" ? 7 : 0]); } panelsRemove.Add(x); } setPanel(tempPan); if (removed) { board.AddPiece(toRem); removed = false; } board.setPanelsInUse(); } foreach (Panel x in panelsRemove) { possibleMoves.Remove(x); } foreach (Panel x in possibleMoves) { foreach (Piece p in board.getPieces()) { if (x == p.getPanel()) { capturePossible = true; } } } } /* Exactly like all other pieces, this part checks all of the current * possible moves for the queen to see if any would leave the player * in check, if it would, it is not a valid move and is removed. */ if (capturePossible && suicide) //if it is suicide chess and there is a capture possible { List <Panel> newPossibleMoves = new List <Panel>(); foreach (Panel x in possibleMoves) { foreach (Piece y in board.getPieces()) { if (x == y.getPanel()) { newPossibleMoves.Add(x); } } } possibleMoves = newPossibleMoves; //captures are the only possible moves if a capture is possible } else if (!capturePossible && suicide) //if there no captures possible in suicide chess { foreach (Piece x in board.getPieces()) { if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1)) { possibleMoves = new List <Panel>(); break; //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move } } } }
private void button4_Click(object sender, EventArgs e) { bool taken = false; try { for (int i = 0; i < undos[undos.Count - 1]; i++) { movingPiece = undoPiece[undoPiece.Count - (i + 1)]; if (!generation.getPieces().Contains(movingPiece)) { movingPiece.getPanel().BackgroundImage = movingPiece.getType().Contains("W") ? pictureBoxesB[bPiecesTaken - 1].BackgroundImage : pictureBoxesW[wPiecesTaken - 1].BackgroundImage; generation.AddPiece(movingPiece); if (movingPiece.getType().Contains("W")) { pictureBoxesB[bPiecesTaken - 1].BackgroundImage = null; bPiecesTaken--; } else { pictureBoxesB[wPiecesTaken - 1].BackgroundImage = null; wPiecesTaken--; } //If there was a piece taken before the undo, remove it from the GUI taken = true; } if (movingPiece == updatePiece) { piecePosRemove.getPanel().BackgroundImage = null; piecePosRemove.setPanel(null); generation.RemovePiece(piecePosRemove); toUpdate = null; updatePiece = null; movingPiece.getPanel().BackgroundImage = movingPiece.getType().Contains("W") ? Resources.WPawn : Resources.BPawn; //Prepare the sqaure the piece needs to return to if necesary } movingPiecePanel = undoPiece[undoPiece.Count - (i + 1)].getPanel(); actionPanel = undoPanel[undoPanel.Count - (i + 1)]; actionPanel.BackgroundImage = movingPiece.getPanel().BackgroundImage; movingPiece.setPanel(actionPanel); generation.setPanelsInUse(); movingPiecePanel.BackgroundImage = null; movingPiece.decreaseNoMoves(); if (movingPiece.numberMoves() <= 0) { movingPiece.setMoved(false); } //return everything to the way it was before the move occured } for (int x = 0; x < undos[undos.Count - 1]; x++) { undoPiece.Remove(undoPiece[undoPiece.Count - 1]); undoPanel.Remove(undoPanel[undoPanel.Count - 1]); } undos.Remove(undos[undos.Count - 1]); if (updateReq || taken) { panel_MouseDown(null, new MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)); updateReq = false; } //update the undos so that it wont be the same undo over and over again } catch (System.ArgumentOutOfRangeException) { MessageBox.Show("You can't go back any further."); //if there aren't any undos in the list, you can't go back more } noUndos = 0; }
public override void setMoves(BoardGen board, bool checkUp, bool suicide) { Piece piece = null; Panel tempPan = null; possibleMoves = new List <Panel>(); List <Panel> panelsRemove = new List <Panel>(); capturePossible = false; bool noMoves = false; /*White's pawns can move up the board, Black's move down. If it is * the first move then a pawn can move two squares, otherwise only * one. Pawns also take other pieces diagonaly.*/ foreach (Panel x in board.getPanels()) { foreach (Piece c in board.getPieces()) { if (c.getPanel() == x) { piece = c; } } if (piece != null) { if (getType().Substring(0, 1).Equals("W")) { if (board.getPanelsInUse().Contains(x) && (x.Location.Y == getPanel().Location.Y - 40) && (x.Location.X == getPanel().Location.X)) { noMoves = true; foreach (Panel z in board.getPanels()) { if ((((getPanel().Location.Y == z.Location.Y + 40) && (z.Location.X == getPanel().Location.X - 40)) || (z.Location.X == getPanel().Location.X + 40))) { noMoves = false; } } } else if (!(board.getPanelsInUse().Contains(x)) && (getPanel().Location.Y == 240) && ((x.Location.Y == getPanel().Location.Y - 40) || (x.Location.Y == getPanel().Location.Y - 80)) && (x.Location.X == getPanel().Location.X)) { possibleMoves.Add(x); } else if (!(board.getPanelsInUse().Contains(x)) && (x.Location.Y == getPanel().Location.Y - 40) && (x.Location.X == getPanel().Location.X)) { possibleMoves.Add(x); } else if (board.getPanelsInUse().Contains(x) && (getType().Substring(0, 1) != piece.getType().Substring(0, 1))) { if ((getPanel().Location.Y == x.Location.Y + 40 && (x.Location.X == getPanel().Location.X - 40 || x.Location.X == getPanel().Location.X + 40))) { possibleMoves.Add(x); } } } else if (getType().Substring(0, 1).Equals("B")) { if (board.getPanelsInUse().Contains(x) && (x.Location.Y == getPanel().Location.Y + 40) && (x.Location.X == getPanel().Location.X)) { noMoves = true; foreach (Panel z in board.getPanels()) { if ((((getPanel().Location.Y == z.Location.Y - 40) && (z.Location.X == getPanel().Location.X - 40)) || (z.Location.X == getPanel().Location.X + 40))) { noMoves = false; } } } else if (!(board.getPanelsInUse().Contains(x)) && (getPanel().Location.Y == 40) && ((x.Location.Y == getPanel().Location.Y + 40) || (x.Location.Y == getPanel().Location.Y + 80)) && (x.Location.X == getPanel().Location.X)) { possibleMoves.Add(x); } else if (!(board.getPanelsInUse().Contains(x)) && (x.Location.Y == getPanel().Location.Y + 40) && (x.Location.X == getPanel().Location.X)) { possibleMoves.Add(x); } else if (board.getPanelsInUse().Contains(x) && (getType().Substring(0, 1) != piece.getType().Substring(0, 1))) { if ((getPanel().Location.Y == x.Location.Y - 40 && (x.Location.X == getPanel().Location.X - 40 || x.Location.X == getPanel().Location.X + 40))) { possibleMoves.Add(x); } } } } } /*This section adds all the moves the pawn could make if the board were * empty to a list called possibleMoves and then all the ones that are * made invalid due to other pieces are added to the list called * panelsRemove*/ foreach (Panel x in panelsRemove) { possibleMoves.Remove(x); } foreach (Panel x in possibleMoves) { foreach (Piece p in board.getPieces()) { if (x == p.getPanel()) { capturePossible = true; } } } if (checkUp && !suicide) //if checking for check and this is not suicide chess (as chess doesn't exist in suicide chess) { Piece toRem = null; bool removed = false; tempPan = getPanel(); foreach (Panel x in possibleMoves) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece z in board.getPieces().ToList()) { if (z.getPanel() == x) { toRem = z; removed = true; board.RemovePiece(z); } } } setPanel(x); board.setPanelsInUse(); foreach (Piece y in board.getPieces().ToList()) { if (!y.getType().Contains(getType())) { y.setMoves(board, false, suicide); } } if (checkCheck(board) && checkType() == getType().Substring(0, 1)) { panelsRemove.Add(x); } setPanel(tempPan); if (removed) { board.AddPiece(toRem); removed = false; } board.setPanelsInUse(); } foreach (Panel x in panelsRemove) { possibleMoves.Remove(x); } if (noMoves) { possibleMoves = new List <Panel>(); } } /* Exactly like all other pieces, this part checks all of the current * possible moves for the pawn to see if any would leave the player * in check, if it would, it is not a valid move and is removed. */ if (capturePossible && suicide) //if it is suicide chess and there is a capture possible { List <Panel> newPossibleMoves = new List <Panel>(); foreach (Panel x in possibleMoves) { foreach (Piece y in board.getPieces()) { if (x == y.getPanel()) { newPossibleMoves.Add(x); } } } possibleMoves = newPossibleMoves; //captures are the only possible moves if a capture is possible } else if (!capturePossible && suicide) //if there no captures possible in suicide chess { foreach (Piece x in board.getPieces()) { if (x.capture() && x.getType().Substring(0, 1) == getType().Substring(0, 1)) { possibleMoves = new List <Panel>(); break; //if there are no possible captures, but there are possible captures from other pieces, this piece cannot move } } } }
private void panel_MouseDown(object sender, MouseEventArgs e) { Panel actionPanel = (Panel)sender; bool castle = false; //This section is for when no piece has been clicked yet if (timer1.Enabled) //if the game hasn't ended { if (movingPiece == null) { pieces = generation.getPieces(); //Gets the piece that was clicked foreach (Piece p in pieces) { if (p.getPanel() == actionPanel) { movingPiece = p; movingPiecePanel = actionPanel; break; } } //If the panel that was clicked actually contains a piece try { //And it's that peice's colours turn if ((WhiteMove && movingPiece.getType().Substring(0, 1).Equals("W")) || (!WhiteMove && movingPiece.getType().Substring(0, 1).Equals("B"))) { movingPiece.setMoves(generation, true, suicide); //Get that piece's moves foreach (Panel x in movingPiece.getMoves()) { x.BackColor = ColorTranslator.FromHtml("#FFFC7F"); //And display them } } } catch (NullReferenceException) { //Could notify the player they clicked an empty sqaure here } } //This section is for when a piece has already been clicked else { movingPiece.setMoves(generation, true, suicide); //Update the moves of that piece for their new position //Get the colours of the board back to normal for (int k = 0; k <= 7; k++) { for (int s = 0; s <= 7; s++) { if (k % 2 == 0) { generation.getPanels()[k, s].BackColor = s % 2 != 0 ? ColorTranslator.FromHtml("#80A83E") : ColorTranslator.FromHtml("#D9DD76"); } else { generation.getPanels()[k, s].BackColor = s % 2 != 0 ? ColorTranslator.FromHtml("#D9DD76") : ColorTranslator.FromHtml("#80A83E"); } } } //If that panel clicked was not the same as the first panel the person clicked if (actionPanel != movingPiecePanel && movingPiece.getType().Substring(0, 1) == (WhiteMove ? "W" : "B")) { //And the panel is valid try { if (movingPiece.getMoves().Contains(actionPanel)) { if (generation.getPanelsInUse().Contains(actionPanel)) { foreach (Piece z in generation.getPieces().ToList()) { if (z.getPanel() == actionPanel) { generation.RemovePiece(z); if (z.getType().Substring(0, 1) == "W") { pictureBoxesB[bPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage; bPiecesTaken++; } else { pictureBoxesW[wPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage; wPiecesTaken++; } } } actionPanel.BackgroundImage = null; /*If the panel was a possible move and contains a * piece, the piece that it already contains needs * to be disposed of and stored in the interface where * taken pieces are displayed*/ } if (movingPiece.getType().Contains("King")) { if ((actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0] || actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) && !movingPiece.getMoved()) { castle = true; } } //Recognises when the king is trying to castle if ((WhiteMove && movingPiece.getType().Substring(0, 1).Equals("W")) || (!WhiteMove && movingPiece.getType().Substring(0, 1).Equals("B"))) { actionPanel.BackgroundImage = movingPiece.getPanel().BackgroundImage; undoPiece.Add(movingPiece); undoPanel.Add(movingPiece.getPanel()); noUndos++; movingPiece.setPanel(actionPanel); if (updateReq && movingPiece.getPanel() != updatePiece.getPanel()) { updatePiece.setPanel(toUpdate); generation.RemovePiece(piecePosRemove); updateReq = false; //if a pawn has moved foward two last move, it can no longer be taken as if it were on the first sqaure, so don't allow that to happen. } else if (updateReq) { if (movingPiece.getType().Substring(0, 1) == "W") { pictureBoxesW[wPiecesTaken - 1].BackgroundImage = Resources.BPawn; } else { pictureBoxesB[bPiecesTaken - 1].BackgroundImage = Resources.WPawn; } updatePiece.getPanel().BackgroundImage = movingPiece.getPanel().BackgroundImage; piecePosRemove.getPanel().BackgroundImage = null; generation.RemovePiece(piecePosRemove); updatePiece.setPanel(null); updateReq = false; //If a pawn has moved foward two last move, and a pawn has taken it as if it were on the first square, this allows it to be taken. } if (movingPiece.getType().Contains("Pawn")) { if (!movingPiece.getMoved() && movingPiece.getPanel().Location.Y == movingPiecePanel.Location.Y + ((movingPiece.getType().Contains("W")) ? -80 : 80)) { foreach (Panel x in generation.getPanels()) { if (x.Location.Y + (movingPiece.getType().Contains("W") ? -40 : 40) == actionPanel.Location.Y && x.Location.X == actionPanel.Location.X) { toUpdate = actionPanel; updatePiece = movingPiece; movingPiece.setPanel(x); updateReq = true; piecePosRemove = new Pawn(updatePiece.getType(), actionPanel, true); generation.AddPiece(piecePosRemove); break; } } } } generation.setPanelsInUse(); movingPiecePanel.BackgroundImage = null; movingPiece.setMoved(true); movingPiece.increaseNoMoves(); //As long as the right person (whose turn it is) is //moving then this bit above moves it if (castle) //if the king is attempting to castle { int action = -1; if (actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { foreach (Piece x in generation.getPieces()) { if (x.getPanel() == generation.getPanels()[7, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { movingPiece = x; action = 5; } } } if (actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { foreach (Piece x in generation.getPieces()) { if (x.getPanel() == generation.getPanels()[0, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { movingPiece = x; action = 3; } } } actionPanel = generation.getPanels()[action, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]; movingPiecePanel = movingPiece.getPanel(); actionPanel.BackgroundImage = movingPiece.getPanel().BackgroundImage; undoPiece.Add(movingPiece); undoPanel.Add(movingPiece.getPanel()); noUndos++; movingPiece.setPanel(actionPanel); generation.setPanelsInUse(); movingPiecePanel.BackgroundImage = null; movingPiece.setMoved(true); movingPiece.increaseNoMoves(); //moves the castle to thing other side of the king } if (movingPiece.getType() == "WPawn" ^ movingPiece.getType() == "BPawn") { if ((movingPiece.getPanel().Location.Y == 0 && movingPiece.getType() == "WPawn") ^ (movingPiece.getPanel().Location.Y == 280 && movingPiece.getType() == "BPawn")) { generation.RemovePiece(movingPiece); newQueen = new Queen (movingPiece.getType().Substring(0, 1) + "Queen", movingPiece.getPanel(), true); generation.AddPiece(newQueen); } } //If a pawn reaches the end, it becoms a queen int totalPossibleMoves = 0; foreach (Piece y in generation.getPieces().ToList()) { y.setMoves(generation, true, suicide); if ((WhiteMove && y.getType().Substring(0, 1) == "B") ^ (!WhiteMove && y.getType().Substring(0, 1) == "W")) { totalPossibleMoves += y.getMoves().Count; } } //Make sure there are some possible moves if (!(gameModes == "M3S") && movingPiece.checkCheck(generation)) { MessageBox.Show("Check!"); //Check check, if it is check, tell the players if (totalPossibleMoves == 0) { //There are no possible moves, it's checkemate timer1.Stop(); MessageBox.Show("Check mate! " + (movingPiece.checkType() == "B" ? "White" : "Black") + " wins!"); //EndGame() } } double val = moveCalc.getBoardValue(generation); WhiteMove = !WhiteMove; movingPiece = null; //Next players move and a piece has not been clicked if (gameModes == "S1S") { generation.setPanelsInUse(); actionPanel = moveCalc.bestMove(generation, -10000, 10000); movingPiece = moveCalc.getMovingPiece(); movingPiecePanel = moveCalc.getMovingPiecePanel(); if (generation.getPanelsInUse().Contains(actionPanel)) { foreach (Piece z in generation.getPieces().ToList()) { if (z.getPanel() == actionPanel) { generation.RemovePiece(z); if (z.getType().Substring(0, 1) == "W") { pictureBoxesB[bPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage; bPiecesTaken++; } else { pictureBoxesW[wPiecesTaken].BackgroundImage = z.getPanel().BackgroundImage; wPiecesTaken++; } } } actionPanel.BackgroundImage = null; /*If the panel was a possible move and contains a * piece, the piece that it already contains needs * to be disposed of and stored in the interface where * taken pieces are displayed*/ } if (movingPiece.getType().Contains("King")) { if ((actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0] || actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) && !movingPiece.getMoved()) { castle = true; } } //Recognises when the king is trying to castle if ((WhiteMove && movingPiece.getType().Substring(0, 1).Equals("W")) || (!WhiteMove && movingPiece.getType().Substring(0, 1).Equals("B"))) { actionPanel.BackgroundImage = movingPiece.getPanel().BackgroundImage; undoPiece.Add(movingPiece); undoPanel.Add(movingPiece.getPanel()); noUndos++; movingPiece.setPanel(actionPanel); if (updateReq && movingPiece.getPanel() != updatePiece.getPanel()) { updatePiece.setPanel(toUpdate); generation.RemovePiece(piecePosRemove); updateReq = false; //if a pawn has moved foward two last move, it can no longer be taken as if it were on the first sqaure, so don't allow that to happen. } else if (updateReq) { if (movingPiece.getType().Substring(0, 1) == "W") { pictureBoxesW[wPiecesTaken - 1].BackgroundImage = Resources.BPawn; } else { pictureBoxesB[bPiecesTaken - 1].BackgroundImage = Resources.WPawn; } updatePiece.getPanel().BackgroundImage = movingPiece.getPanel().BackgroundImage; piecePosRemove.getPanel().BackgroundImage = null; generation.RemovePiece(piecePosRemove); updatePiece.setPanel(null); updateReq = false; //If a pawn has moved foward two last move, and a pawn has taken it as if it were on the first square, this allows it to be taken. } if (movingPiece.getType().Contains("Pawn")) { if (!movingPiece.getMoved() && movingPiece.getPanel().Location.Y == movingPiecePanel.Location.Y + ((movingPiece.getType().Contains("W")) ? -80 : 80)) { foreach (Panel x in generation.getPanels()) { if (x.Location.Y + (movingPiece.getType().Contains("W") ? -40 : 40) == actionPanel.Location.Y && x.Location.X == actionPanel.Location.X) { toUpdate = actionPanel; updatePiece = movingPiece; movingPiece.setPanel(x); updateReq = true; piecePosRemove = new Pawn(updatePiece.getType(), actionPanel, true); generation.AddPiece(piecePosRemove); break; } } } } generation.setPanelsInUse(); movingPiecePanel.BackgroundImage = null; movingPiece.setMoved(true); movingPiece.increaseNoMoves(); //As long as the right person (whose turn it is) is //moving then this bit above moves it if (castle) //if the king is attempting to castle { int action = -1; if (actionPanel == generation.getPanels()[6, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { foreach (Piece x in generation.getPieces()) { if (x.getPanel() == generation.getPanels()[7, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { movingPiece = x; action = 5; } } } if (actionPanel == generation.getPanels()[2, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { foreach (Piece x in generation.getPieces()) { if (x.getPanel() == generation.getPanels()[0, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]) { movingPiece = x; action = 3; } } } actionPanel = generation.getPanels()[action, movingPiece.getType().Substring(0, 1) == "W" ? 7 : 0]; movingPiecePanel = movingPiece.getPanel(); actionPanel.BackgroundImage = movingPiece.getPanel().BackgroundImage; undoPiece.Add(movingPiece); undoPanel.Add(movingPiece.getPanel()); noUndos++; movingPiece.setPanel(actionPanel); generation.setPanelsInUse(); movingPiecePanel.BackgroundImage = null; movingPiece.setMoved(true); movingPiece.increaseNoMoves(); //moves the castle to thing other side of the king } if (movingPiece.getType() == "WPawn" ^ movingPiece.getType() == "BPawn") { if ((movingPiece.getPanel().Location.Y == 0 && movingPiece.getType() == "WPawn") ^ (movingPiece.getPanel().Location.Y == 280 && movingPiece.getType() == "BPawn")) { generation.RemovePiece(movingPiece); newQueen = new Queen (movingPiece.getType().Substring(0, 1) + "Queen", movingPiece.getPanel(), true); generation.AddPiece(newQueen); } } //If a pawn reaches the end, it becoms a queen totalPossibleMoves = 0; foreach (Piece y in generation.getPieces().ToList()) { y.setMoves(generation, true, suicide); if ((WhiteMove && y.getType().Substring(0, 1) == "B") ^ (!WhiteMove && y.getType().Substring(0, 1) == "W")) { totalPossibleMoves += y.getMoves().Count; } } //Make sure there are some possible moves if (!(gameModes == "M3S") && movingPiece.checkCheck(generation)) { MessageBox.Show("Check!"); //Check check, if it is check, tell the players if (totalPossibleMoves == 0) { //There are no possible moves, it's checkemate timer1.Stop(); MessageBox.Show("Check mate! " + (movingPiece.checkType() == "B" ? "White" : "Black") + " wins!"); //EndGame() } } WhiteMove = !WhiteMove; movingPiece = null; } else { movingPiece = null; } } } } else { movingPiece = null; } } catch (NullReferenceException) { movingPiece = null; } undos.Add(noUndos); noUndos = 0; } } } }