public Panel bestMove(BoardGen board, double alpha, double beta) { Panel bestMove = null; double bestVal = 100000; bool removed = false; Piece removedPiece = null; board.setPanelsInUse(); allPossibleBMoves = board.getAllPossibleBMoves(board); allPossibleWMoves = board.getAllPossibleWMoves(board); possibleWPieces = board.getPossibleWPieces(); possibleBPieces = board.getPossibleBPieces(); for (var i = 0; i < allPossibleBMoves.Count(); i++) { Panel temp = possibleBPieces[i].getPanel(); if (board.getPanelsInUse().Contains(allPossibleBMoves[i])) { foreach (Piece y in board.getPieces().ToList()) { if (y.getPanel() == allPossibleBMoves[i]) { removedPiece = y; removed = true; board.RemovePiece(y); //take piece if we're taking a piece } } } possibleBPieces[i].setPanel(allPossibleBMoves[i]); board.setPanelsInUse(); board.getAllPossibleWMoves(board); board.getPossibleWPieces(); double tempVal = minimax(board, true, 1, board.getAllPossibleWMoves(board), board.getPossibleWPieces(), alpha, beta); //start a search on this move to see where it leads if (tempVal < bestVal) { bestVal = tempVal; beta = tempVal; bestMove = allPossibleBMoves[i]; piece = possibleBPieces[i]; //if this move leads to a good board value then tempVal will be small/more //negative.If it is then we update bestVal, update beta and update the best //move and the piece it moves to get there } possibleBPieces[i].setPanel(temp); if (removed) { removed = false; board.AddPiece(removedPiece); //return the removed piece to its position } board.setPanelsInUse(); //update board } return(bestMove); //returns the best move we found }
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 double getBoardValue(BoardGen board) { double boardValue = 0; //boardValue starts as zero foreach (Piece p in board.getPieces().ToList()) { if (p.getType().Substring(0, 1) == "B") { boardValue = boardValue - p.getValue(board); //when there's a black piece the board value becomes smaller/more negative } else { boardValue = boardValue + p.getValue(board); //otherwise the board value becomes bigger } } return(boardValue); }
public override void setMoves(BoardGen board, bool checkUp, bool suicide) { possibleMoves = new List <Panel>(); Panel tempPan = null; capturePossible = false; List <Panel> panelsRemove = new List <Panel>(); /*A knight moves in a L shape pattern, any "L" shape (3 squares in a * straight line and then one to the left or right).*/ foreach (Panel x in board.getPanels()) { if ((getPanel().Location.X == x.Location.X + 40 || getPanel().Location.X == x.Location.X - 40) && (getPanel().Location.Y == x.Location.Y + 80 || getPanel().Location.Y == x.Location.Y - 80)) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece y in board.getPieces()) { if (y.getPanel() == x) { if (getType().Substring(0, 1) != y.getType().Substring(0, 1)) { possibleMoves.Add(x); } } } } else { possibleMoves.Add(x); } } if ((getPanel().Location.X == x.Location.X + 80 || getPanel().Location.X == x.Location.X - 80) && (getPanel().Location.Y == x.Location.Y + 40 || getPanel().Location.Y == x.Location.Y - 40)) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece y in board.getPieces()) { if (y.getPanel() == x) { if (getType().Substring(0, 1) != y.getType().Substring(0, 1)) { possibleMoves.Add(x); } } } } else { possibleMoves.Add(x); } } } foreach (Panel x in possibleMoves) { foreach (Piece p in board.getPieces()) { if (x == p.getPanel()) { capturePossible = true; } } } /*This part gets all the possible moves, no need to remove panels being * blocked as a knight can jump over pieces. The only thing that would * make it not possible is if a same coloured piece is on the panel, this * was easy enough to add to the if statements*/ 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); } } /* Exactly like all other pieces, this part checks all of the current * possible moves for the the bisop 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 } } } }
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 } } } }
public double minimax(BoardGen board, bool max, int depth, List <Panel> possibleMoves, List <Piece> possiblePieces, double alpha, double beta) { double bestValue; bool removed = false; Piece removedPiece = null; board.setPanelsInUse(); if (depth == 0) { return(getBoardValue(board)); //We have searched as far as we need to and can return what the board value //would be in this position } if (max) //if the player is trying to maximise score { double bestVal = -100000; //set bestVal to some very large negative number so //that it easily beaten for (var i = 0; i < possibleMoves.Count(); i++) //for every move possible from //this position { Panel temp = possiblePieces[i].getPanel(); if (board.getPanelsInUse().Contains(possibleMoves[i])) { foreach (Piece y in board.getPieces().ToList()) { if (y.getPanel() == possibleMoves[i]) { removedPiece = y; removed = true; board.RemovePiece(y); //take piece if we're taking a piece } } } possiblePieces[i].setPanel(possibleMoves[i]); board.setPanelsInUse(); board.getAllPossibleWMoves(board); board.getPossibleWPieces(); //update the board for one of the possible moves double tempVal = minimax(board, !max, depth - 1, board.getAllPossibleBMoves(board), board.getPossibleBPieces(), alpha, beta); //dive deeper into a search looking ahead by 1 furhter witht tempVal if (tempVal > bestVal) { bestVal = tempVal; alpha = tempVal; //if tempVal is better than the current bestVal then this is the new //tempVal and alpha is set to tempVal } possiblePieces[i].setPanel(temp); if (removed) { removed = false; board.AddPiece(removedPiece); //put taken piece back } board.setPanelsInUse(); if (alpha >= beta) { return(bestVal); //If alpha is greater or equal to beta we can prune the search } } bestValue = bestVal; } else { double bestVal = 100000; //We're minimising so some large value will be easily //beaten here for (var i = 0; i < possibleMoves.Count(); i++) { Panel temp = possiblePieces[i].getPanel(); if (board.getPanelsInUse().Contains(possibleMoves[i])) { foreach (Piece y in board.getPieces().ToList()) { if (y.getPanel() == possibleMoves[i]) { removedPiece = y; removed = true; board.RemovePiece(y); //take piece if we're taking a piece } } } possiblePieces[i].setPanel(possibleMoves[i]); board.setPanelsInUse(); board.getAllPossibleBMoves(board); board.getPossibleBPieces(); //update the board to do this move double tempVal = minimax(board, !max, depth - 1, board.getAllPossibleWMoves(board), board.getPossibleWPieces(), alpha, beta); //go deeper to see if this move will lead anywhere good beta = Math.Min(beta, tempVal); if (tempVal < bestVal) { bestVal = tempVal; beta = tempVal; //if this move leads somewhere good then tempVal will be less than bestVal //as black tries to minimise, so this val is set to bestVal } possiblePieces[i].setPanel(temp); if (removed) { removed = false; board.AddPiece(removedPiece); //return the piece that was taken } board.setPanelsInUse(); if (alpha >= beta) { return(bestVal); //if alpha is greater than beta then we can prune the search a bit } } bestValue = bestVal; } return(bestValue); //return the best value we have found }
public override void setMoves(BoardGen board, bool checkUp, bool suicide) { possibleMoves = new List <Panel>(); Piece piece = null; Panel tempPan = null; capturePossible = false; //A bishop may move along the diagnol it is on foreach (Panel x in board.getPanels()) { for (int i = 0; i < 8; i++) { foreach (Piece c in board.getPieces()) { if (c.getPanel() == x) { piece = c; } } if ((getPanel().Location.X == x.Location.X + (40 * i) || getPanel().Location.X == x.Location.X - (40 * i)) && (getPanel().Location.Y == x.Location.Y + (40 * i) || getPanel().Location.Y == x.Location.Y - (40 * i))) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece y in board.getPieces()) { if (y.getPanel() == x) { if (getType().Substring(0, 1) != y.getType().Substring(0, 1)) { possibleMoves.Add(x); } } } } else { possibleMoves.Add(x); } } } } /* This bit just gets all of the panels in a diagonal direction from the * bishop in use */ List <Panel> panelsRemove = new List <Panel>(); foreach (Panel x in board.getPanels()) { for (int i = 0; i < 8; i++) { if ((getPanel().Location.X == x.Location.X + (40 * i) || getPanel().Location.X == x.Location.X - (40 * i)) && (getPanel().Location.Y == x.Location.Y + (40 * i) || getPanel().Location.Y == x.Location.Y - (40 * i)) && board.getPanelsInUse().Contains(x)) { if (x.Location.Y < getPanel().Location.Y&& x.Location.X < getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y < x.Location.Y && g.Location.X < x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.Y < getPanel().Location.Y&& x.Location.X > getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y < x.Location.Y && g.Location.X > x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.Y > getPanel().Location.Y&& x.Location.X < getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y > x.Location.Y && g.Location.X < x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.Y > getPanel().Location.Y&& x.Location.X > getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y > x.Location.Y && g.Location.X > x.Location.X) { panelsRemove.Add(g); } } } } } } /* This part adds all of the panels that are blocked in some way to an * array that removes all of those panels from possible moves (the next * part */ foreach (Panel x in possibleMoves) { foreach (Piece p in board.getPieces()) { if (x == p.getPanel()) { capturePossible = true; } } } foreach (Panel x in panelsRemove) { possibleMoves.Remove(x); } 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(); } /* This part checks all of the current possible moves for the * the bisop to see if any would leave the player in check, if it * would, it is not a valid move and is removed. */ foreach (Panel x in panelsRemove) { possibleMoves.Remove(x); } } 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 } } } }
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 } } } }
public override void setMoves(BoardGen board, bool checkUp, bool suicide) { possibleMoves = new List <Panel>(); capturePossible = false; //Rooks can move in a straigh line, up, down, left and right. foreach (Panel x in board.getPanels()) { if ((getPanel().Location.Y == x.Location.Y || getPanel().Location.X == x.Location.X)) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece y in board.getPieces()) { if (y.getPanel() == x) { if (getType().Substring(0, 1) != y.getType().Substring(0, 1)) { possibleMoves.Add(x); } } } } else { possibleMoves.Add(x); } } } /*This part just gets all of the panels with the same x or y value as * the rook in use*/ List <Panel> panelsRemove = new List <Panel>(); foreach (Panel x in board.getPanels()) { if (getPanel().Location.X == x.Location.X && board.getPanelsInUse().Contains(x)) { if (x.Location.Y > getPanel().Location.Y) { foreach (Panel g in possibleMoves) { if (g.Location.Y > x.Location.Y) { panelsRemove.Add(g); } } } else if (x.Location.Y < getPanel().Location.Y) { foreach (Panel g in possibleMoves) { if (g.Location.Y < x.Location.Y) { panelsRemove.Add(g); } } } } if (getPanel().Location.Y == x.Location.Y && board.getPanelsInUse().Contains(x)) { if (x.Location.X > getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.X > x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.X < getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.X < x.Location.X) { panelsRemove.Add(g); } } } } } /*This part adds any moves that aren't possible due to a piece blocking * the move to an array that is removed from possible moves*/ 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); } } /* Exactly like all other pieces, this part checks all of the current * possible moves for the the bisop 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; } } } }
public override void setMoves(BoardGen board, bool checkUp, bool suicide) { possibleMoves = new List <Panel>(); Panel tempPan = null; capturePossible = false; /*Queens move like a combination of a rook and bishop. Along a diagnol or * in a straight line*/ foreach (Panel x in board.getPanels()) { if ((getPanel().Location.Y == x.Location.Y || getPanel().Location.X == x.Location.X)) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece y in board.getPieces()) { if (y.getPanel() == x) { if (getType().Substring(0, 1) != y.getType().Substring(0, 1)) { possibleMoves.Add(x); } } } } else { possibleMoves.Add(x); } } } //Gets all the moves a queen could make if the board were empty List <Panel> panelsRemove = new List <Panel>(); foreach (Panel x in board.getPanels()) { if (getPanel().Location.X == x.Location.X && board.getPanelsInUse().Contains(x)) { if (x.Location.Y > getPanel().Location.Y) { foreach (Panel g in possibleMoves) { if (g.Location.Y > x.Location.Y) { panelsRemove.Add(g); } } } else if (x.Location.Y < getPanel().Location.Y) { foreach (Panel g in possibleMoves) { if (g.Location.Y < x.Location.Y) { panelsRemove.Add(g); } } } } if (getPanel().Location.Y == x.Location.Y && board.getPanelsInUse().Contains(x)) { if (x.Location.X > getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.X > x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.X < getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.X < x.Location.X) { panelsRemove.Add(g); } } } } } Piece piece = null; foreach (Panel x in board.getPanels()) { for (int i = 0; i < 8; i++) { foreach (Piece c in board.getPieces()) { if (c.getPanel() == x) { piece = c; } } if ((getPanel().Location.X == x.Location.X + (40 * i) || getPanel().Location.X == x.Location.X - (40 * i)) && (getPanel().Location.Y == x.Location.Y + (40 * i) || getPanel().Location.Y == x.Location.Y - (40 * i))) { if (board.getPanelsInUse().Contains(x)) { foreach (Piece y in board.getPieces()) { if (y.getPanel() == x) { if (getType().Substring(0, 1) != y.getType().Substring(0, 1)) { possibleMoves.Add(x); } } } } else { possibleMoves.Add(x); } } } } foreach (Panel x in board.getPanels()) { for (int i = 0; i < 8; i++) { if ((getPanel().Location.X == x.Location.X + (40 * i) || getPanel().Location.X == x.Location.X - (40 * i)) && (getPanel().Location.Y == x.Location.Y + (40 * i) || getPanel().Location.Y == x.Location.Y - (40 * i)) && board.getPanelsInUse().Contains(x)) { if (x.Location.Y < getPanel().Location.Y&& x.Location.X < getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y < x.Location.Y && g.Location.X < x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.Y < getPanel().Location.Y&& x.Location.X > getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y < x.Location.Y && g.Location.X > x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.Y > getPanel().Location.Y&& x.Location.X < getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y > x.Location.Y && g.Location.X < x.Location.X) { panelsRemove.Add(g); } } } else if (x.Location.Y > getPanel().Location.Y&& x.Location.X > getPanel().Location.X) { foreach (Panel g in possibleMoves) { if (g.Location.Y > x.Location.Y && g.Location.X > x.Location.X) { panelsRemove.Add(g); } } } } } } 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; } } } //Removes any moves blocked by another piece. 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); } } /* This part checks all of the current possible moves for the * the bisop 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 } } } }