EVERYTHING IS WORKING

This commit is contained in:
LinlyBoi
2023-05-06 23:14:19 +03:00
parent 59474b531a
commit 31f5b2cdc7
3 changed files with 98 additions and 35 deletions

View File

@@ -1,4 +1,7 @@
use raylib::prelude::Color; use raylib::{
prelude::{Color, MouseButton},
RaylibHandle,
};
use crate::gamedata::{algorithms::minimax_decision, Board, Disk}; use crate::gamedata::{algorithms::minimax_decision, Board, Disk};
pub const STARTY: i32 = 9; pub const STARTY: i32 = 9;
@@ -9,6 +12,7 @@ const CIRCLEWIDTH: i32 = 56;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[derive(Clone)]
pub struct PlayState { pub struct PlayState {
pub circles: Vec<(i32, i32, Disk)>, pub circles: Vec<(i32, i32, Disk)>,
pub bottom: Vec<i32>, pub bottom: Vec<i32>,
@@ -45,10 +49,18 @@ impl PlayState {
self.player_turn = true; self.player_turn = true;
} }
} }
#[derive(Clone)]
pub struct MenuState { pub struct MenuState {
difficulty: i32, pub difficulty: i32,
p1: (Color, Disk), p1: (Color, Disk),
p2: (Color, Disk), p2: (Color, Disk),
pub selection: Selection,
pub strategy: Strategy,
}
#[derive(Clone)]
pub enum Strategy {
MiniMax,
AlphaBeta,
} }
impl Default for MenuState { impl Default for MenuState {
fn default() -> Self { fn default() -> Self {
@@ -56,13 +68,51 @@ impl Default for MenuState {
difficulty: 3, difficulty: 3,
p1: (Color::RED, Disk::P1), p1: (Color::RED, Disk::P1),
p2: (Color::YELLOW, Disk::P2), p2: (Color::YELLOW, Disk::P2),
selection: Selection::Difficulty,
strategy: Strategy::MiniMax,
} }
} }
} }
impl MenuState {
pub fn init(&mut self, rl: &RaylibHandle) -> bool {
dbg!(&self.selection);
match self.selection {
Selection::Difficulty => {
if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
self.difficulty = 3;
self.selection = Selection::Cooking;
}
if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) {
self.difficulty = 5;
self.selection = Selection::Cooking;
}
}
Selection::Cooking => {
if rl.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
self.difficulty = 3;
self.selection = Selection::Done;
}
if rl.is_mouse_button_pressed(MouseButton::MOUSE_RIGHT_BUTTON) {
self.difficulty = 5;
self.selection = Selection::Done;
}
}
Selection::Done => return true,
}
false
}
}
#[derive(Clone)]
pub enum GameState { pub enum GameState {
Play(PlayState), Play(PlayState),
MainMenu(MenuState), MainMenu(MenuState),
} }
#[derive(Clone, Debug)]
pub enum Selection {
Difficulty,
Cooking,
Done,
}
fn get_circle_coords(x: i32, y: i32) -> (i32, i32) { fn get_circle_coords(x: i32, y: i32) -> (i32, i32) {
let mut returned: (i32, i32) = (0, 0); let mut returned: (i32, i32) = (0, 0);

View File

@@ -11,7 +11,6 @@ const POT_STREAKS: i32 = 4;
const POT_WIN: i32 = 5; // should be nerfed if its just 1 potential win const POT_WIN: i32 = 5; // should be nerfed if its just 1 potential win
const POT_WINS: i32 = 6; const POT_WINS: i32 = 6;
const SCORE_DIFF: i32 = 40; const SCORE_DIFF: i32 = 40;
const MAX_WINS: i32 = 17;
pub fn get_score(board: &Board, disk: Disk) -> i32 { pub fn get_score(board: &Board, disk: Disk) -> i32 {
//this should be summing up a bunch of functions defined below this one //this should be summing up a bunch of functions defined below this one
@@ -239,7 +238,7 @@ fn streak_test_1() {
board.play(Disk::P2, 3); board.play(Disk::P2, 3);
board.play(Disk::P2, 3); board.play(Disk::P2, 3);
let sequences = get_streaks(&board.columns, &Disk::P2); let sequences = get_streaks(&board.columns, &Disk::P2);
assert_eq!(12, potential_streaks(&sequences, &Disk::P2)); assert_eq!(POT_STREAKS * 2, potential_streaks(&sequences, &Disk::P2));
} }
#[test] #[test]
fn win_test_flipping() { fn win_test_flipping() {

View File

@@ -1,6 +1,9 @@
use oxidised4::{ use oxidised4::{
bored::PlayState, bored::{GameState, MenuState, PlayState, Strategy},
gamedata::{algorithms::minimax_decision_pruning, Disk}, gamedata::{
algorithms::{minimax_decision, minimax_decision_pruning},
Disk,
},
}; };
use raylib::prelude::*; use raylib::prelude::*;
const NROW: i32 = 6; const NROW: i32 = 6;
@@ -36,11 +39,22 @@ fn main() {
let _square_wewant = (square_widf * NROW / 2, square_heif * 3 / 2); let _square_wewant = (square_widf * NROW / 2, square_heif * 3 / 2);
let _square_center = square_widf / 2; let _square_center = square_widf / 2;
//7,9 are the values to center the circle //7,9 are the values to center the circle
let mut state: PlayState = PlayState::default(); let mut state: GameState = GameState::MainMenu(MenuState::default());
let mut strategy = Strategy::MiniMax;
let mut difficulty = 3;
rl.set_target_fps(60); rl.set_target_fps(60);
while !rl.window_should_close() { while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread); let mut d = rl.begin_drawing(&thread);
match &mut state {
GameState::MainMenu(ref mut mstate) => {
if mstate.init(&d) {
strategy = mstate.strategy.clone();
difficulty = mstate.difficulty.clone();
state = GameState::Play(PlayState::default());
}
}
GameState::Play(ref mut state) => {
match state.player_turn { match state.player_turn {
true => { true => {
if d.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) { if d.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON) {
@@ -49,33 +63,33 @@ fn main() {
} }
} }
} }
false => state.play_cpu(minimax_decision_pruning), false => match strategy {
Strategy::MiniMax => state.play_cpu(minimax_decision),
Strategy::AlphaBeta => state.play_cpu(minimax_decision_pruning),
},
} }
if state.board.game_over() { if state.board.game_over() {
let scores = state.board.getscore(); let scores = state.board.getscore();
println!("Player score: {} \n CPU Score: {}", scores.0, scores.1); println!("Player score: {} \n CPU Score: {}", scores.0, scores.1);
} }
for circle in &state.circles { for circle in state.clone().circles {
let (x, y, disk) = circle; let (x, y, disk) = circle;
let color = match disk { let color = match disk {
Disk::P1 => Color::RED, Disk::P1 => Color::RED,
Disk::P2 => Color::YELLOW, Disk::P2 => Color::YELLOW,
Disk::EMPTY => Color::WHITE, Disk::EMPTY => Color::WHITE,
}; };
d.draw_texture(&circle_texture, *x, *y, color); d.draw_texture(&circle_texture, x, y, color);
} }
d.clear_background(Color::WHITE); d.clear_background(Color::WHITE);
d.draw_texture(&board_texture, BOARDSTART.0, BOARDSTART.1, Color::VIOLET); d.draw_texture(&board_texture, BOARDSTART.0, BOARDSTART.1, Color::VIOLET);
} }
}
}
} }
//TODO move this to a struct const STARTY: i32 = 9;
pub const STARTY: i32 = 9;
pub const STARTX: i32 = 7;
const WX: i32 = 14;
const WY: i32 = 14;
const CIRCLEWIDTH: i32 = 56;
fn get_mouse_column(rl: &RaylibHandle, sw: i32) -> Option<i32> { fn get_mouse_column(rl: &RaylibHandle, sw: i32) -> Option<i32> {
//row,col return //row,col return
let mouse_pos = rl.get_mouse_x(); let mouse_pos = rl.get_mouse_x();