private bool ChechCheckmateState() { FigureOnBoard kingOfTheCurrentPlayer = GetKing(CurrentPlayer.Color); // Check if the king can move in a safe place. IEnumerable <Point> possiblePositionsOfKing = GetPossibleFigurePositions(kingOfTheCurrentPlayer, true); IEnumerable <Direction> enemyDirections = GetAllFiguresDirectionsByColor(EnemyPlayer.Color); IEnumerable <Point> enemyDirectionsPositions = GetAllFiguresPositionsByColor(EnemyPlayer.Color); foreach (Point kingPosition in possiblePositionsOfKing) { if (!enemyDirectionsPositions.Contains(kingPosition) && !CheckCheckStateAfterMove(kingOfTheCurrentPlayer, kingPosition)) { return(false); } } // Check if any of figures can block the king. IEnumerable <Point> currentPlayerFiguresPositions = GetAllFiguresPositionsByColor(CurrentPlayer.Color); foreach (Point potentialCurrentPlayerFigurePosition in currentPlayerFiguresPositions) { if (CheckIfPointBlocksDirections(enemyDirections, kingOfTheCurrentPlayer.Position, potentialCurrentPlayerFigurePosition)) { return(false); } } return(true); }
private void TryToMoveTo(FigureOnBoard figure, Point position) { if (!GetPossibleFigurePositions(figure).Contains(position)) { return; } if (CheckIfTryingToCastle(position)) { Tuple <FigureOnBoard, Point> rookAndPosition = GetCastlingRook(position); rookAndPosition.Item1.Position = rookAndPosition.Item2; } FigureOnBoard figureOnPosition = GetFigureByPosition(position); if (figureOnPosition != null) { DestroyFigure(figureOnPosition); } figure.Position = position; figure.Figure.Step(); SwitchPlayer(); CheckmateState state = GetCheckmateState(); if (state == CheckmateState.Checkmate) { GameOver(); } }
private void copySelectedFigure() { if (selectedFigure != null) { bufferedFigure = selectedFigure; btnPaste.Enabled = true; } }
public bool move(int from, int to) { FigureOnBoard tmp = (FigureOnBoard)mas[from]; mas.Remove(tmp); mas.Insert(to, tmp); return(true); }
public bool shuffle(int firstIndex, int secondIndex) { FigureOnBoard tmp = (FigureOnBoard)mas[secondIndex]; mas[secondIndex] = mas[firstIndex]; mas[firstIndex] = tmp; return(true); }
public bool add(FigureOnBoard addingFigure) { if (mas.Add(addingFigure) != 0) { return(true); } else { return(false); } }
public IEnumerable <Point> GetPossibleFigurePositions(FigureOnBoard figure, bool isPotentialCalculation = false) { IEnumerable <Direction> directions = GetFigureDirections(figure, isPotentialCalculation); foreach (Direction direction in directions) { foreach (Point position in direction.Positions) { yield return(position); } } }
private void unselectFigure() { if (selectedFigure != null) { selectedFigure.figure.unselectFigure(); btnCopy.Enabled = false; btnDelete.Enabled = false; btnInverse.Enabled = false; btnMirrorHorizontal.Enabled = false; btnMirrorVertical.Enabled = false; btnMoveBack.Enabled = false; btnMoveFront.Enabled = false; } selectedFigure = null; }
private void ReturnLastAction() { CanSaveState obj = savedStateStack.Pop(); obj.backToPrevious(); if (!bkgImages.Contains(selectedFigure)) { selectedFigure = null; } UpdateGraphics(); if (savedStateStack.Count == 0) { btnReturnLastAction.Enabled = false; } }
private void deleteFigure(FigureOnBoard selectedFigure) { if (selectedFigure.figure is BkgImage) { DeletedImageState deletedImageState = new DeletedImageState(bkgImages, selectedFigure); saveFigureState(deletedImageState); bkgImages.remove(selectedFigure); } else { facades.remove(selectedFigure); } unselectFigure(); UpdateGraphics(); }
private void selectFigure(FigureOnBoard item) { if (selectedFigure != null) { selectedFigure.figure.unselectFigure(); } btnReturnLastAction.Enabled = true; selectedFigure = item; selectedFigure.figure.selectFigure(); btnCopy.Enabled = true; btnDelete.Enabled = true; btnInverse.Enabled = true; btnMirrorHorizontal.Enabled = true; btnMirrorVertical.Enabled = true; btnMoveBack.Enabled = true; btnMoveFront.Enabled = true; }
public void DoActionByPositions(Point position) { if (currentlySelectedFigure != null) { TryToMoveTo(currentlySelectedFigure, position); currentlySelectedFigure.Selected = false; currentlySelectedFigure = null; } FigureOnBoard figure = GetCurrentPlayerFigureByPosition(position); if (figure != null) { currentlySelectedFigure = figure; currentlySelectedFigure.Selected = true; } }
private IEnumerable <Direction> GetFigureDirections(FigureOnBoard figure, bool isPotentialCalculation = false) { IEnumerable <Direction> figureDirections = figure.Figure.PossiblePositionsToMove(); Direction newDirection = new Direction(); foreach (Direction direction in figureDirections) { foreach (Point vector in direction.Positions) { Point potentialPosition = figure.Position.Add(vector); FigureOnBoard figureOnPosition = GetFigureByPosition(potentialPosition); if (potentialPosition.CheckIfOutsideTheBoard() || figureOnPosition?.Color == figure.Color) { break; } else if (!isPotentialCalculation && CheckCheckStateAfterMove(figure, potentialPosition)) { break; } else if (CheckIfFigureCanMoveTo(figure, potentialPosition, isPotentialCalculation)) { newDirection.Positions.Add(potentialPosition); } else if (CheckIfFigureCanBeatTo(figure, potentialPosition)) { newDirection.Positions.Add(potentialPosition); break; } else { break; } } } IEnumerable <Point> castlingPositions = GetCastlingPositions(figure); foreach (Point castlingPosition in castlingPositions) { newDirection.Positions.Add(castlingPosition); } yield return(newDirection); }
/// <summary> /// Block a figure’s ability to move if this leads to a check. /// </summary> /// <param name="figure">Figure to move.</param> /// <param name="potentialPosition">Potential position of a figure.</param> /// <returns>True if movement leads to a check.</returns> private bool CheckCheckStateAfterMove(FigureOnBoard figure, Point potentialPosition) { FigureOnBoard kingOfCurrentPlayer = GetKing(CurrentPlayer.Color); if (kingOfCurrentPlayer == null) { return(false); } Point currentPosition = figure.Position; figure.Position = potentialPosition; bool result = ChechCheckState(); figure.Position = currentPosition; return(result); }
public void addFigure(Figure figure, int x, int y) { if (figure is BkgImage) { FigureOnBoard figureOnBoard = new FigureOnBoard(figure, x, y); addedImageState addImageState = new addedImageState(bkgImages, figureOnBoard); saveFigureState(addImageState); bkgImages.add(figureOnBoard); if (selectedFigure != null) { unselectFigure(); } selectFigure(figureOnBoard); } else { facades.add(new FigureOnBoard(figure, x, y)); } UpdateGraphics(); }
private bool CheckIfFigureCanBeatTo(FigureOnBoard figure, Point potentialPosition) { FigureOnBoard figureOnPotentialPosition = GetFigureByPosition(potentialPosition); if (figure.Figure is Pawn) { if (potentialPosition.Subtract(figure.Position).X != 0) // If trying to move diagonally. { return(figureOnPotentialPosition != null && figureOnPotentialPosition.Color != figure.Color); } } else { return(figureOnPotentialPosition != null && figureOnPotentialPosition.Color != figure.Color); } return(false); }
private bool ChechCheckState() { FigureOnBoard kingOfCurrentPlayer = GetKing(CurrentPlayer.Color); if (kingOfCurrentPlayer == null) { return(false); } IEnumerable <Direction> enemyDirections = GetAllFiguresDirectionsByColor(EnemyPlayer.Color); foreach (Direction enemyDirection in enemyDirections) { if (enemyDirection.Positions.Contains(kingOfCurrentPlayer.Position)) { return(true); } } return(false); }
private bool CheckIfFigureCanMoveTo(FigureOnBoard figure, Point potentialPosition, bool isPotentialCalculation = false) { FigureOnBoard figureOnPotentialPosition = GetFigureByPosition(potentialPosition); if (figureOnPotentialPosition == null) { if (figure.Figure is Pawn) { if (potentialPosition.Subtract(figure.Position).X == 0) // If trying to move vertically. { return(true); } } else { return(true); } } return(false); }
public CheckmateState GetCheckmateState() { if (CurrentPlayer == null) { return(CheckmateState.None); } FigureOnBoard kingOfCurrentPlayer = GetKing(CurrentPlayer.Color); if (kingOfCurrentPlayer == null) { return(CheckmateState.None); } if (ChechCheckmateState()) { return(CheckmateState.Checkmate); } else if (ChechCheckState()) { return(CheckmateState.Check); } return(CheckmateState.None); }
private IEnumerable <Point> GetCastlingPositions(FigureOnBoard kingOnBoard) { if (!(kingOnBoard.Figure is King king) || !king.IsNeverMoved) { yield break; } castlingConditions = new Dictionary <Tuple <FigureOnBoard, Point>, Tuple <FigureOnBoard, Point> >(); int kingXPos = (int)kingOnBoard.Position.X; int kingYPos = (int)kingOnBoard.Position.Y; List <FigureOnBoard> rooks = new List <FigureOnBoard> { GetRookForCastling(true, kingYPos), GetRookForCastling(false, kingYPos) }; foreach (FigureOnBoard rook in rooks) { if (rook == null || kingOnBoard.Color != rook.Color) { continue; } bool isPathClear = true; int rookXPos = (int)rook.Position.X; int fromX, toX; Point additionalVector; Point additionalRooksPotentialPosition; if (rookXPos < kingXPos) { fromX = rookXPos; toX = kingXPos; additionalVector = new Point(-2, 0); additionalRooksPotentialPosition = kingOnBoard.Position.Add(additionalVector).Add(new Point(1, 0)); } else { fromX = kingXPos; toX = rookXPos; additionalVector = new Point(2, 0); additionalRooksPotentialPosition = kingOnBoard.Position.Add(additionalVector).Add(new Point(-1, 0)); } for (int x = fromX + 1; x < toX; x++) { if (GetFigureByPosition(new Point(x, kingYPos)) != null) { isPathClear = false; break; } } if (isPathClear) { Point kingsPotentialPosition = kingOnBoard.Position.Add(additionalVector); castlingConditions.Add(new Tuple <FigureOnBoard, Point>(kingOnBoard, kingsPotentialPosition), new Tuple <FigureOnBoard, Point>(rook, additionalRooksPotentialPosition)); yield return(kingsPotentialPosition); } } }
private void DestroyFigure(FigureOnBoard figure) { FiguresOnBoard.Remove(figure); }
private int getIndex(FigureOnBoard selectedFigure) { return(bkgImages.getIndex(selectedFigure)); }
public ICollection <FigureOnBoard> getFacades() { LinkedList <FigureOnBoard> facades = new LinkedList <FigureOnBoard>(); try { _appli.StartSessionFromCallParams(iParamsBlock); }catch (Exception e) { throw new MethodAccessException("Cannot start session: " + e.StackTrace); } int n = _scene.SelectionGetObjectsNb(); if (n < 1) { throw new ArgumentNullException("Please selected object in scene"); } int objectId = 0; float x = 0, y = 0, z = 0, width = 0, height = 0, angle = 0, heightScenes, xScenes, yScenes, minX = float.MaxValue, minY = float.MaxValue; int onOrUnder; if (!float.TryParse(_scene.SceneGetInfo(SceneEnum.SceneInfo.DIMX), out xScenes) || !float.TryParse(_scene.SceneGetInfo(SceneEnum.SceneInfo.DIMY), out yScenes) || !float.TryParse(_scene.SceneGetInfo(SceneEnum.SceneInfo.DIMZ), out heightScenes)) { throw new Exception("cannot get dimension of Scenes"); } //MessageBox.Show("CountOfElement=" + n); for (int i = 0; i < n; i++) { objectId = _scene.SelectionGetObjectId(i); if (!searchLabel(objectId)) { continue; } //MessageBox.Show(objectId.ToString()); //MessageBox.Show("x=" + x + " y = " + y + " width=" + width + "height=" + height); string textureNum = _scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.TEXTURE); //MessageBox.Show(textureNum); int textureNumber = -1; if (!textureNum.Equals("")) { try { textureNum = textureNum.Split(";,".ToCharArray())[1]; } catch (Exception) { textureNumber = -1; } if (!Int32.TryParse(textureNum, out textureNumber)) { textureNumber = -1; } } //MessageBox.Show(textureNumber.ToString()); if (float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.POSX), out x) && float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.POSY), out y) && float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.POSZ), out z) && float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.DIMX), out width) && float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.DIMZ), out height) && int.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.ON_OR_UNDER), out onOrUnder) && float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.ANGLEXY), out angle) && float.TryParse(_scene.ObjectGetInfo(objectId, SceneEnum.ObjectInfo.ANGLEXY), out angle)) { FigureOnBoard fig = createFacadeOnBoardFromKdScenes(objectId, textureNumber, x, y, z, width, height, angle, xScenes, yScenes, heightScenes, onOrUnder, getSceneDimension()); if (fig.x < minX) { minX = fig.x; } if (fig.y < minY) { minY = fig.y; } facades.AddLast(fig); } else { MessageBox.Show("I cant get all values from object"); } } foreach (FigureOnBoard item in facades) { item.x -= (int)minX; item.y -= (int)minY; } return(facades); }
public virtual void DoAction(BoardPosition bp, bool back = false) { var fig = FigureOnBoard.GetFigureByPosition(bp); if (fig.IsNullObject && ChessMatchCurrentState.CurrentSelectedPosition == BoardPosition.None) { return; } var prev_bp = ChessMatchCurrentState.PreviousSelectedPosition; var curr_bp = ChessMatchCurrentState.CurrentSelectedPosition; SetBPs(ChessMatchCurrentState.CurrentSelectedPosition, bp); IChessItemModelShort chess = FigureOnBoard.GetFigureByPosition(curr_bp); IChessItemModelShort prev_chess; if (prev_bp.horizontal < 0) { prev_chess = new NullChessItemModelShort(); } else { prev_chess = FigureOnBoard.GetFigureByPosition(curr_bp); } //If there wasn't chosen any position on board if (prev_chess.IsNullObject) { //If there figure on this board position and it's side is current player's side if (!chess.IsNullObject && ChessMatchCurrentState.CurrentPlayer.Side == chess.Side) { Select(); } } //If one of the positions on board is chosen else { bool wasMoved = false; //Check for possible moves List <bool> temp; foreach (var possibleMove in FigureOnBoard.GetPossibleMoves(ChessMatchCurrentState.PreviousSelectedPosition, out temp)) { //If figure can move to this board position if (possibleMove == ChessMatchCurrentState.CurrentSelectedPosition) { Move(); wasMoved = true; break; } } //If move was not possible if (!wasMoved) { if (!chess.IsNullObject && chess.Side == prev_chess.Side) { Select(); } else { SetBPs(prev_bp, curr_bp); } } } }
public int getIndex(FigureOnBoard figure) { return(mas.IndexOf(figure)); }
public bool insert(int index, FigureOnBoard insetingFigure) { mas.Insert(index, insetingFigure); return(true); }
public bool remove(FigureOnBoard removingFigure) { mas.Remove(removingFigure); return(true); }
private void SearchForMove() { double secs = DateTime.Now.ToOADate() * ChessEngineConstants.SecondsInDay; double new_secs = secs; while (new_secs - secs < (double)skill * 0.5) { /*Thinking*/ new_secs = DateTime.Now.ToOADate() * ChessEngineConstants.SecondsInDay; } chessUCIEngine.Stop(); secs = DateTime.Now.ToOADate() * ChessEngineConstants.SecondsInDay; new_secs = secs; bool gotMove = false; bool isMate = false; while (!gotMove) { new_secs = DateTime.Now.ToOADate() * ChessEngineConstants.SecondsInDay; if (new_secs - secs > 0.5) { secs = new_secs; int k = 0; var answers = chessUCIEngine.GetAnswers(); foreach (var answer in answers) { if (answer.Contains("bestmove")) { if (!(char.IsLetter(answer[9]) && char.IsDigit(answer[10]) && char.IsLetter(answer[11]) && char.IsDigit(answer[12]))) { throw new System.Exception("Wrong bestmove message format: " + answer); } string move_cmd = new string(new char[] { answer[9], answer[10], answer[11], answer[12] }); BoardPosition from_temp; BoardPosition to_temp; UciConverter.GetBoardPositionsFromMoveCommand(move_cmd, out from_temp, out to_temp); IChessItemModelShort fig = FigureOnBoard.GetFigureByPosition(from_temp); if (fig.IsNullObject || fig.Side != Side) { throw new Exception("Wrong figure placement!"); } List <bool> temp; var bps = FigureOnBoard.GetPossibleMoves(fig.Pos, out temp); bool is_to_temp_legal = false; foreach (var bp in bps) { if (bp == to_temp) { is_to_temp_legal = true; break; } } if (!is_to_temp_legal) { //throw new Exception("Move Is Not Possibile by chess rules!"); } if (!answer.Contains("ponder")) { isMate = true; } gotMove = true; UciConverter.GetBoardPositionsFromMoveCommand(move_cmd, out from, out to); break; } } } } SetBPs(ChessMatchCurrentState.CurrentSelectedPosition, from); Select(); SetBPs(from, to); Move(); }
public override void DoAction(BoardPosition bp, bool back = false) { var fig = FigureOnBoard.GetFigureByPosition(bp); if (fig.IsNullObject && ChessMatchCurrentState.CurrentSelectedPosition == BoardPosition.None) { return; } var prev_bp = ChessMatchCurrentState.PreviousSelectedPosition; var curr_bp = ChessMatchCurrentState.CurrentSelectedPosition; SetBPs(ChessMatchCurrentState.CurrentSelectedPosition, bp); IChessItemModelShort chess = FigureOnBoard.GetFigureByPosition(ChessMatchCurrentState.CurrentSelectedPosition); IChessItemModelShort prev_chess; if (ChessMatchCurrentState.PreviousSelectedPosition.IsNullObject) { prev_chess = new NullChessItemModelShort(); } else { prev_chess = FigureOnBoard.GetFigureByPosition(ChessMatchCurrentState.PreviousSelectedPosition); } //If there wasn't chosen any position on board if (prev_chess.IsNullObject) { //If there figure on this board position and it's side is current player's side if (!chess.IsNullObject && ChessMatchCurrentState.CurrentPlayer.Side == chess.Side) { Select(); } } //If one of the positions on board is chosen else { bool wasMoved = false; //Check for possible moves var possibleMoves = new List <BoardPosition>(); List <bool> temp; if (!back) { possibleMoves = FigureOnBoard.GetPossibleMoves(ChessMatchCurrentState.PreviousSelectedPosition, out temp); } else { for (byte i = 0; i < FigureOnBoard.BoardSizeX; i++) { for (byte j = 0; j < FigureOnBoard.BoardSizeY; j++) { possibleMoves.Add(new BoardPosition(i, j)); } } } foreach (var possibleMove in possibleMoves) { //If figure can move to this board position if (possibleMove == ChessMatchCurrentState.CurrentSelectedPosition) { Move(); wasMoved = true; break; } } //If move was not possible if (!wasMoved) { if (!chess.IsNullObject && chess.Side == prev_chess.Side) { Select(); } else { SetBPs(prev_bp, curr_bp); } } } }