Add files via upload

This commit is contained in:
Coding with Tom
2025-01-12 21:46:12 +00:00
committed by GitHub
parent f883124378
commit 15bbd7b1fc
11 changed files with 3826 additions and 0 deletions

1860
Go/Perft.go Normal file

File diff suppressed because it is too large Load Diff

145
Go/bbconstants.go Normal file
View File

@@ -0,0 +1,145 @@
package main
var SQUARE_BBS [64]uint64 = [64]uint64{
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192,
16384,
32768,
65536,
131072,
262144,
524288,
1048576,
2097152,
4194304,
8388608,
16777216,
33554432,
67108864,
134217728,
268435456,
536870912,
1073741824,
2147483648,
4294967296,
8589934592,
17179869184,
34359738368,
68719476736,
137438953472,
274877906944,
549755813888,
1099511627776,
2199023255552,
4398046511104,
8796093022208,
17592186044416,
35184372088832,
70368744177664,
140737488355328,
281474976710656,
562949953421312,
1125899906842624,
2251799813685248,
4503599627370496,
9007199254740992,
18014398509481984,
36028797018963968,
72057594037927936,
144115188075855872,
288230376151711744,
576460752303423488,
1152921504606846976,
2305843009213693952,
4611686018427387904,
9223372036854775808,
}
const ONE_U64 uint64 = 1
const BP_STARTING_POSITIONS uint64 = 65280
const WP_STARTING_POSITIONS uint64 = 71776119061217280
const BK_STARTING_POSITION uint64 = 16
const WK_STARTING_POSITION uint64 = 1152921504606846976
const BN_STARTING_POSITIONS uint64 = 66
const WN_STARTING_POSITIONS uint64 = 4755801206503243776
const WR_STARTING_POSITIONS uint64 = 9295429630892703744
const BR_STARTING_POSITIONS uint64 = 129
const BB_STARTING_POSITIONS uint64 = 36
const WB_STARTING_POSITIONS uint64 = 2594073385365405696
const WQ_STARTING_POSITION uint64 = 576460752303423488
const BQ_STARTING_POSITION uint64 = 8
const EMPTY_BITBOARD uint64 = 0
const MAX_ULONG uint64 = 18446744073709551615
const MAGIC uint64 = 0x03f79d71b4cb0a89
var DEBRUIJN64 [64]int = [64]int{
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63,
}
func BitscanForward(tempBitboard uint64) int {
return (DEBRUIJN64[MAGIC*(tempBitboard^(tempBitboard-1))>>58])
}
const RANK_1_BITBOARD = 18374686479671623680
const RANK_2_BITBOARD = 71776119061217280
const RANK_3_BITBOARD = 280375465082880
const RANK_4_BITBOARD = 1095216660480
const RANK_5_BITBOARD = 4278190080
const RANK_6_BITBOARD = 16711680
const RANK_7_BITBOARD = 65280
const RANK_8_BITBOARD = 255
const FILE_A_BITBOARD = 72340172838076673
const FILE_B_BITBOARD = 144680345676153346
const FILE_C_BITBOARD = 289360691352306692
const FILE_D_BITBOARD = 578721382704613384
const FILE_E_BITBOARD = 1157442765409226768
const FILE_F_BITBOARD = 2314885530818453536
const FILE_G_BITBOARD = 4629771061636907072
const FILE_H_BITBOARD = 9259542123273814144
var SQ_CHAR_Y [65]byte = [65]byte{
'8', '8', '8', '8', '8', '8', '8', '8',
'7', '7', '7', '7', '7', '7', '7', '7',
'6', '6', '6', '6', '6', '6', '6', '6',
'5', '5', '5', '5', '5', '5', '5', '5',
'4', '4', '4', '4', '4', '4', '4', '4',
'3', '3', '3', '3', '3', '3', '3', '3',
'2', '2', '2', '2', '2', '2', '2', '2',
'1', '1', '1', '1', '1', '1', '1', '1', 'A',
}
var SQ_CHAR_X [65]byte = [65]byte{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'N',
}

15
Go/bbutils.go Normal file
View File

@@ -0,0 +1,15 @@
package main
func EmptyBit(bitboard uint64, square int) uint64 {
var shifted_bitboard = ONE_U64 << uint64(square)
return bitboard & ^shifted_bitboard
}
func SetBit(bitboard uint64, square int) uint64 {
return bitboard | (ONE_U64 << uint64(square))
}
func MoveBit(bitboard uint64, starting_square int, target_square int) uint64 {
bitboard = EmptyBit(bitboard, starting_square)
return SetBit(bitboard, target_square)
}

123
Go/board.go Normal file
View File

@@ -0,0 +1,123 @@
package main
import "fmt"
var PieceArray [12]uint64 = [12]uint64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
var whiteToPlay bool = true
var CastleRights [4]bool = [4]bool{true, true, true, true}
var ep uint8 = NO_SQUARE
var BoardPly uint8 = 0
func SetStartingPosition() {
ep = NO_SQUARE
whiteToPlay = true
CastleRights[0] = true
CastleRights[1] = true
CastleRights[2] = true
CastleRights[3] = true
PieceArray[WP] = WP_STARTING_POSITIONS
PieceArray[WN] = WN_STARTING_POSITIONS
PieceArray[WB] = WB_STARTING_POSITIONS
PieceArray[WR] = WR_STARTING_POSITIONS
PieceArray[WQ] = WQ_STARTING_POSITION
PieceArray[WK] = WK_STARTING_POSITION
PieceArray[BP] = BP_STARTING_POSITIONS
PieceArray[BN] = BN_STARTING_POSITIONS
PieceArray[BB] = BB_STARTING_POSITIONS
PieceArray[BR] = BR_STARTING_POSITIONS
PieceArray[BQ] = BQ_STARTING_POSITION
PieceArray[BK] = BK_STARTING_POSITION
}
func PrintBoard() {
fmt.Println("Board:")
var boardArray [64]int = [64]int(FillBoardArray())
for rank := 0; rank < 8; rank++ {
fmt.Print(" ")
for file := 0; file < 8; file++ {
var square int = (rank * 8) + file
fmt.Printf("%c%c ", PieceColours[boardArray[square]], PieceNames[boardArray[square]])
}
fmt.Println()
}
fmt.Println()
fmt.Printf("White to play: %t\n", whiteToPlay)
fmt.Printf("Castle: %t %t %t %t\n", CastleRights[0], CastleRights[1], CastleRights[2], CastleRights[3])
fmt.Printf("ep: %d\n", ep)
fmt.Printf("ply: %d\n", BoardPly)
fmt.Println()
fmt.Println()
}
func PrintAllBitboards() {
for i := 0; i < 12; i++ {
fmt.Println(PieceArray[i])
}
}
func printBoardBasic() {
fmt.Println("PieceArray:")
for i := 0; i < 12; i++ {
fmt.Println(PieceArray[i])
}
fmt.Printf("white to play: %t\n", whiteToPlay)
fmt.Printf("Castle: %t %t %t %t\n", CastleRights[0], CastleRights[1], CastleRights[2], CastleRights[3])
fmt.Printf("ep: %d\n", ep)
fmt.Printf("ply %d\n", BoardPly)
}
func FillBoardArray() []int {
var boardArray [64]int
for i := 0; i < 64; i++ {
boardArray[i] = GetOccupiedIndex(i)
}
return boardArray[:]
}
func IsBoardArraySame(copy [12]uint64) bool {
for i := 0; i < 12; i++ {
if PieceArray[i] != copy[i] {
fmt.Println("ERROR piece not same: %d", i)
return false
}
}
return true
}
func PrintBitboard(bitboard uint64) {
for rank := 0; rank < 8; rank++ {
for file := 0; file < 8; file++ {
var square = (rank * 8) + file
if (bitboard & (ONE_U64 << square)) != 0 {
fmt.Print("X ")
continue
}
fmt.Print("_ ")
}
fmt.Println()
}
fmt.Println(bitboard)
}
func ResetBoard() {
for i := 0; i < 12; i++ {
PieceArray[i] = EMPTY_BITBOARD
}
whiteToPlay = true
for i := 0; i < 4; i++ {
CastleRights[i] = true
}
ep = NO_SQUARE
BoardPly = 0
}

68
Go/boardconstants.go Normal file
View File

@@ -0,0 +1,68 @@
package main
const A8 = 0
const B8 = 1
const C8 = 2
const D8 = 3
const E8 = 4
const F8 = 5
const G8 = 6
const H8 = 7
const A7 = 8
const B7 = 9
const C7 = 10
const D7 = 11
const E7 = 12
const F7 = 13
const G7 = 14
const H7 = 15
const A6 = 16
const B6 = 17
const C6 = 18
const D6 = 19
const E6 = 20
const F6 = 21
const G6 = 22
const H6 = 23
const A5 = 24
const B5 = 25
const C5 = 26
const D5 = 27
const E5 = 28
const F5 = 29
const G5 = 30
const H5 = 31
const A4 = 32
const B4 = 33
const C4 = 34
const D4 = 35
const E4 = 36
const F4 = 37
const G4 = 38
const H4 = 39
const A3 = 40
const B3 = 41
const C3 = 42
const D3 = 43
const E3 = 44
const F3 = 45
const G3 = 46
const H3 = 47
const A2 = 48
const B2 = 49
const C2 = 50
const D2 = 51
const E2 = 52
const F2 = 53
const G2 = 54
const H2 = 55
const A1 = 56
const B1 = 57
const C1 = 58
const D1 = 59
const E1 = 60
const F1 = 61
const G1 = 62
const H1 = 63
const NO_SQUARE = 65

25
Go/boardutils.go Normal file
View File

@@ -0,0 +1,25 @@
package main
func IsOccupied(bitboard uint64, square int) bool {
return (bitboard & SQUARE_BBS[square]) != 0
}
func GetOccupiedIndex(square int) int {
for i := 0; i < 12; i++ {
if IsOccupied(PieceArray[i], square) {
return i
}
}
return EMPTY
}
func OutOfBounds(move int) bool {
if move < 0 {
return true
}
if move > 63 {
return true
}
return false
}

241
Go/fen.go Normal file
View File

@@ -0,0 +1,241 @@
package main
const FEN_STARTING_POSITION = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
const FEN_STARTING_POSITION_BLACK = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1"
const FEN_STARTING_POSITION_ONLY_PAWNS = "4k3/pppppppp/8/8/8/8/PPPPPPPP/4K3 w KQkq - 0 1"
const FEN_STARTING_POSITION_EP_E4 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq e4 0 1"
const FEN_STARTING_POSITION_ONLY_KNIGHTS = "1n2k1n1/8/8/8/8/8/8/1N2K1N1 w - - 0 1"
const FEN_STARTING_POSITION_ONLY_KNIGHTS_BLACK = "1n2k1n1/8/8/8/8/8/8/1N2K1N1 b - - 0 1"
const FEN_TEST_KNIGHT_CAPTURES = "4k1n1/8/8/8/8/2n5/8/1N2K1N1 b - - 0 1"
const FEN_TEST_ONLY_KINGS = "8/8/3k4/8/3K4/8/8/8 w - - 0 1"
const FEN_TEST_ONLY_KNIGHTS = "8/8/3k4/8/3K4/8/8/8 w - - 0 1"
const FEN_TRICKY_WHITE = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - "
const FEN_TRICKY_BLACK = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R b KQkq - "
const FEN_TEST_EP = "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - "
const FEN_SCHOLARS_MATE = "r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR w KQkq - 4 4"
const FEN_FRIED_LIVER_BLACK = "r1bqkb1r/pppp1ppp/2n2n2/4p1N1/2B1P3/8/PPPP1PPP/RNBQK2R b KQkq - 5 4"
const FEN_FRIED_LIVER_WHITE = "r1bqkb1r/ppp2ppp/2np1n2/4p1N1/2B1P3/8/PPPP1PPP/RNBQK2R w KQkq - 0 5"
const FEN_KNIGHT_FORK = "rn1qk2r/pp3ppp/2p1pn2/6N1/8/1PP3P1/P4PBP/R2Q1RK1 w - - 0 1"
const FEN_MATE_0_TEST = "r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 1"
const FEN_MATE_4_TEST = "r7/q6p/2k1p3/2N1Q3/1ppP4/P2b4/1P4PP/K2R4 b - - 0 1"
const FEN_BONGCLOUD = "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPPKPPP/RNBQ1BNR b kq - 1 2"
const FEN_PERPETUAL = "6k1/5pp1/6p1/8/8/3Q3P/5PP1/6K1 w - - 0 1"
const FEN_MATE_3 = "r4rk1/ppp1q1p1/2n2pP1/8/8/2N5/PPP1BQ2/2KR3R w - - 0 1"
const FEN_BLACK_DOUBLE_CHECK = "4k3/8/8/8/4N3/8/8/4R1K1 w - - 0 1"
const (
Pieces = iota // 0
Side // 1
Castling // 2
EP
FiftyMove
PlyCount
)
func LoadFen(fen string) {
ResetBoard()
var bracketCount int = 0
var squareCount int = 0
var setting int = Pieces
var ep_file_index = -1
var ep_rank_index = -1
for character_index := 0; character_index < len(fen); character_index++ {
switch setting {
case Pieces:
Load_Fen_Sort_Pieces(&bracketCount, fen[character_index], &setting, &squareCount)
case Side:
Load_Fen_Sort_Side(&setting, fen[character_index])
case Castling:
Load_Fen_Sort_Castling(&setting, fen[character_index])
case EP:
Load_Fen_Sort_EP(&setting, fen[character_index], &ep_file_index, &ep_rank_index)
case FiftyMove:
Load_Fen_Sort_Fifty_Move(&setting, fen[character_index])
case PlyCount:
return
}
}
}
func Load_Fen_Sort_Pieces(bracketCount *int, character_in_fen byte, setting *int, squareCount *int) {
if *bracketCount == 7 && character_in_fen == ' ' {
*setting++
return
}
if *bracketCount > 7 {
*bracketCount = 0
}
if *squareCount > 7 {
*squareCount = 0
}
var square = (*bracketCount * 8) + *squareCount
switch character_in_fen {
case 'B':
PieceArray[WB] = SetBit(PieceArray[WB], square)
*squareCount++
case 'R':
PieceArray[WR] = SetBit(PieceArray[WR], square)
*squareCount++
case 'P':
PieceArray[WP] = SetBit(PieceArray[WP], square)
*squareCount++
case 'Q':
PieceArray[WQ] = SetBit(PieceArray[WQ], square)
*squareCount++
case 'K':
PieceArray[WK] = SetBit(PieceArray[WK], square)
*squareCount++
case 'N':
PieceArray[WN] = SetBit(PieceArray[WN], square)
*squareCount++
case 'b':
PieceArray[BB] = SetBit(PieceArray[BB], square)
*squareCount++
case 'p':
PieceArray[BP] = SetBit(PieceArray[BP], square)
*squareCount++
case 'q':
PieceArray[BQ] = SetBit(PieceArray[BQ], square)
*squareCount++
case 'r':
PieceArray[BR] = SetBit(PieceArray[BR], square)
*squareCount++
case 'n':
PieceArray[BN] = SetBit(PieceArray[BN], square)
*squareCount++
case 'k':
PieceArray[BK] = SetBit(PieceArray[BK], square)
*squareCount++
case '/':
*squareCount = 0
*bracketCount++
case '1':
*squareCount += 1
case '2':
*squareCount += 2
case '3':
*squareCount += 3
case '4':
*squareCount += 4
case '5':
*squareCount += 5
case '6':
*squareCount += 6
case '7':
*squareCount += 7
case '8':
*squareCount += 8
}
}
func Load_Fen_Sort_Side(setting *int, character_in_fen byte) {
switch character_in_fen {
case 'w':
whiteToPlay = true
case 'b':
whiteToPlay = false
case ' ':
*setting++
}
}
func Load_Fen_Sort_Castling(setting *int, character_in_fen byte) {
switch character_in_fen {
case 'K':
CastleRights[0] = true
case 'Q':
CastleRights[1] = true
case 'k':
CastleRights[2] = true
case 'q':
CastleRights[3] = true
case '-':
CastleRights[0] = false
CastleRights[1] = false
CastleRights[2] = false
CastleRights[3] = false
case ' ':
*setting++
}
}
func Load_Fen_Sort_EP(setting *int, character_in_fen byte, file_index *int, rank_index *int) {
if character_in_fen == '-' {
ep = NO_SQUARE
}
if character_in_fen == ' ' {
if *file_index != -1 && *rank_index != -1 {
ep = uint8(Convert_to_64(*file_index, *rank_index))
}
*setting++
}
switch character_in_fen {
case 'a':
*file_index = 0
return
case 'b':
*file_index = 1
return
case 'c':
*file_index = 2
return
case 'd':
*file_index = 3
return
case 'e':
*file_index = 4
return
case 'f':
*file_index = 5
return
case 'g':
*file_index = 6
return
case 'h':
*file_index = 7
return
}
switch character_in_fen {
case '1':
*rank_index = 7
return
case '2':
*rank_index = 6
return
case '3':
*rank_index = 5
return
case '4':
*rank_index = 4
return
case '5':
*rank_index = 3
return
case '6':
*rank_index = 2
return
case '7':
*rank_index = 1
return
case '8':
*rank_index = 0
return
}
}
func Convert_to_64(file int, rank int) int {
return (rank * 8) + file
}
func Load_Fen_Sort_Fifty_Move(setting *int, character_in_fen byte) {
if character_in_fen == ' ' {
*setting++
return
}
}

72
Go/generatemoves.go Normal file
View File

@@ -0,0 +1,72 @@
package main
func GetRookAttacksFast(startingSquare int, occupancy uint64) uint64 {
occupancy &= ROOK_MASKS[startingSquare]
occupancy *= ROOK_MAGIC_NUMBERS[startingSquare]
occupancy >>= 64 - ROOK_REL_BITS[startingSquare]
return ROOK_ATTACKS[startingSquare][occupancy]
}
func GetBishopAttacksFast(startingSquare int, occupancy uint64) uint64 {
occupancy &= BISHOP_MASKS[startingSquare]
occupancy *= BISHOP_MAGIC_NUMBERS[startingSquare]
occupancy >>= 64 - BISHOP_REL_BITS[startingSquare]
return BISHOP_ATTACKS[startingSquare][occupancy]
}
func Is_Square_Attacked_By_Black(square int, occupancy uint64) bool {
if (PieceArray[BP] & WHITE_PAWN_ATTACKS[square]) != 0 {
return true
}
if (PieceArray[BN] & KNIGHT_ATTACKS[square]) != 0 {
return true
}
if (PieceArray[BK] & KING_ATTACKS[square]) != 0 {
return true
}
var bishopAttacks = GetBishopAttacksFast(square, occupancy)
if (PieceArray[BB] & bishopAttacks) != 0 {
return true
}
if (PieceArray[BQ] & bishopAttacks) != 0 {
return true
}
var rookAttacks = GetRookAttacksFast(square, occupancy)
if (PieceArray[BR] & rookAttacks) != 0 {
return true
}
if (PieceArray[BQ] & rookAttacks) != 0 {
return true
}
return false
}
func Is_Square_Attacked_By_White(square int, occupancy uint64) bool {
if (PieceArray[WP] & BLACK_PAWN_ATTACKS[square]) != 0 {
return true
}
if (PieceArray[WN] & KNIGHT_ATTACKS[square]) != 0 {
return true
}
if (PieceArray[WK] & KING_ATTACKS[square]) != 0 {
return true
}
var bishopAttacks = GetBishopAttacksFast(square, occupancy)
if (PieceArray[WB] & bishopAttacks) != 0 {
return true
}
if (PieceArray[WQ] & bishopAttacks) != 0 {
return true
}
var rookAttacks = GetRookAttacksFast(square, occupancy)
if (PieceArray[WR] & rookAttacks) != 0 {
return true
}
if (PieceArray[WQ] & rookAttacks) != 0 {
return true
}
return false
}

1225
Go/moveconstants.go Normal file

File diff suppressed because one or more lines are too long

23
Go/piececonstants.go Normal file
View File

@@ -0,0 +1,23 @@
package main
const WP = 0
const WN = 1
const WB = 2
const WR = 3
const WQ = 4
const WK = 5
const BP = 6
const BN = 7
const BB = 8
const BR = 9
const BQ = 10
const BK = 11
const EMPTY = 12
var PieceNames = [13]byte{'P', 'N', 'B', 'R', 'Q', 'K', 'P', 'N', 'B', 'R', 'Q', 'K', '_'}
var PieceColours = [13]byte{'W', 'W', 'W', 'W', 'W', 'W', 'B', 'B', 'B', 'B', 'B', 'B', '_'}
const WHITE_START_INDEX = WP
const WHITE_END_INDEX = WK
const BLACK_START_INDEX = BP
const BLACK_END_INDEX = BK

29
Go/printing.go Normal file
View File

@@ -0,0 +1,29 @@
package main
import "fmt"
func PrintMoveNoNL(starting int, target_square int, tag int) { //starting
if OutOfBounds(starting) == true {
fmt.Printf("%d", starting)
} else {
fmt.Printf("%c", SQ_CHAR_X[starting])
fmt.Printf("%c", SQ_CHAR_Y[starting])
}
//target
if OutOfBounds(target_square) == true {
fmt.Printf("%d", target_square)
} else {
fmt.Printf("%c", SQ_CHAR_X[target_square])
fmt.Printf("%c", SQ_CHAR_Y[target_square])
}
if tag == TAG_BCaptureKnightPromotion || tag == TAG_BKnightPromotion || tag == TAG_WKnightPromotion || tag == TAG_WCaptureKnightPromotion {
fmt.Printf("n")
} else if tag == TAG_BCaptureRookPromotion || tag == TAG_BRookPromotion || tag == TAG_WRookPromotion || tag == TAG_WCaptureRookPromotion {
fmt.Printf("r")
} else if tag == TAG_BCaptureBishopPromotion || tag == TAG_BBishopPromotion || tag == TAG_WBishopPromotion || tag == TAG_WCaptureBishopPromotion {
fmt.Printf("b")
} else if tag == TAG_BCaptureQueenPromotion || tag == TAG_BQueenPromotion || tag == TAG_WQueenPromotion || tag == TAG_WCaptureQueenPromotion {
fmt.Printf("q")
}
}