<commit_before>global ai, aiMove
global aiPlayer
global curPlayer
global whitePawns
global whiteBishops
global whiteKnights
global whiteCastles
global whiteQueens
global whiteKing
global blackPawns
global blackBishops
global blackKnights
global blackCastles
global blackQueens
global blackKing

global rightEdge
global leftEdge
global topEdge
global bottomEdge

global fillWhiteBoard
global fillBlackBoard
global removePiece
global boardBuffer
global whiteBoard
global blackBoard
global pieceDie

global kingMove

global knightTop, knightBottom, knightLeft, knightRight
global firstWhitePawnRow, fistBlackPawnRow

extern print
extern printSpace
extern printBitMap
extern pushGame
extern popGame
extern pushWinningMove
extern popWinningMove
;piece move procedures
extern pawnMoves
extern bishopMoves
extern knightMoves
extern castleMoves

extern lastMovA

section .data
	aiDepth		dq	5	;depth for negaMax tree
	aiPlayer	dq 	1	;if ai is black/white 1 = white -1 = black

	curDepth	dq 	0	;used by negaMax during loop
	curPlayer	dq 	0
	curScore	dq 	0	;used by negamax for score keepin

	numMovs	 	dq	0	;num of moves for a certain piece
	pieceDie	dq	0	;1 if piece died
	noGoodMove	dq	0	;flag is on ai doens't find good move

	blackBoard 	dq 	0x0	;used to store black pieces when calc
	whiteBoard 	dq 	0x0	;used to store white pieces when calc

	boardBuffer 	dq	0x0	;used as temporary move storage
	;used to check if piece is on an edge we fill bits where valid area
	leftEdge	dq	0xFEFEFEFEFEFEFEFE
	rightEdge	dq	0x7F7F7F7F7F7F7F7F
	topEdge		dq	0xFFFFFFFFFFFFFF
	bottomEdge	dq	0xFFFFFFFFFFFFFF00

	knightTop	dq	0xFFFFFFFFFFFF
	knightBottom dq	0xFFFFFFFFFFFF0000
	knightLeft	dq	0xFCFCFCFCFCFCFCFC
	knightRight	dq	0x3F3F3F3F3F3F3F3F

	firstWhitePawnRow dq 0xff00
	fistBlackPawnRow dq 0xff000000000000

	;game helper flags
	kingMove	dq	0		;if it is a king move

					;bitboards for white
	whitePawns 		dq 	0xff00
	whiteBishops 	dq	0x24
	whiteKnights 	dq 	0x42
	whiteCastles 	dq 	0x81
	whiteQueens 	dq 	0x8
	whiteKing 		dq 	0x10
					;bitboards for black
	blackPawns  	dq 	0xff000000000000
	blackBishops 	dq 	0x2400000000000000
	blackKnights 	dq 	0x4200000000000000
	blackCastles	dq	0x8100000000000000
	blackQueens 	dq 	0x800000000000000
	blackKing    	dq	0x1000000000000000

section .text
;--------------------------------------
;NegaMax Procedure
;This is the backbone of our AI
;analyzes all best moves to "AIdepth"
;for each player choose best for itself
;--------------------------------------
;Upper negaMax gets score for every current move on one side
ai:
	mov rdx, qWord [aiPlayer]
	mov qWord [curPlayer], rdx
	mov qWord [curScore], -300
	call pushGame
	call getMoves
loopai:
	call popGame
	call pushGame
	mov rcx, qWord [aiDepth]
	inc rcx						;to counter initial dec in ai
	push rax					;loop every piece counter
	push qWord [curScore]
	imul rdx, -1
	push rdx
	push rcx
	call NegaMax				;get best move score
	call popGame				;get game state back
	pop rcx
	pop rdx
	pop qWord [curScore]
	imul rax, -1
	imul rdx, -1				;change player
	cmp rax, qWord [curScore]
	jng finalScore
	mov qWord [curScore], rax
	call print
	call pushWinningMove
finalScore:
	pop rax
	dec rax
	cmp rax, 0					;check every piece?
	jne loopai
	;if checkMate don't do anything
	cmp qWord [curScore], -300
	je checkMate
	cmp qWord [curScore], 300
	je checkMate
	call popWinningMove			;make the move
	ret
checkMate:
	call popGame
	ret

;this is our AI, nega max recursive algorithm
NegaMax:
	push rbp					;save base pointer

	mov rbp, rsp				;get pushed parameter
	add rbp, 16
	mov rcx, [rbp]				;move depth parameter to rcx
	add rbp, 8
	mov rdx, [rbp]

	cmp rcx, 0
	jle doneNegaMax

	;minimum score
	push rdx
	mov rdx, -300		;base score
	mov qWord [curScore], rdx
	pop rdx

	mov qWord [curPlayer], rdx	;move cur player

	call getMoves		;get all posible movs for that player
	cmp rax, 0
	je doneNegaMax
negaLoop:
	;for every move
	call popGame

	push rax		;loop counter
	push rbx
	push rdx
	push qWord [curScore]   ;save score
	imul rdx, -1			;switch player
	dec rcx
	push rdx
	push rcx					;push parameter
	call NegaMax
	pop rcx
	pop rdx
	pop qWord [curScore]	;get score back
	pop rdx
	imul rax, -1
	cmp rax, qWord [curScore]	;is nex score greater?<commit_msg>increased max score so ai can see checkmate<commit_after>global ai, aiMove
global aiPlayer
global curPlayer
global whitePawns
global whiteBishops
global whiteKnights
global whiteCastles
global whiteQueens
global whiteKing
global blackPawns
global blackBishops
global blackKnights
global blackCastles
global blackQueens
global blackKing

global rightEdge
global leftEdge
global topEdge
global bottomEdge

global fillWhiteBoard
global fillBlackBoard
global removePiece
global boardBuffer
global whiteBoard
global blackBoard
global pieceDie

global kingMove

global knightTop, knightBottom, knightLeft, knightRight
global firstWhitePawnRow, fistBlackPawnRow

extern print
extern printSpace
extern printBitMap
extern pushGame
extern popGame
extern pushWinningMove
extern popWinningMove
;piece move procedures
extern pawnMoves
extern bishopMoves
extern knightMoves
extern castleMoves

extern lastMovA

section .data
	aiDepth		dq	5	;depth for negaMax tree
	aiPlayer	dq 	1	;if ai is black/white 1 = white -1 = black

	curDepth	dq 	0	;used by negaMax during loop
	curPlayer	dq 	0
	curScore	dq 	0	;used by negamax for score keepin

	numMovs	 	dq	0	;num of moves for a certain piece
	pieceDie	dq	0	;1 if piece died
	noGoodMove	dq	0	;flag is on ai doens't find good move

	blackBoard 	dq 	0x0	;used to store black pieces when calc
	whiteBoard 	dq 	0x0	;used to store white pieces when calc

	boardBuffer 	dq	0x0	;used as temporary move storage
	;used to check if piece is on an edge we fill bits where valid area
	leftEdge	dq	0xFEFEFEFEFEFEFEFE
	rightEdge	dq	0x7F7F7F7F7F7F7F7F
	topEdge		dq	0xFFFFFFFFFFFFFF
	bottomEdge	dq	0xFFFFFFFFFFFFFF00

	knightTop	dq	0xFFFFFFFFFFFF
	knightBottom dq	0xFFFFFFFFFFFF0000
	knightLeft	dq	0xFCFCFCFCFCFCFCFC
	knightRight	dq	0x3F3F3F3F3F3F3F3F

	firstWhitePawnRow dq 0xff00
	fistBlackPawnRow dq 0xff000000000000

	;game helper flags
	kingMove	dq	0		;if it is a king move

					;bitboards for white
	whitePawns 		dq 	0xff00
	whiteBishops 	dq	0x24
	whiteKnights 	dq 	0x42
	whiteCastles 	dq 	0x81
	whiteQueens 	dq 	0x8
	whiteKing 		dq 	0x10
					;bitboards for black
	blackPawns  	dq 	0xff000000000000
	blackBishops 	dq 	0x2400000000000000
	blackKnights 	dq 	0x4200000000000000
	blackCastles	dq	0x8100000000000000
	blackQueens 	dq 	0x800000000000000
	blackKing    	dq	0x1000000000000000

section .text
;--------------------------------------
;NegaMax Procedure
;This is the backbone of our AI
;analyzes all best moves to "AIdepth"
;for each player choose best for itself
;--------------------------------------
;Upper negaMax gets score for every current move on one side
ai:
	mov rdx, qWord [aiPlayer]
	mov qWord [curPlayer], rdx
	mov qWord [curScore], -3000
	call pushGame
	call getMoves
loopai:
	call popGame
	call pushGame
	mov rcx, qWord [aiDepth]
	inc rcx						;to counter initial dec in ai
	push rax					;loop every piece counter
	push qWord [curScore]
	imul rdx, -1
	push rdx
	push rcx
	call NegaMax				;get best move score
	call popGame				;get game state back
	pop rcx
	pop rdx
	pop qWord [curScore]
	imul rax, -1
	imul rdx, -1				;change player
	cmp rax, qWord [curScore]
	jng finalScore
	mov qWord [curScore], rax
	call print
	call pushWinningMove
finalScore:
	pop rax
	dec rax
	cmp rax, 0					;check every piece?
	jne loopai
	;if checkMate don't do anything
	cmp qWord [curScore], -3000
	je checkMate
	cmp qWord [curScore], 3000
	je checkMate
	call popWinningMove			;make the move
	ret
checkMate:
	call popGame
	ret

;this is our AI, nega max recursive algorithm
NegaMax:
	push rbp					;save base pointer

	mov rbp, rsp				;get pushed parameter
	add rbp, 16
	mov rcx, [rbp]				;move depth parameter to rcx
	add rbp, 8
	mov rdx, [rbp]

	cmp rcx, 0
	jle doneNegaMax

	;minimum score
	push rdx
	mov rdx, -3000		;base score
	mov qWord [curScore], rdx
	pop rdx

	mov qWord [curPlayer], rdx	;move cur player

	call getMoves		;get all posible movs for that player
	cmp rax, 0
	je doneNegaMax
negaLoop:
	;for every move
	call popGame

	push rax		;loop counter
	push rbx
	push rdx
	push qWord [curScore]   ;save score
	imul rdx, -1			;switch player
	dec rcx
	push rdx
	push rcx					;push parameter
	call NegaMax
	pop rcx
	pop rdx
	pop qWord [curScore]	;get score back
	pop rdx
	imul rax, -1
	cmp rax, qWord [curScore]	;is nex score greater?
{'fraction_non_alphanumeric': 0.05087140838436175, 'fraction_numerical': 0.047809703250117755, 'mean_word_length': 3.676762114537445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 22, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4746792', 'n_tokens_mistral': 3583, 'n_tokens_neox': 3119, 'n_words': 1178}