void RefreshBuffer() { if (Tiles == null || Buffer == null) { return; } var g = Graphics.FromImage(Buffer); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.DrawImageUnscaled(Tiles, 0, 0); try { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { int index = x + (7 - y) * 8; if (Bitboard.Get(BitState, index)) { g.FillEllipse(Brushes.Black, (x + 0.5f) * TileSize - DotSize / 2, (y + 0.5f) * TileSize - DotSize / 2, DotSize, DotSize); } } } } catch (Exception) { } this.Invalidate(); }
public void TestUnsetGetTopBit() { ulong max = 0x8000000000000000; var bit = Bitboard.Get(max, 63); var bitref = Bitboard.GetRef(ref max, 63); Assert.IsTrue(bit); Assert.IsTrue(bitref); var unset = Bitboard.Unset(max, 63); Assert.AreEqual((ulong)0, unset); Assert.AreEqual(0x8000000000000000, max); Bitboard.UnsetRef(ref max, 63); Assert.AreEqual((ulong)0, max); var set = Bitboard.Set(max, 55); Assert.AreEqual((ulong)0x80000000000000, set); Assert.AreEqual((ulong)0, max); set = Bitboard.Set(max, 63); Assert.AreEqual((ulong)0x8000000000000000, set); Assert.AreEqual((ulong)0, max); Bitboard.SetRef(ref max, 63); Assert.AreEqual((ulong)0x8000000000000000, max); }
/// <summary> /// Convert the bitboard permutation into a valid attack board /// </summary> /// <param name="permutation"></param> /// <param name="index"></param> /// <returns></returns> internal static ulong GetMoves(ulong permutation, int index) { ulong moves = 0; int target = 0; // Move up target = index + 8; while (target < 64) { Bitboard.SetRef(ref moves, target); if (Bitboard.Get(permutation, target)) // check for blockers { break; } target += 8; } // Move down target = index - 8; while (target >= 0) { Bitboard.SetRef(ref moves, target); if (Bitboard.Get(permutation, target)) // check for blockers { break; } target -= 8; } // Move right target = index + 1; while (Chess.Base.Board.X(target) > Chess.Base.Board.X(index)) { Bitboard.SetRef(ref moves, target); if (Bitboard.Get(permutation, target)) // check for blockers { break; } target++; } // Move left target = index - 1; while (Chess.Base.Board.X(target) < Chess.Base.Board.X(index)) { Bitboard.SetRef(ref moves, target); if (Bitboard.Get(permutation, target)) // check for blockers { break; } target--; } return(moves); }
public void TestUnsetGet() { var v = Bitboard.Unset(256, 8); Assert.AreEqual((ulong)0, v); Assert.IsFalse(Bitboard.Get(v, 8)); v = Bitboard.Unset(256 + 128, 7); Assert.AreEqual((ulong)256, v); Assert.IsFalse(Bitboard.Get(v, 7)); }
public unsafe void TestMakePawn() { BoardStruct *b = (BoardStruct *)Board.Create(); Board.Init(b, 1); Board.Make(b, 12, 28); Assert.AreEqual(20, b->EnPassantTile); Assert.AreEqual(true, Bitboard.Get(b->Boards[Board.BOARD_PAWNS], 28)); Assert.AreEqual(false, Bitboard.Get(b->Boards[Board.BOARD_PAWNS], 12)); }
public void TestSetGet() { var v = Bitboard.Set(0, 8); Assert.AreEqual((ulong)256, v); Assert.IsTrue(Bitboard.Get(v, 8)); v = Bitboard.Set(256, 7); Assert.AreEqual((ulong)(256 + 128), v); Assert.IsTrue(Bitboard.Get(v, 7)); v = Bitboard.Set(563, 12); Assert.AreEqual((ulong)(563 + 4096), v); Assert.IsTrue(Bitboard.Get(v, 12)); }
public unsafe void TestMakeRooks() { BoardStruct *b = (BoardStruct *)Board.Create(); Board.Init(b, 1); Board.Make(b, 6, 21); Assert.AreEqual(1, b->CurrentMove); Assert.AreEqual(0, b->EnPassantTile); Assert.AreEqual(true, Bitboard.Get(b->Boards[Board.BOARD_KNIGHTS], 21)); Assert.AreEqual(false, Bitboard.Get(b->Boards[Board.BOARD_WHITE], 6)); Assert.AreEqual(Board.COLOR_BLACK, b->PlayerTurn); Board.Make(b, 62, 45); Assert.AreEqual(2, b->CurrentMove); Assert.AreEqual(2, b->FiftyMoveRulePlies); Assert.AreEqual(Board.COLOR_WHITE, b->PlayerTurn); Assert.AreEqual(true, Bitboard.Get(b->Boards[Board.BOARD_KNIGHTS], 45)); Assert.AreEqual(false, Bitboard.Get(b->Boards[Board.BOARD_BLACK], 62)); }
/// <summary> /// Gets all permutations possible for a rook in that position. /// checks all possible variations of blockers /// (between 1024-4096 possibilities, depending on rook position) /// </summary> /// <param name="pos"></param> /// <returns></returns> internal static List <ulong> GetPermutations(int pos) { var variations = new List <ulong>(); var vector = RookVectors[pos]; List <int> bitlist = new List <int>(); for (int i = 0; i < 64; i++) { if (Bitboard.Get(vector, i)) { bitlist.Add(i); } } // scroll through all permutations from 0...max int max = 1 << bitlist.Count; for (int val = 0; val < max; val++) { ulong permutation = 0; // set bits in the variation for (int b = 0; b < bitlist.Count; b++) { if (Bitboard.Get((ulong)val, b)) { Bitboard.SetRef(ref permutation, bitlist[b]); } else { Bitboard.UnsetRef(ref permutation, bitlist[b]); } } variations.Add(permutation); } return(variations); }
protected override void OnMouseDown(MouseEventArgs e) { try { Console.WriteLine("Bang"); int x = (e.X / TileSize); int y = 7 - (e.Y / TileSize); int index = x + y * 8; if (Bitboard.Get(BitState, index)) { BitState = Bitboard.Unset(BitState, index); } else { BitState = Bitboard.Set(BitState, index); } UpdateCallback(); } catch (Exception) { } }
public List <State> CalculateBlack(List <State> stateSet) { // find only moves where black MUST move to a state in the stateSet var newStates = new List <State>(); var b = Board.Create(); foreach (var state in stateSet) { var originalHash = state.Hash; state.PlayerTurn = Board.COLOR_BLACK; state.Hash = CalcHash(state); SetupBoard(b, state, Board.COLOR_BLACK); var moves = GetMovesReverse(b, Board.COLOR_BLACK); for (int i = 0; i < moves.Length; i++) { var from = moves[i][0]; var to = moves[i][1]; // set player to black and set the position to the "precursor" state state.PlayerTurn = Board.COLOR_BLACK; MovePieceState(state, to, from); SetupBoard(b, state, Board.COLOR_BLACK); b->CurrentMove = 0; if (AreKingsTouching(b)) { // revert the state back to the original state state.PlayerTurn = Board.COLOR_WHITE; MovePieceState(state, from, to); continue; } // get moves from precursor state to any other available states var moveBitboard = Moves.GetMoves(b, from); var movesFromPreviousPos = Bitboard.Bitboard_BitList(moveBitboard); // We see if there is a way for black to get out of a forced mate by looking // for any moves that escape it. If white has a forced mate, the input state (original state) // is supposedly the best move black could make. // IF black can make moves tho states that aren't already explored, then they lead to a better // state for black, thus he can escape bool canEscape = false; for (int j = 0; j < movesFromPreviousPos.Length; j++) { var backTo = movesFromPreviousPos[j]; var valid = Board.Make(b, from, backTo); if (valid) { Board.Unmake(b); // Check if this is a capture. We can't "un-capture" pieces so this would be a way out // for black. if (Bitboard.Get(b->Boards[Board.BOARD_WHITE], backTo)) { canEscape = true; break; } // find the hash for the next possible state state.PlayerTurn = Board.COLOR_WHITE; MovePieceState(state, from, backTo); var nextHash = state.Hash; state.PlayerTurn = Board.COLOR_BLACK; MovePieceState(state, backTo, from); // check if that is a known position if (!States.ContainsKey(nextHash)) { canEscape = true; break; } } } // If black can not escape the then moving to the original state is the best option black has. if (!canEscape) { var newState = new State(state); newState.NextState = originalHash; newState.DepthToMate++; newStates.Add(newState); } // revert the state back to the original state state.PlayerTurn = Board.COLOR_WHITE; MovePieceState(state, from, to); } // We messed with the state... alot. // Make sure there isn't a bug in the code and we just corrupted something :) if (originalHash != state.Hash) { throw new Exception("State was altered"); } } Board.Delete(b); return(newStates); }
void SetTexboxes() { try { if (State == (ulong)0) { textBoxHex.Text = ""; textBoxDec.Text = ""; textBoxList.Text = ""; } else { textBoxHex.Text = "0x" + String.Format("{0:X}", State); textBoxDec.Text = String.Format("{0:G}", State); var list = new List <int>(); for (int i = 0; i < 64; i++) { if (Bitboard.Get(State, i)) { list.Add(i); } } textBoxList.Text = list.Select(k => k.ToString()).Aggregate((x, y) => x + "," + y); } string bitsHigh = ""; for (int i = 63; i > 31; i--) { if ((i + 1) % 8 == 0 && i != 63) { bitsHigh += "."; } if (Bitboard.Get(State, i)) { bitsHigh += "1"; } else { bitsHigh += "0"; } } labelBitsUpper.Text = bitsHigh; string bitsLow = ""; for (int i = 31; i >= 0; i--) { if ((i + 1) % 8 == 0 && i != 31) { bitsLow += "."; } if (Bitboard.Get(State, i)) { bitsLow += "1"; } else { bitsLow += "0"; } } labelBitsLower.Text = bitsLow; } catch (Exception) { } }
/// <summary> /// Convert the bitboard permutation into a valid attack board /// </summary> /// <param name="permutation"></param> /// <param name="index"></param> /// <returns></returns> internal static unsafe ulong GetMoves(ulong permutation, int index) { ulong moves = 0; int target = 0; // Move up right target = index; while (true) { if (target < 64 && (Chess.Base.Board.X(target) > Chess.Base.Board.X(index) || target == index)) { Bitboard.SetRef(ref moves, target); } else { break; } if (Bitboard.Get(permutation, target)) // check for blockers { break; } target += 9; } // Move up left target = index; while (true) { if (target < 64 && (Chess.Base.Board.X(target) < Chess.Base.Board.X(index) || target == index)) { Bitboard.SetRef(ref moves, target); } else { break; } if (Bitboard.Get(permutation, target)) // check for blockers { break; } target += 7; } // Move down right target = index; while (true) { if (target >= 0 && (Chess.Base.Board.X(target) > Chess.Base.Board.X(index) || target == index)) { Bitboard.SetRef(ref moves, target); } else { break; } if (Bitboard.Get(permutation, target)) // check for blockers { break; } target -= 7; } // Move down left target = index; while (true) { if (target >= 0 && (Chess.Base.Board.X(target) <= Chess.Base.Board.X(index) || target == index)) { Bitboard.SetRef(ref moves, target); } else { break; } if (Bitboard.Get(permutation, target)) // check for blockers { break; } target -= 9; } Bitboard.UnsetRef(ref moves, index); return(moves); }