mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-17 23:35:51 -08:00
rust port
- draw board - legal move checks
This commit is contained in:
75
72_Queen/rust/src/draw.rs
Normal file
75
72_Queen/rust/src/draw.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
pub fn draw_board(q: u8) {
|
||||
let mut blocks = Vec::new();
|
||||
let mut block = 91;
|
||||
|
||||
for _ in 0..8 {
|
||||
for _ in 0..8 {
|
||||
block -= 10;
|
||||
blocks.push(block);
|
||||
println!("{}", block);
|
||||
}
|
||||
block += 91;
|
||||
}
|
||||
|
||||
let draw_h_border = |top: Option<bool>| {
|
||||
let corners;
|
||||
|
||||
if let Some(top) = top {
|
||||
if top {
|
||||
corners = ("┌", "┐", "┬");
|
||||
} else {
|
||||
corners = ("└", "┘", "┴");
|
||||
}
|
||||
} else {
|
||||
corners = ("├", "┤", "┼");
|
||||
}
|
||||
|
||||
print!("{}", corners.0);
|
||||
|
||||
for i in 0..8 {
|
||||
let corner = if i == 7 { corners.1 } else { corners.2 };
|
||||
|
||||
print!("───{}", corner);
|
||||
}
|
||||
println!();
|
||||
};
|
||||
|
||||
draw_h_border(Some(true));
|
||||
|
||||
let mut column = 0;
|
||||
let mut row = 0;
|
||||
|
||||
for block in blocks.iter() {
|
||||
let block = *block as u8;
|
||||
|
||||
let n = if block == q {
|
||||
" Q ".to_string()
|
||||
} else {
|
||||
block.to_string()
|
||||
};
|
||||
|
||||
if block > 99 {
|
||||
print!("│{}", n);
|
||||
} else {
|
||||
print!("│{} ", n);
|
||||
}
|
||||
|
||||
column += 1;
|
||||
|
||||
if column != 1 && (column % 8) == 0 {
|
||||
column = 0;
|
||||
row += 1;
|
||||
|
||||
print!("│");
|
||||
println!();
|
||||
|
||||
if row == 8 {
|
||||
draw_h_border(Some(false));
|
||||
} else {
|
||||
draw_h_border(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
9
72_Queen/rust/src/main.rs
Normal file
9
72_Queen/rust/src/main.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use draw::draw_board;
|
||||
|
||||
mod draw;
|
||||
mod util;
|
||||
|
||||
fn main() {
|
||||
draw_board(158);
|
||||
println!("{}",util::is_move_legal(32,63));
|
||||
}
|
||||
34
72_Queen/rust/src/util.rs
Normal file
34
72_Queen/rust/src/util.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
pub fn is_move_legal(loc: u8, mov: u8) -> bool {
|
||||
let dt: i32 = (mov - loc).into();
|
||||
|
||||
if dt.is_negative() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dt % 21) == 0 || (dt % 10) == 0 || (dt % 11) == 0 {
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn is_legal_start(loc: u8) -> bool {
|
||||
let mut legal_spots = Vec::new();
|
||||
let start: u8 = 11;
|
||||
|
||||
legal_spots.push(start);
|
||||
|
||||
for i in 1..=7 {
|
||||
legal_spots.push(start + (10 * i));
|
||||
}
|
||||
|
||||
for i in 1..=7 {
|
||||
legal_spots.push(start + (11 * i));
|
||||
}
|
||||
|
||||
if legal_spots.contains(&loc) {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user