get_good_move function
This commit is contained in:
+33
-33
@@ -195,32 +195,7 @@ fn find_valid_instruction(
|
|||||||
.then_some(instruction)
|
.then_some(instruction)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), std::io::Error> {
|
fn get_good_move(state: &Klondike) -> Option<KlondikeInstruction> {
|
||||||
let mut session = Session::new(Klondike::new_random_default());
|
|
||||||
let mut input = String::new();
|
|
||||||
loop {
|
|
||||||
// display game
|
|
||||||
println!("{}", Displayed(session.state()));
|
|
||||||
|
|
||||||
// parse input
|
|
||||||
input.clear();
|
|
||||||
std::io::stdin().read_line(&mut input)?;
|
|
||||||
let Ok(instruction) = input.trim().parse() else {
|
|
||||||
println!("Invalid instruction.");
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
// run game
|
|
||||||
match instruction {
|
|
||||||
SessionInstruction::New => session = Session::new(Klondike::new_random_default()),
|
|
||||||
SessionInstruction::Undo => session.undo(),
|
|
||||||
SessionInstruction::Exit => break Ok(()),
|
|
||||||
SessionInstruction::Hint => {
|
|
||||||
for instruction in session.possible_instructions() {
|
|
||||||
println!("{instruction:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SessionInstruction::Auto => {
|
|
||||||
fn useless_moves(instruction: &KlondikeInstruction) -> bool {
|
fn useless_moves(instruction: &KlondikeInstruction) -> bool {
|
||||||
!matches!(
|
!matches!(
|
||||||
instruction,
|
instruction,
|
||||||
@@ -231,10 +206,7 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
fn instruction_priority(
|
fn instruction_priority(state: &Klondike, instruction: &KlondikeInstruction) -> usize {
|
||||||
state: &Klondike,
|
|
||||||
instruction: &KlondikeInstruction,
|
|
||||||
) -> usize {
|
|
||||||
// 1 Move into foundation
|
// 1 Move into foundation
|
||||||
// 2 T->T Move to reveal new card (moving a non-king to reveal empty tableau also counts)
|
// 2 T->T Move to reveal new card (moving a non-king to reveal empty tableau also counts)
|
||||||
// 3 Move from stock
|
// 3 Move from stock
|
||||||
@@ -261,11 +233,39 @@ fn main() -> Result<(), std::io::Error> {
|
|||||||
KlondikeInstruction::RotateStock => 4,
|
KlondikeInstruction::RotateStock => 4,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(instruction) = session
|
state
|
||||||
.possible_instructions()
|
.possible_instructions()
|
||||||
.filter(useless_moves)
|
.filter(useless_moves)
|
||||||
.min_by_key(|ins| instruction_priority(session.state(), ins))
|
.min_by_key(|ins| instruction_priority(state, ins))
|
||||||
{
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), std::io::Error> {
|
||||||
|
let mut session = Session::new(Klondike::new_random_default());
|
||||||
|
let mut input = String::new();
|
||||||
|
loop {
|
||||||
|
// display game
|
||||||
|
println!("{}", Displayed(session.state()));
|
||||||
|
|
||||||
|
// parse input
|
||||||
|
input.clear();
|
||||||
|
std::io::stdin().read_line(&mut input)?;
|
||||||
|
let Ok(instruction) = input.trim().parse() else {
|
||||||
|
println!("Invalid instruction.");
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
// run game
|
||||||
|
match instruction {
|
||||||
|
SessionInstruction::New => session = Session::new(Klondike::new_random_default()),
|
||||||
|
SessionInstruction::Undo => session.undo(),
|
||||||
|
SessionInstruction::Exit => break Ok(()),
|
||||||
|
SessionInstruction::Hint => {
|
||||||
|
for instruction in session.possible_instructions() {
|
||||||
|
println!("{instruction:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SessionInstruction::Auto => {
|
||||||
|
if let Some(instruction) = get_good_move(session.state()) {
|
||||||
session.process_instruction(instruction);
|
session.process_instruction(instruction);
|
||||||
} else {
|
} else {
|
||||||
println!("No valid moves!");
|
println!("No valid moves!");
|
||||||
|
|||||||
Reference in New Issue
Block a user