15 puzzle game: Difference between revisions

m
(Applesoft BASIC)
Line 12,143:
=={{header|Rust}}==
{{libheader|rand}}
<syntaxhighlight lang="rust">extern crate rand;
extern crate rand;
 
use clearscreen::clear;
use std::collections::HashMap;
use std::fmt;
use std::io::{self, Write};
 
use rand::seq::SliceRandom;
Line 12,214 ⟶ 12,216:
let pos = board
.iter()
.position(|&cell| match matches!(cell, Cell::Card(value) if value == {i))
Cell::Card(value) if value == i => true,
_ => 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);
print!clear().expect("\x1B[2J\x1B[1;1Hfailed to clear screen");
}
Action::Quit => {
print!clear().expect("\x1B[2J\x1B[1;1Hfailed to clear screen");
 
println!("Bye !");
break;
}
Line 12,308 ⟶ 12,307:
 
if p15.is_complete() {
println!("Well done ! You won in {turns} turns");
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");
print!("Choose your move : ");
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,
_ => {
println!if unknown_action("Unknown action:) {action}");
ask_action(moves, true)
} else {
_ =>ask_action(moves, false,)
})
}
}
}
}</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
}
 
}</syntaxhighlight>
 
=={{header|Scala}}==
3

edits