use crate::app::Action;
use crate::app::Notice;
use crate::bot::Bot;
use crate::bot::Difficulty;
use crate::bot::Opponent;
use crate::game::BoardDimensions;
use crate::game::Game;
use crate::game::GameError;
use crate::game::GameState;
use crate::game::Player;
use crate::game::Status;
#[derive(Debug)]
pub struct App {
dimensions: BoardDimensions,
seed: Option<u64>,
difficulty: Difficulty,
game: Game,
opponent: Opponent,
human_player: Player,
selected_column: usize,
notice: Option<Notice>,
running: bool,
}
impl App {
pub fn new(dimensions: BoardDimensions) -> Self {
Self::with_seed(dimensions, None)
}
pub fn seeded(dimensions: BoardDimensions, seed: u64) -> Self {
Self::with_seed(dimensions, Some(seed))
}
pub fn playing(game: Game, opponent: Opponent) -> Self {
let dimensions = game.state().dimensions();
Self {
selected_column: dimensions.column_count() / 2,
difficulty: opponent.difficulty(),
dimensions,
seed: None,
game,
opponent,
human_player: Player::FIRST,
notice: None,
running: true,
}
}
fn with_seed(dimensions: BoardDimensions, seed: Option<u64>) -> Self {
let difficulty = Difficulty::default();
let mut app = Self::playing(
Game::with_dimensions(dimensions),
opponent_for(difficulty, seed),
);
app.seed = seed;
app
}
pub const fn difficulty(&self) -> Difficulty {
self.difficulty
}
pub const fn opponent(&self) -> &Opponent {
&self.opponent
}
pub const fn game(&self) -> &Game {
&self.game
}
pub const fn state(&self) -> &GameState {
self.game.state()
}
pub const fn human_player(&self) -> Player {
self.human_player
}
pub const fn selected_column(&self) -> usize {
self.selected_column
}
pub const fn notice(&self) -> Option<Notice> {
self.notice
}
pub const fn is_running(&self) -> bool {
self.running
}
pub fn is_waiting_for_player(&self) -> bool {
!self.state().is_over() && self.state().current_player() == self.human_player
}
pub fn headline(&self) -> Headline {
match self.state().status() {
Status::InProgress => Headline::Turn(self.state().current_player()),
Status::Won(victory) => Headline::Won(victory.winner()),
Status::Draw => Headline::Draw,
}
}
pub fn headline_text(&self) -> String {
match self.headline() {
Headline::Turn(player) if player == self.human_player => "Your turn".to_owned(),
Headline::Turn(_) => format!("{} is thinking", self.opponent.name()),
Headline::Won(player) if player == self.human_player => "You win!".to_owned(),
Headline::Won(_) => format!("{} wins", self.opponent.name()),
Headline::Draw => "A draw, the board is full".to_owned(),
}
}
pub fn set_difficulty(&mut self, difficulty: Difficulty) {
self.difficulty = difficulty;
self.opponent = opponent_for(difficulty, self.seed);
}
pub fn restart(&mut self) {
self.game = Game::with_dimensions(self.dimensions);
self.opponent = opponent_for(self.difficulty, self.seed);
self.selected_column = self.dimensions.column_count() / 2;
self.notice = None;
}
pub fn handle(&mut self, action: Action) {
self.notice = None;
match action {
Action::Previous => self.select(self.selected_column.saturating_sub(1)),
Action::Next => self.select(self.selected_column.saturating_add(1)),
Action::Choose(column) => self.select(column),
Action::Confirm => self.place_disc(),
Action::CycleDifficulty => self.set_difficulty(self.difficulty.next()),
Action::SetDifficulty(difficulty) => self.set_difficulty(difficulty),
Action::Restart => self.restart(),
Action::Quit => self.running = false,
}
}
fn select(&mut self, column: usize) {
if column < self.state().column_count() {
self.selected_column = column;
}
}
fn place_disc(&mut self) {
if self.state().is_over() {
self.notice = Some(Notice::GameFinished);
return;
}
if !self.is_waiting_for_player() {
return;
}
match self.game.play(self.selected_column) {
Ok(_) => self.play_opponent_turns(),
Err(GameError::ColumnFull { .. }) => self.notice = Some(Notice::ColumnFull),
Err(_) => self.notice = Some(Notice::MoveRejected),
}
}
fn play_opponent_turns(&mut self) {
while !self.state().is_over() && self.state().current_player() != self.human_player {
let Some(column) = self.opponent.choose_move(self.game.state()) else {
return;
};
if self.game.play(column).is_err() {
self.notice = Some(Notice::MoveRejected);
return;
}
}
}
}
impl Default for App {
fn default() -> Self {
Self::new(BoardDimensions::STANDARD)
}
}
fn opponent_for(difficulty: Difficulty, seed: Option<u64>) -> Opponent {
seed.map_or_else(
|| Opponent::new(difficulty),
|seed| Opponent::seeded(difficulty, seed),
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Headline {
Turn(Player),
Won(Player),
Draw,
}
impl Headline {
pub const fn player(self) -> Option<Player> {
match self {
Self::Turn(player) | Self::Won(player) => Some(player),
Self::Draw => None,
}
}
}