//gets all bishop danger moves from a square - "x-rays" the king public static List <square> bishopDangerMoves(square startSquare) { List <square> ret = new List <square>(); int x = startSquare.xPosition; int y = startSquare.yPosition; int i = x; int j = y; bool shouldStop = false; //starting clockwise from the top i = x + 1; j = y - 1; while (i < 8 && j >= 0) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType != "king") //&& board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType == "king" && board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (shouldStop) { break; } i++; j--; } //90 i = x + 1; j = y + 1; shouldStop = false; while (i < 8 && j < 8) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType != "king") //&& board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType == "king" && board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (shouldStop) { break; } i++; j++; } //180 i = x - 1; j = y + 1; shouldStop = false; while (i >= 0 && j < 8) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType != "king") //&& board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType == "king" && board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (shouldStop) { break; } i--; j++; } //270 i = x - 1; j = y - 1; shouldStop = false; while (i >= 0 && j >= 0) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType != "king") //&& board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (board.squareArray[i, j].here != null && board.squareArray[i, j].here.pieceType == "king" && board.squareArray[i, j].here.pieceColor != board.turn) { shouldStop = true; } if (shouldStop) { break; } i--; j--; } return(ret); }
//gets the rook slider moves - for calculating pushMask public static List <square> rookSliderMoves(square startSquare) { List <square> ret = new List <square>(); int x = startSquare.xPosition; int y = startSquare.yPosition; int i = x; int j = y; bool shouldStop = false; //starting from the top, clockwise i = x; j = y - 1; shouldStop = false; while (j >= 0) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null) { shouldStop = true; } if (shouldStop) { break; } j--; } //90 i = x + 1; j = y; shouldStop = false; while (i < 8) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null) { shouldStop = true; } if (shouldStop) { break; } i++; } //180 i = x; j = y + 1; shouldStop = false; while (j < 8) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null) { shouldStop = true; } if (shouldStop) { break; } j++; } //270 i = x - 1; j = y; shouldStop = false; while (i >= 0) { ret.Add(board.squareArray[i, j]); if (board.squareArray[i, j].here != null) { shouldStop = true; } if (shouldStop) { break; } i--; } return(ret); }
public move(piece p, square s) { movablePiece = p; targetSquare = s; }
//A.I. play constructor public board(string mode, string faction) { InitializeComponent(); board.mode = mode; loadBoard(this, null); playerfaction = faction; //initiate square array with no pieces present squareArray = new square[8, 8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { squareArray[i, j] = new square(i, j); } } //only normal chess for now foreach (square sq in squareArray) { if (sq.yPosition == 1 || sq.yPosition == 6) { if (sq.yPosition == 1) { sq.here = new Pieces.pawn(sq.xPosition, sq.yPosition, "black"); } else { sq.here = new Pieces.pawn(sq.xPosition, sq.yPosition, "white"); } //draw the piece on the chessboard _chessBoardPanels[sq.xPosition, sq.yPosition].BackgroundImage = sq.here.pieceImage; } if (sq.yPosition == 0) //black pieces { if (sq.xPosition == 0 || sq.xPosition == 7) { sq.here = new Pieces.rook(sq.xPosition, sq.yPosition, "black"); } if (sq.xPosition == 1 || sq.xPosition == 6) { sq.here = new Pieces.knight(sq.xPosition, sq.yPosition, "black"); } if (sq.xPosition == 2 || sq.xPosition == 5) { sq.here = new Pieces.bishop(sq.xPosition, sq.yPosition, "black"); } if (sq.xPosition == 3) { sq.here = new Pieces.queen(sq.xPosition, sq.yPosition, "black"); } if (sq.xPosition == 4) { sq.here = new Pieces.king(sq.xPosition, sq.yPosition, "black"); } _chessBoardPanels[sq.xPosition, sq.yPosition].BackgroundImage = sq.here.pieceImage; } if (sq.yPosition == 7) //white pieces { if (sq.xPosition == 0 || sq.xPosition == 7) { sq.here = new Pieces.rook(sq.xPosition, sq.yPosition, "white"); } if (sq.xPosition == 1 || sq.xPosition == 6) { sq.here = new Pieces.knight(sq.xPosition, sq.yPosition, "white"); } if (sq.xPosition == 2 || sq.xPosition == 5) { sq.here = new Pieces.bishop(sq.xPosition, sq.yPosition, "white"); } if (sq.xPosition == 3) { sq.here = new Pieces.queen(sq.xPosition, sq.yPosition, "white"); } if (sq.xPosition == 4) { sq.here = new Pieces.king(sq.xPosition, sq.yPosition, "white"); } _chessBoardPanels[sq.xPosition, sq.yPosition].BackgroundImage = sq.here.pieceImage; } //add the pieces to the piece list if (sq.here != null) { if (sq.here.pieceColor == "white") { whitePieces.Add(sq.here); } else { blackPieces.Add(sq.here); } } } }
//TODO //handler of the player clicking a square on the board private void boardClickHandler(object sender, EventArgs e) { var colorDark = Color.DarkGray; var colorLight = Color.White; var colorSelected = Color.OrangeRed; var colorMovable = Color.DarkSeaGreen; Panel clickedPanel = (Panel)sender; //the piece the player tried to click (can be null! = clicked an empty square) piece clickedPiece = squareArray[clickedPanel.Location.X / tileSize, clickedPanel.Location.Y / tileSize].here; square clickedSquare = squareArray[clickedPanel.Location.X / tileSize, clickedPanel.Location.Y / tileSize]; //selecting squares works //TODO - check for present pieces //TODO - check of other selected squares - global variable? //dummy for debugging the game board - uncomment to use /*if(clickedPanel.BackColor == colorSelected) * { * if ((clickedPanel.Location.X / tileSize) % 2 == 0) * clickedPanel.BackColor = (clickedPanel.Location.Y / tileSize) % 2 != 0 ? colorDark : colorLight; * else * clickedPanel.BackColor = (clickedPanel.Location.Y / tileSize) % 2 != 0 ? colorLight : colorDark; * } * else clickedPanel.BackColor = colorSelected;*/ //if it's the first turn of the game, end turn to evaluate moves for pieces if (firstMove) { firstMove = false; endTurn(); if (board.mode == "Play versus A.I." && turn != playerfaction) { move aiMove = ai_core.getMove(); //simulate the ai clicking on the board to make the move boardClickHandler(_chessBoardPanels[aiMove.movablePiece.xPosition, aiMove.movablePiece.yPosition], null); boardClickHandler(_chessBoardPanels[aiMove.targetSquare.xPosition, aiMove.targetSquare.yPosition], null); } } if (clickedPiece == null && selectedPiece == null) //player clicked meaninglessly { //do nothing } //the player attempted to select a piece while not having one selected else if (selectedPiece == null) //clicked piece can NOT be null { if (clickedPiece.pieceColor == turn) //the player clicked his own piece (selected) { selectedPiece = clickedPiece; movableSquares = new List <square>(); foreach (move m in legalMoves) { if (m.movablePiece == selectedPiece) { movableSquares.Add(m.targetSquare); } } //update the gameboard with movable squares (in green) _chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].BackColor = colorSelected; foreach (square s in movableSquares) { _chessBoardPanels[s.xPosition, s.yPosition].BackColor = colorMovable; } } //else do nothing - the player tried to select the enemy piece } //the player attempted to deselect a piece else if ((clickedPiece == null && !(movableSquares.Contains(clickedSquare))) || (clickedPiece == selectedPiece)) { //deselect the selected piece if ((_chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].Location.X / tileSize) % 2 == 0) { _chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].BackColor = (_chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].Location.Y / tileSize) % 2 != 0 ? colorDark : colorLight; } else { _chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].BackColor = (_chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].Location.Y / tileSize) % 2 != 0 ? colorLight : colorDark; } selectedPiece = null; //update the board to deselect the pieces in color foreach (square s in movableSquares) { Panel workingPanel = _chessBoardPanels[s.xPosition, s.yPosition]; if ((workingPanel.Location.X / tileSize) % 2 == 0) { workingPanel.BackColor = (workingPanel.Location.Y / tileSize) % 2 != 0 ? colorDark : colorLight; } else { workingPanel.BackColor = (workingPanel.Location.Y / tileSize) % 2 != 0 ? colorLight : colorDark; } } movableSquares = null; } //the player selected one of his other pieces while having one selected already else if (selectedPiece != null && clickedPiece != null && clickedPiece.pieceColor == turn) { //deselect the selected piece if ((_chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].Location.X / tileSize) % 2 == 0) { _chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].BackColor = (_chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].Location.Y / tileSize) % 2 != 0 ? colorDark : colorLight; } else { _chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].BackColor = (_chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].Location.Y / tileSize) % 2 != 0 ? colorLight : colorDark; } selectedPiece = null; //update the board to deselect the pieces in color foreach (square s in movableSquares) { Panel workingPanel = _chessBoardPanels[s.xPosition, s.yPosition]; if ((workingPanel.Location.X / tileSize) % 2 == 0) { workingPanel.BackColor = (workingPanel.Location.Y / tileSize) % 2 != 0 ? colorDark : colorLight; } else { workingPanel.BackColor = (workingPanel.Location.Y / tileSize) % 2 != 0 ? colorLight : colorDark; } } movableSquares = null; //now select the new piece selectedPiece = clickedPiece; movableSquares = new List <square>(); foreach (move m in legalMoves) { if (m.movablePiece == selectedPiece) { movableSquares.Add(m.targetSquare); } } //update the gameboard with movable squares (in green) _chessBoardPanels[selectedPiece.xPosition, selectedPiece.yPosition].BackColor = colorSelected; foreach (square s in movableSquares) { _chessBoardPanels[s.xPosition, s.yPosition].BackColor = colorMovable; } } //the player performed a move (or a take) else if (selectedPiece != null && movableSquares.Contains(clickedSquare)) { selectedPiece.move(clickedSquare); endTurn(); //handler for the A.I. to make a move if (board.mode == "Play versus A.I." && turn != playerfaction) { move aiMove = ai_core.getMove(); //simulate the ai clicking on the board to make the move boardClickHandler(_chessBoardPanels[aiMove.movablePiece.xPosition, aiMove.movablePiece.yPosition], null); boardClickHandler(_chessBoardPanels[aiMove.targetSquare.xPosition, aiMove.targetSquare.yPosition], null); } } }
//move the piece to the specified square //NOTE: the square must be legal! public abstract void move(square s);