Skip to content

Commit

Permalink
Latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-moreton committed Mar 29, 2023
1 parent ef5becc commit c24d1b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ screenshots = "0.5.3"
fs = "0.0.5"
image = "0.24.6"
opencv = "0.78.2"
md5 = "0.7.0"



2 changes: 1 addition & 1 deletion history.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#V2
go movetime 10000
ucinewgame
go movetime 250
ucinewgame
Expand Down Expand Up @@ -99,3 +98,4 @@ asd
scan
bench 1000
scan
position fen rnbqkbnr/pp--pppp/---p----/--p-----/----P---/-----N--/PPPP-PPP/RNBQKB-R b KQkq - 0 1
70 changes: 37 additions & 33 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,28 @@ pub fn screen_scan() -> Result<()> {

let mut move_number = 0;
let mut mover = "";
let mut fen_list = Vec::new();
let mut last_fen: String = "".to_string();

fen_list.push("Start".parse().unwrap());
Command::new("screencapture")
.args(&["-D1", "-x", "-t", "png", "/tmp/screenshot.png"])
.output().map_err(|e| e.to_string()).expect("TODO: panic message");

loop {
Command::new("screencapture")
.args(&["-D1", "-t", "png", "/tmp/screenshot.png"])
.output().map_err(|e| e.to_string()).expect("TODO: panic message");

let whole_screen = imgcodecs::imread("/tmp/screenshot.png", imgcodecs::IMREAD_COLOR)?;
let best_match_top_left = find_scaled_template("/tmp/screenshot.png", "/Users/chris/git/chris-moreton/resources/eight.png")?;
let best_match_bottom_right = find_scaled_template("/tmp/screenshot.png", "/Users/chris/git/chris-moreton/resources/h.png")?;

let best_match_top_left = find_scaled_template("/tmp/screenshot.png", "/Users/chris/git/chris-moreton/resources/eight.png")?;
let best_match_bottom_right = find_scaled_template("/tmp/screenshot.png", "/Users/chris/git/chris-moreton/resources/h.png")?;
let chessboard_x = best_match_top_left.x;
let chessboard_y = best_match_top_left.y;

let chessboard_x = best_match_top_left.x;
let chessboard_y = best_match_top_left.y;
let chessboard_width = best_match_bottom_right.x - chessboard_x + 67;

let chessboard_width = best_match_bottom_right.x - chessboard_x + 67;
loop {
let whole_screen = imgcodecs::imread("/tmp/screenshot.png", imgcodecs::IMREAD_COLOR)?;

let roi = Rect::new(chessboard_x, chessboard_y, chessboard_width, chessboard_width);

let mut chessboard_image = Mat::roi(&whole_screen, roi)?;
let mut chessboard_image_resized = resize_square_image(&chessboard_image, Size::new(RESIZED_BOARD_IMAGE_WIDTH, RESIZED_BOARD_IMAGE_WIDTH))?;

imgcodecs::imwrite(&*format!("/tmp/chessboard-cropped.png"), &chessboard_image, &Vector::new())?;
imgcodecs::imwrite(&*format!("/tmp/chessboard-resized.png"), &chessboard_image_resized, &Vector::new())?;

let squares = extract_chessboard_squares(&chessboard_image_resized)?;
let mut piece_list = Vec::new();

Expand All @@ -64,30 +59,31 @@ pub fn screen_scan() -> Result<()> {
}
}

mover = if move_number == 0 {
if move_number == 0 && mover == "" {
if piece_list[0] == 'r' {
"w"
mover = "w"
} else {
"b"
mover = "b"
}
} else {
if mover == "w" {
"b"
} else {
"w"
}
};
}

let fen = vec_to_fen(&piece_list, mover);
if let Some(last_fen) = fen_list.last() {
if fen != *last_fen {
println!("{}", fen);
move_number += 1;
fen_list.push(fen.to_string());
println!("{}", fen);
if fen.split(" ").next() != last_fen.split(" ").next() {
println!("{}", fen);
move_number += 1;
if mover == "w" {
mover = "b"
} else {
mover = "w"
}
last_fen = fen.clone();
}

sleep(Duration::from_millis(2000))
Command::new("screencapture")
.args(&["-D1", "-t", "-x", "png", "/tmp/screenshot.png"])
.output().map_err(|e| e.to_string()).expect("TODO: panic message");

}

Ok(())
Expand Down Expand Up @@ -123,7 +119,15 @@ fn vec_to_fen(pieces: &Vec<char>, mover: &str) -> String {
// Update these according to the actual state of the game.
fen.push_str(&*format!(" {} KQkq - 0 1", mover));

fen
fen.replace("--------", "8")
.replace("-------", "7")
.replace("------", "6")
.replace("-----", "5")
.replace("----", "4")
.replace("---", "3")
.replace("--", "2")
.replace("-", "1")

}

fn extract_center(square: &Mat) -> Result<Mat, opencv::Error> {
Expand Down

0 comments on commit c24d1b4

Please sign in to comment.