Skip to content

Commit 09192d6

Browse files
committed
refactor: Move perft function and improvement of it
1 parent aadad73 commit 09192d6

File tree

3 files changed

+55
-66
lines changed

3 files changed

+55
-66
lines changed

Sources/Reldus/Perft.swift

-49
This file was deleted.

Sources/Reldus/UCI.swift

+55-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class UCI {
2929
handleUCINewGame()
3030
case "position":
3131
handlePosition(command: command)
32+
case "goperft":
33+
handlePerft(command: command.joined(separator: " "))
3234
case "go":
3335
handleGo(command: command.joined(separator: " "), board: board)
3436
case "quit":
@@ -53,6 +55,59 @@ class UCI {
5355
private func handleUCINewGame() {
5456
board = ChessBoard(fen: startPosFen)
5557
}
58+
59+
func handlePerft(command: String) {
60+
let board = ChessBoard(fen: startPosFen)
61+
let parts = command.split(separator: " ")
62+
guard parts.count > 1, let depth = Int(parts[1]) else {
63+
print("Invalid perft command")
64+
return
65+
}
66+
67+
let startTime = Date()
68+
let totalNodes = perft(board: board, depth: depth)
69+
let endTime = Date()
70+
71+
let timeTaken = endTime.timeIntervalSince(startTime)
72+
let nodesPerSecond = Double(totalNodes) / timeTaken
73+
74+
print("Total: \(totalNodes)")
75+
print(String(format: "Time: %.2f secs", timeTaken))
76+
print(String(format: "NodesPerSec: %.2f NPS", nodesPerSecond))
77+
}
78+
79+
private func perft(board: ChessBoard, depth: Int) -> Int {
80+
var totalNodes = 0
81+
let moves = MoveGenerator.generateMoves(for: board, color: board.turn)
82+
83+
for move in moves {
84+
board.makeMove(move)
85+
let nodes = perftRecursive(board: board, depth: depth - 1)
86+
board.undoMove(move)
87+
88+
print("\(move.toUCI()) - \(nodes)")
89+
totalNodes += nodes
90+
}
91+
92+
return totalNodes
93+
}
94+
95+
private func perftRecursive(board: ChessBoard, depth: Int) -> Int {
96+
if depth == 0 {
97+
return 1
98+
}
99+
100+
var nodes = 0
101+
let moves = MoveGenerator.generateMoves(for: board, color: board.turn)
102+
103+
for move in moves {
104+
board.makeMove(move)
105+
nodes += perftRecursive(board: board, depth: depth - 1)
106+
board.undoMove(move)
107+
}
108+
109+
return nodes
110+
}
56111

57112
private func handlePosition(command: [Substring]) {
58113
guard command.count > 1 else { return }
@@ -161,7 +216,6 @@ class UCI {
161216
moveValue = Search.minimax(board: boardCopy, depth: searchDepth - 1, maximizingPlayer: true)
162217
} else {
163218
moveValue = Search.minimax(board: boardCopy, depth: searchDepth - 1, maximizingPlayer: false)
164-
165219
}
166220
board.undoMove(move)
167221

Sources/Reldus/main.swift

-16
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,5 @@ import Foundation
1212
// print(chessBoard.getFEN())
1313

1414

15-
16-
func testPerft() {
17-
let fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
18-
let board = ChessBoard(fen: fen)
19-
let depth = 1
20-
let nodes = perft(board: board, depth: depth)
21-
22-
print("Perft \(depth) nodes: \(nodes.nodes)")
23-
print("Captures: \(nodes.captures)")
24-
print("En passants: \(nodes.enPassants)")
25-
print("Castles: \(nodes.castles)")
26-
print("Promotions: \(nodes.promotions)")
27-
print("Checks: \(nodes.checks)")
28-
29-
30-
}
3115
let uci = UCI()
3216
uci.start()

0 commit comments

Comments
 (0)