-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame length distribution.py
More file actions
59 lines (47 loc) · 1.81 KB
/
game length distribution.py
File metadata and controls
59 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import chess.pgn
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from collections import Counter
def parse_pgn(file_path):
"""Parses a PGN file and extracts moves & metadata."""
with open(file_path) as pgn:
game = chess.pgn.read_game(pgn)
board = game.board()
moves = []
move_san_list = []
for move in game.mainline_moves():
moves.append(move.uci()) # Convert move to UCI format
move_san_list.append(board.san(move)) # Get move notation
board.push(move) # Apply move to board
return moves, move_san_list, len(moves) # Return moves, notation, and game length
def analyze_games(pgn_files):
"""Extracts move statistics from multiple PGN files."""
data = []
for file in pgn_files:
print(f"\n📂 Analyzing PGN File: {file}")
moves, move_san_list, game_length = parse_pgn(file)
data.append({
"PGN File": file,
"Total Moves": game_length,
"First Move": move_san_list[0] if move_san_list else "N/A",
"Most Frequent Move": Counter(move_san_list).most_common(1)[0][0] if move_san_list else "N/A"
})
return pd.DataFrame(data)
def plot_game_lengths(df):
"""Plot game length distribution."""
plt.figure(figsize=(8,5))
sns.barplot(x="PGN File", y="Total Moves", data=df)
plt.title("Game Length Distribution")
plt.xlabel("PGN File")
plt.ylabel("Number of Moves")
plt.show()
# ✅ Analyze multiple PGN files
pgn_files = ["game.pgn", "new_game.pgn"]
df = analyze_games(pgn_files)
# Save to CSV
df.to_csv("pgn_analysis.csv", index=False)
# Generate Game Length Graph
plot_game_lengths(df)
# Print Data for Reference
print(df)