15 puzzle game: Difference between revisions
m
→{{header|Rust}}
(Applesoft BASIC) |
|||
Line 12,143:
=={{header|Rust}}==
{{libheader|rand}}
<syntaxhighlight lang="rust">
extern crate rand;
use clearscreen::clear;
use std::collections::HashMap;
use std::fmt;
use rand::seq::SliceRandom;
Line 12,214 ⟶ 12,216:
let pos = board
.iter()
.position(|&cell|
_ => false,▼
})▼
.unwrap();
Line 12,294 ⟶ 12,293:
for turns in 1.. {
println!("{p15}");
match ask_action(&p15.get_moves(), true) {
Action::Move(direction) => {
p15.play(&direction);
}
Action::Quit => {
println!("Bye
break;
}
Line 12,308 ⟶ 12,307:
if p15.is_complete() {
println!("Well done
break;
}
Line 12,314 ⟶ 12,313:
}
fn ask_action(moves: &HashMap<Direction, Cell>, render_list: bool) -> Action {
▲ use std::io::{self, Write};
use Action::*;
use Direction::*;
if render_list {
println!("Possible moves:");
if let Some(&Cell::Card(value)) = moves.get(&Up) {
println!("\tU) {value}");
}
if let Some(&Cell::Card(value)) = moves.get(&Left) {
println!("\tL) {value}");
}▼
if let Some(&Cell::Card(value)) = moves.get(&Right) {▼
println!("\tR) {value}");▼
}▼
if let Some(&Cell::Card(value)) = moves.get(&Down) {▼
println!("\tD) {value}");▼
}
println!("\tQ) Quit");▼
io::stdout().flush().unwrap();▼
} else {
print!("Unknown move, try again: ");
io::stdout().flush().unwrap();
}
▲ if let Some(&Cell::Card(value)) = moves.get(&Right) {
▲ println!("\tR) {value}");
▲ }
▲ if let Some(&Cell::Card(value)) = moves.get(&Down) {
▲ println!("\tD) {value}");
▲ }
▲ println!("\tQ) Quit");
▲ print!("Choose your move : ");
▲ io::stdout().flush().unwrap();
let mut action = String::new();
Line 12,346 ⟶ 12,349:
"Q" => Quit,
_ => {
ask_action(moves, true)
} else {
}
}
}
}</syntaxhighlight>▼
fn unknown_action() -> bool {
use crossterm::{
cursor::MoveToPreviousLine,
terminal::{Clear, ClearType},
ExecutableCommand,
};
std::io::stdout().execute(MoveToPreviousLine(1)).unwrap();
std::io::stdout()
.execute(Clear(ClearType::CurrentLine))
.unwrap();
false
}
=={{header|Scala}}==
|