|
|
import numpy as np |
|
|
import pandas as pd |
|
|
import csv |
|
|
from io import StringIO |
|
|
import time |
|
|
|
|
|
|
|
|
def check_win(board): |
|
|
""" |
|
|
Checks if the current player (who just made the last move) has won. |
|
|
board: 9-element list where 1=X, -1=O, 0=Empty |
|
|
""" |
|
|
winning_lines = [ |
|
|
(0, 1, 2), (3, 4, 5), (6, 7, 8), |
|
|
(0, 3, 6), (1, 4, 7), (2, 5, 8), |
|
|
(0, 4, 8), (2, 4, 6) |
|
|
] |
|
|
|
|
|
for line in winning_lines: |
|
|
if abs(board[line[0]] + board[line[1]] + board[line[2]]) == 3: |
|
|
return True |
|
|
|
|
|
def get_board_str(board): |
|
|
"""Converts the encoded board to a human-readable string.""" |
|
|
symbols = { 1: 'X', -1: 'O', 0: '_' } |
|
|
return ','.join(symbols[p] for p in board) |
|
|
|
|
|
|
|
|
def normalize_board(board): |
|
|
""" |
|
|
Finds the canonical (normalized) representation of a board state. |
|
|
Used for generating a consistent 'symmetry_id'. |
|
|
""" |
|
|
transformations = [ |
|
|
(0, 1, 2, 3, 4, 5, 6, 7, 8), |
|
|
(6, 3, 0, 7, 4, 1, 8, 5, 2), |
|
|
(8, 7, 6, 5, 4, 3, 2, 1, 0), |
|
|
(2, 5, 8, 1, 4, 7, 0, 3, 6), |
|
|
(6, 7, 8, 3, 4, 5, 0, 1, 2), |
|
|
(2, 1, 0, 5, 4, 3, 8, 7, 6), |
|
|
(8, 5, 2, 7, 4, 1, 6, 3, 0), |
|
|
(0, 3, 6, 1, 4, 7, 2, 5, 8) |
|
|
] |
|
|
|
|
|
canonical_board = tuple(board) |
|
|
|
|
|
|
|
|
for transform in transformations: |
|
|
transformed_board = tuple(board[i] for i in transform) |
|
|
if transformed_board < canonical_board: |
|
|
canonical_board = transformed_board |
|
|
|
|
|
return hash(canonical_board) |
|
|
|
|
|
|
|
|
|
|
|
GAME_COUNT = 0 |
|
|
|
|
|
FIELDNAMES = [ |
|
|
'game_id', 'step', 'player', 'board_state', 'next_move', |
|
|
'result', 'board_state_str', 'symmetry_id' |
|
|
] |
|
|
|
|
|
FILE_NAME = 'tic_tac_toe_dataset.csv' |
|
|
|
|
|
def generate_sequences(board, current_player, history, csv_writer): |
|
|
""" |
|
|
Recursively explores the Tic-Tac-Toe game tree. |
|
|
board: current board state (numpy array) |
|
|
current_player: 1 (X) or -1 (O) |
|
|
history: list of (board_state, next_move_index) tuples in the current game |
|
|
csv_writer: The writer object to output rows directly to the file |
|
|
""" |
|
|
global GAME_COUNT |
|
|
|
|
|
|
|
|
is_win = check_win(board) |
|
|
is_draw = not is_win and np.all(board != 0) |
|
|
|
|
|
if is_win or is_draw: |
|
|
|
|
|
GAME_COUNT += 1 |
|
|
|
|
|
|
|
|
if is_win: |
|
|
|
|
|
winner = 'X' if current_player == -1 else 'O' |
|
|
result = f'{winner} Win' |
|
|
else: |
|
|
result = 'Draw' |
|
|
|
|
|
|
|
|
for i, (prev_board, move_idx) in enumerate(history): |
|
|
|
|
|
|
|
|
player_char = 'X' if (i + 1) % 2 != 0 else 'O' |
|
|
|
|
|
row = { |
|
|
'game_id': GAME_COUNT, |
|
|
'step': i + 1, |
|
|
'player': player_char, |
|
|
'board_state': [int(x) for x in board.tolist()], |
|
|
'next_move': move_idx, |
|
|
'result': result, |
|
|
'board_state_str': get_board_str(prev_board), |
|
|
'symmetry_id': normalize_board(prev_board) |
|
|
} |
|
|
csv_writer.writerow(row) |
|
|
return |
|
|
|
|
|
empty_spots = np.where(board == 0)[0] |
|
|
|
|
|
for move_index in empty_spots: |
|
|
|
|
|
current_move_data = (board.copy(), move_index) |
|
|
|
|
|
|
|
|
new_board = board.copy() |
|
|
new_board[move_index] = current_player |
|
|
|
|
|
|
|
|
generate_sequences( |
|
|
board=new_board, |
|
|
current_player=-current_player, |
|
|
history=history + [current_move_data], |
|
|
csv_writer=csv_writer |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GAME_COUNT = 0 |
|
|
CSV_BUFFER = StringIO() |
|
|
csv_writer = csv.DictWriter(CSV_BUFFER, fieldnames=FIELDNAMES) |
|
|
csv_writer.writeheader() |
|
|
|
|
|
initial_board = np.zeros(9, dtype=int) |
|
|
start_time = time.time() |
|
|
|
|
|
print("🚀 Starting dataset generation (this may take a few seconds)...") |
|
|
|
|
|
generate_sequences( |
|
|
board=initial_board, |
|
|
current_player=1, |
|
|
history=[], |
|
|
csv_writer=csv_writer |
|
|
) |
|
|
|
|
|
end_time = time.time() |
|
|
print(f"✅ Generation complete in {end_time - start_time:.2f} seconds.") |
|
|
print(f"Total distinct game sequences found: **{GAME_COUNT}**") |
|
|
|
|
|
|
|
|
CSV_BUFFER.seek(0) |
|
|
df = pd.read_csv(CSV_BUFFER) |
|
|
|
|
|
print(f"Total move entries (rows) generated: **{len(df)}**") |
|
|
|
|
|
|
|
|
df.to_csv('tic_tac_toe_dataset.csv', index=False) |
|
|
print("\n💾 Dataset saved to **tic_tac_toe_dataset.csv**") |