rusty2048_core/
lib.rs

1//! Core game logic for Rusty2048
2//!
3//! This module provides the fundamental game mechanics including:
4//! - Game board representation
5//! - Move validation and execution
6//! - Score calculation
7//! - Game state management
8//! - Random number generation with seed support
9
10pub mod ai;
11pub mod board;
12pub mod error;
13pub mod game;
14pub mod replay;
15pub mod rng;
16pub mod score;
17pub mod stats;
18
19pub use ai::{AIAlgorithm, AIGameController, AIPlayer};
20pub use board::Board;
21pub use error::{GameError, GameResult};
22pub use game::{Direction, Game, GameState};
23pub use replay::{
24    ReplayData, ReplayManager, ReplayMetadata, ReplayMove, ReplayPlayer, ReplayRecorder,
25};
26pub use rng::GameRng;
27pub use score::Score;
28pub use stats::{create_session_stats, GameSessionStats, StatisticsManager, StatisticsSummary};
29
30/// Get current time as Unix timestamp
31pub fn get_current_time() -> u64 {
32    #[cfg(target_arch = "wasm32")]
33    {
34        // For WASM, return 0 for now
35        0
36    }
37    #[cfg(not(target_arch = "wasm32"))]
38    {
39        use std::time::{SystemTime, UNIX_EPOCH};
40        SystemTime::now()
41            .duration_since(UNIX_EPOCH)
42            .unwrap_or_default()
43            .as_secs()
44    }
45}
46
47/// Game configuration
48#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
49pub struct GameConfig {
50    /// Board size (default: 4)
51    pub board_size: usize,
52    /// Target score to win (default: 2048)
53    pub target_score: u32,
54    /// Whether to allow undo (default: true)
55    pub allow_undo: bool,
56    /// Random seed for reproducible games
57    pub seed: Option<u64>,
58}
59
60impl Default for GameConfig {
61    fn default() -> Self {
62        Self {
63            board_size: 4,
64            target_score: 2048,
65            allow_undo: true,
66            seed: None,
67        }
68    }
69}
70
71/// Game statistics
72#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Default)]
73pub struct GameStats {
74    /// Current score
75    pub score: u32,
76    /// Best score achieved
77    pub best_score: u32,
78    /// Number of moves made
79    pub moves: u32,
80    /// Game duration in seconds
81    pub duration: u64,
82    /// Whether the game is won
83    pub won: bool,
84    /// Whether the game is over
85    pub game_over: bool,
86}