Skip to content

Commit

Permalink
move game tick handling to game engine
Browse files Browse the repository at this point in the history
  • Loading branch information
boldandbrad committed Apr 18, 2024
1 parent 44e5b63 commit 11a1a9b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
39 changes: 37 additions & 2 deletions src/engine/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Game {
.expect("player not found")
}

pub fn current_player_mut(&mut self) -> &mut Player {
fn current_player_mut(&mut self) -> &mut Player {
self.players
.get_mut(&self.current_player_seat)
.expect("player not found")
Expand All @@ -115,9 +115,44 @@ impl Game {
self.players.get(&seat).expect("player not found")
}

pub fn next_turn(&mut self) {
fn next_turn(&mut self) {
self.current_player_seat = self.current_player_seat.next();
}

pub fn handle_game_tick(&mut self, mut tick_count: u64) -> u64 {
match self.state {
GameState::PickingDealer => {
if self.hand_num == 0 {
// TODO: implement picking first dealer by first black jack, then recreating the deck
self.dealer_seat = rand::random();
} else {
self.dealer_seat = self.dealer_seat.next();
}
self.current_player_seat = self.dealer_seat.next();
self.state = GameState::DealingHand;
}
GameState::DealingHand => {
// TODO: deal the "appropriate" way (2, 3, 2, 3, 3, 2, 3, 2)
if tick_count >= 5 {
if self.current_player().hand.is_empty() {
self.current_player_mut().hand = self.deck.deal(5);
self.next_turn();
} else {
// TODO: display face up card and deck on the table
self.current_player_seat = self.dealer_seat.next();
self.state = GameState::CallingPickup;
}
tick_count = 0
} else {
tick_count += 1
}
}
GameState::CallingPickup => {}
GameState::CallingHighSuit => {}
GameState::PlayingHand => {}
}
tick_count
}
}

impl Default for Game {
Expand Down
45 changes: 3 additions & 42 deletions src/interface/screens/game_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::interface::{
use crate::{
engine::{
card::Card,
game::{Game, GameState},
game::Game,
player::Player,
table::{Seat, SEAT_VARIANTS},
},
Expand Down Expand Up @@ -157,47 +157,8 @@ impl Screen for GameScreen {
}

fn handle_tick_event(&mut self) -> Option<InterfaceCallback> {
match self.is_paused {
false => {
match self.game.state {
GameState::PickingDealer => {
if self.game.hand_num == 0 {
// TODO: implement picking first dealer by first black jack, then recreating the deck
self.game.dealer_seat = rand::random();
} else {
self.game.dealer_seat = self.game.dealer_seat.next();
}
self.game.current_player_seat = self.game.dealer_seat.next();
self.game.state = GameState::DealingHand;
}
GameState::DealingHand => {
// TODO: deal the "appropriate" way (2, 3, 2, 3, 3, 2, 3, 2)
if self.tick_count >= 5 {
if self.game.current_player().hand.is_empty() {
self.game.current_player_mut().hand = self.game.deck.deal(5);
self.game.next_turn();
} else {
// TODO: display face up card and deck on the table
self.game.current_player_seat = self.game.dealer_seat.next();
self.game.state = GameState::CallingPickup;
}
self.tick_count = 0
} else {
self.tick_count += 1
}
}
GameState::CallingPickup => {}
GameState::CallingHighSuit => {}
GameState::PlayingHand => {}
}
// if self.tick_count >= 20 {
// self.game.next_turn();
// self.tick_count = 0
// } else {
// self.tick_count += 1
// }
}
true => {}
if !self.is_paused {
self.tick_count = self.game.handle_game_tick(self.tick_count);
}
None
}
Expand Down

0 comments on commit 11a1a9b

Please sign in to comment.