reversi working, fixes

This commit is contained in:
stjet
2024-12-06 03:10:45 +00:00
parent eec8b055d3
commit 5da36108fe
54 changed files with 466 additions and 28 deletions

View File

@@ -63,15 +63,17 @@ impl WindowLike for AudioPlayer {
fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions> {
let mut instructions = vec![DrawInstructions::Text([2, self.dimensions[1] - LINE_HEIGHT], vec!["times-new-roman".to_string()], if self.command.len() > 0 { self.command.clone() } else { self.response.clone() }, theme_info.text, theme_info.background, None, None)];
if let Some(sink) = &self.sink {
let current = &self.queue[self.queue.len() - sink.len()];
let current_name = current.0.file_name().unwrap().to_string_lossy().into_owned();
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - current_name.len() * MONO_WIDTH as usize / 2, 2], vec!["times-new-romono".to_string(), "shippori-mincho".to_string()], current_name.clone(), theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
if let Some(artist) = &current.2 {
let artist_string = "by ".to_string() + &artist;
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - artist_string.len() * MONO_WIDTH as usize / 2, LINE_HEIGHT + 2], vec!["times-new-romono".to_string()], artist_string, theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
if sink.len() > 0 {
let current = &self.queue[self.queue.len() - sink.len()];
let current_name = current.0.file_name().unwrap().to_string_lossy().into_owned();
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - current_name.len() * MONO_WIDTH as usize / 2, 2], vec!["times-new-romono".to_string(), "shippori-mincho".to_string()], current_name.clone(), theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
if let Some(artist) = &current.2 {
let artist_string = "by ".to_string() + &artist;
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - artist_string.len() * MONO_WIDTH as usize / 2, LINE_HEIGHT + 2], vec!["times-new-romono".to_string()], artist_string, theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
}
let time_string = format!("{}/{}", format_seconds(sink.get_pos().as_secs()), format_seconds(current.1));
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - time_string.len() * MONO_WIDTH as usize / 2, LINE_HEIGHT * 2 + 2], vec!["times-new-romono".to_string()], time_string, theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
}
let time_string = format!("{}/{}", format_seconds(sink.get_pos().as_secs()), format_seconds(current.1));
instructions.push(DrawInstructions::Text([self.dimensions[0] / 2 - time_string.len() * MONO_WIDTH as usize / 2, LINE_HEIGHT * 2 + 2], vec!["times-new-romono".to_string()], time_string, theme_info.text, theme_info.background, Some(0), Some(MONO_WIDTH)));
}
//
instructions

View File

@@ -1,6 +1,5 @@
use std::vec::Vec;
use std::vec;
use std::fmt;
use ming_wm::window_manager::{ DrawInstructions, WindowLike, WindowLikeType };
use ming_wm::messages::{ WindowMessage, WindowMessageResponse };
@@ -8,6 +7,24 @@ use ming_wm::framebuffer::{ Dimensions, RGBColor };
use ming_wm::themes::ThemeInfo;
use ming_wm::ipc::listen;
const REVERSI_GREEN: RGBColor = [72, 93, 63];
struct ValidMove {
pub point: [usize; 2],
pub will_flip: Vec<[usize; 2]>,
}
//tried to do some PartialEq implementation but didn't play nice with .contains
//so just do this instead
fn valid_moves_contains(valid_moves: &Vec<ValidMove>, point: &[usize; 2]) -> Option<Vec<[usize; 2]>> {
for valid_move in valid_moves {
if &valid_move.point == point {
return Some(valid_move.will_flip.clone());
}
}
None
}
#[derive(Default, PartialEq)]
enum Tile {
#[default]
@@ -30,7 +47,9 @@ impl Tile {
struct Reversi {
dimensions: Dimensions,
tiles: [[Tile; 8]; 8],
//
current_number: Option<u8>, //the first number of the tile that user wants to place piece on
valid_moves: Vec<ValidMove>,
white_turn: bool, //if false, black turn
}
impl WindowLike for Reversi {
@@ -39,16 +58,39 @@ impl WindowLike for Reversi {
WindowMessage::Init(dimensions) => {
self.dimensions = dimensions;
self.new_tiles();
self.valid_moves = self.get_valid_moves();
WindowMessageResponse::JustRerender
},
WindowMessage::KeyPress(key_press) => {
if let Ok(n) = key_press.key.to_string().parse::<u8>() {
if let Some(current_number) = self.current_number {
let y = current_number as usize;
let x = n as usize;
if let Some(mut will_flip) = valid_moves_contains(&self.valid_moves, &[x, y]) {
self.tiles[y][x] = if self.white_turn { Tile::White } else { Tile::Black };
will_flip.push([x, y]);
for point in will_flip {
self.tiles[point[1]][point[0]] = if self.white_turn { Tile::White } else { Tile::Black };
}
self.white_turn = !self.white_turn;
self.valid_moves = self.get_valid_moves();
}
self.current_number = None;
} else {
self.current_number = Some(n);
}
} else if key_press.key == '𐘁' { //backspace
self.current_number = None;
}
WindowMessageResponse::JustRerender
},
//
_ => WindowMessageResponse::DoNothing,
}
}
fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions> {
let mut instructions = vec![
DrawInstructions::Rect([0, 0], self.dimensions, [72, 93, 63]),
DrawInstructions::Rect([0, 0], self.dimensions, REVERSI_GREEN),
];
let square_width = (self.dimensions[0] - 10) / 8;
for l in 0..9 {
@@ -67,7 +109,16 @@ impl WindowLike for Reversi {
for x in 0..8 {
let tile = &self.tiles[y][x];
if tile == &Tile::Empty {
//
instructions.push(DrawInstructions::Text([x * square_width + square_width / 2, y * square_width + square_width / 2], vec!["times-new-roman".to_string()], format!("{}{}", y, x), theme_info.text, REVERSI_GREEN, None, None));
if valid_moves_contains(&self.valid_moves, &[x, y]).is_some() {
//yellow border
instructions.extend([
DrawInstructions::Rect([5 + x * square_width, 5 + y * square_width], [square_width + 2, 2], [255, 255, 0]),
DrawInstructions::Rect([5 + x * square_width, 5 + y * square_width], [2, square_width], [255, 255, 0]),
DrawInstructions::Rect([5 + (x + 1) * square_width, 5 + y * square_width], [2, square_width], [255, 255, 0]),
DrawInstructions::Rect([5 + x * square_width + 2, 5 + (y + 1) * square_width], [square_width + 2, 2], [255, 255, 0]),
]);
}
} else {
instructions.push(DrawInstructions::Circle([x * square_width + square_width / 2 + 5, y * square_width + square_width / 2 + 5], square_width / 2 - 3, tile.to_color().unwrap()));
}
@@ -103,6 +154,97 @@ impl Reversi {
self.tiles[3][4] = Tile::Black;
self.tiles[4][4] = Tile::White;
}
pub fn get_valid_moves(&self) -> Vec<ValidMove> {
let mut valid_moves = Vec::new();
for y0 in 0..8 {
for x0 in 0..8 {
let current_tile = &self.tiles[y0][x0];
if (current_tile == &Tile::White && self.white_turn) || (current_tile == &Tile::Black && !self.white_turn) {
for t in 0..8 {
let mut potential_move = false; //true once opposite colour tile found
let mut point = [x0, y0];
let mut will_flip = Vec::new();
loop {
let x = point[0];
let y = point[1];
if t == 0 {
//up left
if y > 0 && x > 0 {
point = [x - 1, y - 1];
} else {
break;
}
} else if t == 1 {
//up
if y > 0 {
point = [x, y - 1];
} else {
break;
}
} else if t == 2 {
//up right
if y > 0 && x < 7 {
point = [x, y - 1];
} else {
break;
}
} else if t == 3 {
//left
if x > 0 {
point = [x - 1, y];
} else {
break;
}
} else if t == 4 {
//right
if x < 7 {
point = [x + 1, y];
} else {
break;
}
} else if t == 5 {
//down left
if y < 7 && x > 0 {
point = [x - 1, y + 1];
} else {
break;
}
} else if t == 6 {
//down
if y < 7 {
point = [x, y + 1];
} else {
break;
}
} else if t == 7 {
//down right
if y < 7 && x < 7 {
point = [x + 1, y + 1];
} else {
break;
}
}
let tile = &self.tiles[point[1]][point[0]];
if tile == &Tile::Empty && potential_move {
valid_moves.push(ValidMove {
point,
will_flip,
});
break;
} else if (tile == &Tile::Black && self.white_turn) || (tile == &Tile::White && !self.white_turn) {
will_flip.push(point);
potential_move = true;
} else {
break;
}
}
}
}
}
}
valid_moves
}
}
pub fn main() {

View File

@@ -9,7 +9,7 @@ fn main() {
let mut a = Command::new("cargo").arg("run").arg("-q").arg("--bin").arg("start_menu").stdout(Stdio::piped()).stdin(Stdio::piped()).stderr(Stdio::null()).spawn().unwrap();
a.stdin.unwrap().write_all("subtype\n".to_string().as_bytes());
let mut output = String::new();
a.stdout.as_mut().unwrap().read_to_string(&mut output);
let _ = a.stdout.as_mut().unwrap().read_to_string(&mut output);
println!("{}", output);
//println!("{}", &ron::to_string(&[122, 400]).unwrap());
}