124 lines
No EOL
3.3 KiB
Rust
124 lines
No EOL
3.3 KiB
Rust
use rand::prelude::SliceRandom;
|
|
use rand::rngs::SmallRng;
|
|
use rand::SeedableRng;
|
|
use crate::board::{Board, Letter};
|
|
use crate::constants::{standard_tile_pool, TRAY_LENGTH};
|
|
use crate::dictionary::{Dictionary, DictionaryImpl};
|
|
use crate::player_interaction::ai::Difficulty;
|
|
use crate::player_interaction::Tray;
|
|
|
|
pub enum Player {
|
|
Human(String),
|
|
AI{
|
|
name: String,
|
|
difficulty: Difficulty,
|
|
}
|
|
}
|
|
|
|
impl Player {
|
|
|
|
pub fn get_name(&self) -> &str {
|
|
match &self {
|
|
Player::Human(name) => {name}
|
|
Player::AI { name, .. } => {name}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct PlayerState {
|
|
pub player: Player,
|
|
pub score: u32,
|
|
pub tray: Tray
|
|
}
|
|
|
|
pub struct Game{
|
|
pub tile_pool: Vec<Letter>,
|
|
board: Board,
|
|
pub player_states: Vec<PlayerState>,
|
|
dictionary: DictionaryImpl,
|
|
}
|
|
|
|
// 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 {
|
|
pub fn new(seed: u64, dictionary_text: &str, mut player_names: Vec<String>) -> Self {
|
|
let mut rng = SmallRng::seed_from_u64(seed);
|
|
|
|
let mut letters = standard_tile_pool(Some(&mut rng));
|
|
|
|
player_names.shuffle(&mut rng);
|
|
let player_states: Vec<PlayerState> = player_names.iter()
|
|
.map(|name| {
|
|
let mut tray = Tray::new(TRAY_LENGTH);
|
|
tray.fill(&mut letters);
|
|
PlayerState {
|
|
player: Player::Human(name.clone()),
|
|
score: 0,
|
|
tray,
|
|
}
|
|
})
|
|
.collect();
|
|
|
|
Game {
|
|
tile_pool: letters,
|
|
board: Board::new(),
|
|
player_states,
|
|
dictionary: DictionaryImpl::create_from_str(dictionary_text)
|
|
}
|
|
}
|
|
|
|
pub fn get_player_state(&self, name: &str) -> Option<&PlayerState> {
|
|
self.player_states.iter()
|
|
.filter(|state| state.player.get_name().eq(name))
|
|
.nth(0)
|
|
|
|
}
|
|
|
|
pub fn get_player_state_mut(&mut self, name: &str) -> Option<&mut PlayerState> {
|
|
self.player_states.iter_mut()
|
|
.filter(|state| state.player.get_name().eq(name))
|
|
.nth(0)
|
|
|
|
}
|
|
|
|
pub fn get_tray(&self, name: &str) -> Option<&Tray> {
|
|
let player = self.get_player_state(name)?;
|
|
|
|
Some(&player.tray)
|
|
|
|
}
|
|
|
|
pub fn get_tray_mut(&mut self, name: &str) -> Option<&mut Tray> {
|
|
let player = self.get_player_state_mut(name)?;
|
|
|
|
Some(&mut player.tray)
|
|
|
|
}
|
|
|
|
pub fn get_board(&self) -> &Board {&self.board}
|
|
|
|
pub fn set_board(&mut self, new_board: Board) {
|
|
self.board = new_board;
|
|
}
|
|
|
|
pub fn fill_trays(&mut self){
|
|
for state in self.player_states.iter_mut() {
|
|
let tray = &mut state.tray;
|
|
tray.fill(&mut self.tile_pool);
|
|
}
|
|
}
|
|
|
|
pub fn get_dictionary(&self) -> &DictionaryImpl {
|
|
&self.dictionary
|
|
}
|
|
|
|
|
|
} |