2023-08-06 01:59:30 +00:00
|
|
|
use rand::rngs::SmallRng;
|
|
|
|
use rand::SeedableRng;
|
2023-08-10 03:00:14 +00:00
|
|
|
use crate::board::{Board, Letter};
|
2023-08-06 01:59:30 +00:00
|
|
|
use crate::constants::{standard_tile_pool, TRAY_LENGTH};
|
2023-08-13 20:08:37 +00:00
|
|
|
use crate::dictionary::{Dictionary, DictionaryImpl};
|
2023-08-06 01:59:30 +00:00
|
|
|
use crate::player_interaction::ai::Difficulty;
|
|
|
|
use crate::player_interaction::Tray;
|
|
|
|
|
|
|
|
pub enum Player {
|
|
|
|
Human(String),
|
|
|
|
AI{
|
|
|
|
name: String,
|
|
|
|
difficulty: Difficulty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PlayerState {
|
|
|
|
player: Player,
|
|
|
|
score: u32,
|
|
|
|
tray: Tray
|
|
|
|
}
|
|
|
|
|
2023-08-13 20:08:37 +00:00
|
|
|
pub struct Game{
|
2023-08-10 03:00:14 +00:00
|
|
|
tile_pool: Vec<Letter>,
|
|
|
|
board: Board,
|
2023-08-06 01:59:30 +00:00
|
|
|
player: PlayerState,
|
2023-08-13 20:08:37 +00:00
|
|
|
dictionary: DictionaryImpl,
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Problem - I want to provide a UI to the player
|
|
|
|
// Ideally they would get some kind of 'tray' object with methods to use and run
|
|
|
|
// However I want the main game state to live in Rust, not in JS.
|
|
|
|
// Does this mean I provide Rc<RefCell<Tray>>?
|
|
|
|
|
|
|
|
// Other option - what if I just have one Game object that exposes all methods.
|
|
|
|
// At no point do they get a Tray reference that auto-updates, they need to handle that
|
|
|
|
// I just provide read-only JSON of everything, and they call the methods for updates
|
|
|
|
// This will later work out well when I build it out as an API for multiplayer.
|
|
|
|
|
|
|
|
impl Game {
|
2023-08-13 20:08:37 +00:00
|
|
|
pub fn new(seed: u64, dictionary_text: &str) -> Self {
|
2023-08-06 01:59:30 +00:00
|
|
|
let mut rng = SmallRng::seed_from_u64(seed);
|
|
|
|
|
|
|
|
let mut letters = standard_tile_pool(Some(&mut rng));
|
|
|
|
let mut tray = Tray::new(TRAY_LENGTH);
|
|
|
|
tray.fill(&mut letters);
|
|
|
|
|
|
|
|
|
|
|
|
let player = PlayerState {
|
|
|
|
player: Player::Human("Joel".to_string()),
|
|
|
|
score: 0,
|
|
|
|
tray,
|
|
|
|
};
|
|
|
|
|
|
|
|
Game {
|
2023-08-10 03:00:14 +00:00
|
|
|
tile_pool: letters,
|
|
|
|
board: Board::new(),
|
2023-08-06 01:59:30 +00:00
|
|
|
player,
|
2023-08-13 20:08:37 +00:00
|
|
|
dictionary: DictionaryImpl::create_from_str(dictionary_text)
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_tray(&self) -> &Tray {
|
|
|
|
&self.player.tray
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_tray_mut(&mut self) -> &mut Tray {
|
|
|
|
&mut self.player.tray
|
|
|
|
}
|
2023-08-10 03:00:14 +00:00
|
|
|
|
|
|
|
pub fn get_board(&self) -> &Board {&self.board}
|
|
|
|
|
2023-08-13 20:08:37 +00:00
|
|
|
pub fn get_board_mut(&mut self) -> &mut Board {
|
|
|
|
&mut self.board
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_dictionary(&self) -> &DictionaryImpl {
|
|
|
|
&self.dictionary
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|