2023-08-16 02:35:23 +00:00
|
|
|
use rand::prelude::SliceRandom;
|
2023-08-06 01:59:30 +00:00
|
|
|
use rand::rngs::SmallRng;
|
|
|
|
use rand::SeedableRng;
|
2023-08-19 01:14:27 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tsify::Tsify;
|
|
|
|
use crate::board::{Board, Coordinates, 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 02:35:23 +00:00
|
|
|
impl Player {
|
|
|
|
|
|
|
|
pub fn get_name(&self) -> &str {
|
|
|
|
match &self {
|
|
|
|
Player::Human(name) => {name}
|
|
|
|
Player::AI { name, .. } => {name}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 01:59:30 +00:00
|
|
|
pub struct PlayerState {
|
2023-08-16 02:35:23 +00:00
|
|
|
pub player: Player,
|
|
|
|
pub score: u32,
|
2023-08-17 02:37:32 +00:00
|
|
|
pub tray: Tray
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 01:14:27 +00:00
|
|
|
#[derive(Deserialize, Tsify, Copy, Clone)]
|
|
|
|
#[tsify(from_wasm_abi)]
|
|
|
|
pub struct PlayedTile {
|
|
|
|
index: usize,
|
|
|
|
character: Option<char>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Tsify)]
|
|
|
|
#[tsify(from_wasm_abi)]
|
|
|
|
pub struct WordResult {
|
|
|
|
word: String,
|
|
|
|
score: u32,
|
|
|
|
}
|
|
|
|
|
2023-08-23 03:52:03 +00:00
|
|
|
pub struct PlayerStates(pub Vec<PlayerState>);
|
|
|
|
impl PlayerStates {
|
|
|
|
pub fn get_player_state(&self, name: &str) -> Option<&PlayerState> {
|
|
|
|
self.0.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.0.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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 20:08:37 +00:00
|
|
|
pub struct Game{
|
2023-08-17 02:37:32 +00:00
|
|
|
pub tile_pool: Vec<Letter>,
|
2023-08-23 03:52:03 +00:00
|
|
|
rng: SmallRng,
|
2023-08-10 03:00:14 +00:00
|
|
|
board: Board,
|
2023-08-23 03:52:03 +00:00
|
|
|
pub player_states: PlayerStates,
|
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-16 02:35:23 +00:00
|
|
|
pub fn new(seed: u64, dictionary_text: &str, mut player_names: Vec<String>) -> 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));
|
|
|
|
|
2023-08-16 02:35:23 +00:00
|
|
|
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();
|
2023-08-06 01:59:30 +00:00
|
|
|
|
|
|
|
Game {
|
2023-08-10 03:00:14 +00:00
|
|
|
tile_pool: letters,
|
2023-08-23 03:52:03 +00:00
|
|
|
rng,
|
2023-08-10 03:00:14 +00:00
|
|
|
board: Board::new(),
|
2023-08-23 03:52:03 +00:00
|
|
|
player_states: PlayerStates(player_states),
|
2023-08-13 20:08:37 +00:00
|
|
|
dictionary: DictionaryImpl::create_from_str(dictionary_text)
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 02:35:23 +00:00
|
|
|
|
2023-08-10 03:00:14 +00:00
|
|
|
|
|
|
|
pub fn get_board(&self) -> &Board {&self.board}
|
|
|
|
|
2023-08-17 02:37:32 +00:00
|
|
|
pub fn set_board(&mut self, new_board: Board) {
|
|
|
|
self.board = new_board;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fill_trays(&mut self){
|
2023-08-23 03:52:03 +00:00
|
|
|
for state in self.player_states.0.iter_mut() {
|
2023-08-17 02:37:32 +00:00
|
|
|
let tray = &mut state.tray;
|
|
|
|
tray.fill(&mut self.tile_pool);
|
|
|
|
}
|
2023-08-13 20:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_dictionary(&self) -> &DictionaryImpl {
|
|
|
|
&self.dictionary
|
|
|
|
}
|
|
|
|
|
2023-08-19 01:14:27 +00:00
|
|
|
pub fn receive_play(&mut self, player: &str, tray_tile_locations: Vec<Option<PlayedTile>>, commit_move: bool) -> Result<Vec<WordResult>, String> {
|
|
|
|
|
|
|
|
let mut board_instance = self.get_board().clone();
|
2023-08-23 03:52:03 +00:00
|
|
|
let mut tray = self.player_states.get_tray(player).unwrap().clone();
|
2023-08-19 01:14:27 +00:00
|
|
|
|
|
|
|
let mut played_letters: Vec<(Letter, Coordinates)> = Vec::new();
|
|
|
|
for (i, played_tile) in tray_tile_locations.iter().enumerate() {
|
|
|
|
if played_tile.is_some() {
|
|
|
|
let played_tile = played_tile.unwrap();
|
|
|
|
let mut letter: Letter = tray.letters.get(i).unwrap().unwrap();
|
|
|
|
*tray.letters.get_mut(i).unwrap() = None;
|
|
|
|
|
|
|
|
let coord = Coordinates::new_from_index(played_tile.index);
|
|
|
|
if letter.is_blank {
|
|
|
|
match played_tile.character {
|
|
|
|
None => {
|
|
|
|
panic!("You can't play a blank character without providing a letter value")
|
|
|
|
}
|
|
|
|
Some(x) => {
|
|
|
|
// TODO - I should check that the character is a valid letter
|
|
|
|
letter.text = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
played_letters.push((letter, coord));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
board_instance.receive_play(played_letters)?;
|
|
|
|
|
|
|
|
|
|
|
|
let x = board_instance.calculate_scores(self.get_dictionary())?;
|
|
|
|
|
|
|
|
let words: Vec<WordResult> = x.0.iter()
|
|
|
|
.map(|(word, score)| {
|
|
|
|
WordResult {
|
|
|
|
word: word.to_string(),
|
|
|
|
score: *score
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if commit_move {
|
2023-08-23 03:52:03 +00:00
|
|
|
let mut player_state = self.player_states.get_player_state_mut(player).unwrap();
|
2023-08-19 01:14:27 +00:00
|
|
|
player_state.score += x.1;
|
|
|
|
player_state.tray = tray;
|
|
|
|
|
|
|
|
board_instance.fix_tiles();
|
|
|
|
self.set_board(board_instance);
|
|
|
|
self.fill_trays()
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(words)
|
|
|
|
}
|
2023-08-13 20:08:37 +00:00
|
|
|
|
2023-08-23 03:52:03 +00:00
|
|
|
pub fn exchange_tiles(&mut self, player: &str, tray_tile_locations: Vec<bool>) -> Result<Tray, String> {
|
|
|
|
let tray = match self.player_states.get_tray_mut(player) {
|
|
|
|
None => {return Err(format!("Player {} not found", player))}
|
|
|
|
Some(x) => {x}
|
|
|
|
};
|
|
|
|
|
|
|
|
if tray.letters.len() != tray_tile_locations.len() {
|
|
|
|
return Err("Incoming tray and existing tray have different lengths".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
let tile_pool = &mut self.tile_pool;
|
|
|
|
|
|
|
|
for (i, played_tile) in tray_tile_locations.iter().enumerate() {
|
|
|
|
if *played_tile {
|
|
|
|
let letter = tray.letters.get_mut(i).unwrap();
|
|
|
|
if letter.is_some() {
|
|
|
|
tile_pool.push(letter.unwrap().clone());
|
|
|
|
*letter = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tile_pool.shuffle(&mut self.rng);
|
|
|
|
tray.fill(&mut self.tile_pool);
|
|
|
|
|
|
|
|
Ok(tray.clone())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-06 01:59:30 +00:00
|
|
|
}
|