public void GetBoardData() { for (byte i = 0; i < boardSizeX; i++) { for (byte j = 0; j < boardSizeY; j++) { IChessItemModelShort c = iFigureOnBoard.GetFigureByPosition(new BoardPosition(i, j)); if (!c.IsNullObject) { board[i, j] = new ChessItemProperties(c.Pos.ToVector2Int(), c.Type, c.Side); } else { board[i, j] = new ChessItemProperties(new Vector2Int(225, 225), c.Type, c.Side, 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 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 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); } } } }