15 puzzle game: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
Line 12,143: Line 12,143:
=={{header|Rust}}==
=={{header|Rust}}==
{{libheader|rand}}
{{libheader|rand}}
<syntaxhighlight lang="rust">extern crate rand;
<syntaxhighlight lang="rust">
extern crate rand;

use clearscreen::clear;
use std::collections::HashMap;
use std::collections::HashMap;
use std::fmt;
use std::fmt;
use std::io::{self, Write};


use rand::seq::SliceRandom;
use rand::seq::SliceRandom;
Line 12,214: Line 12,216:
let pos = board
let pos = board
.iter()
.iter()
.position(|&cell| match cell {
.position(|&cell| matches!(cell, Cell::Card(value) if value == i))
Cell::Card(value) if value == i => true,
_ => false,
})
.unwrap();
.unwrap();


Line 12,294: Line 12,293:
for turns in 1.. {
for turns in 1.. {
println!("{p15}");
println!("{p15}");
match ask_action(&p15.get_moves()) {
match ask_action(&p15.get_moves(), true) {
Action::Move(direction) => {
Action::Move(direction) => {
p15.play(&direction);
p15.play(&direction);
print!("\x1B[2J\x1B[1;1H");
clear().expect("failed to clear screen");
}
}
Action::Quit => {
Action::Quit => {
print!("\x1B[2J\x1B[1;1H");
clear().expect("failed to clear screen");


println!("Bye !");
println!("Bye!");
break;
break;
}
}
Line 12,308: Line 12,307:


if p15.is_complete() {
if p15.is_complete() {
println!("Well done ! You won in {turns} turns");
println!("Well done! You won in {turns} turns");
break;
break;
}
}
Line 12,314: Line 12,313:
}
}


fn ask_action(moves: &HashMap<Direction, Cell>) -> Action {
fn ask_action(moves: &HashMap<Direction, Cell>, render_list: bool) -> Action {
use std::io::{self, Write};
use Action::*;
use Action::*;
use Direction::*;
use Direction::*;


if render_list {
println!("Possible moves:");
println!("Possible moves:");


if let Some(&Cell::Card(value)) = moves.get(&Up) {
if let Some(&Cell::Card(value)) = moves.get(&Up) {
println!("\tU) {value}");
println!("\tU) {value}");
}
}
if let Some(&Cell::Card(value)) = moves.get(&Left) {
if let Some(&Cell::Card(value)) = moves.get(&Left) {
println!("\tL) {value}");
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();
let mut action = String::new();
Line 12,346: Line 12,349:
"Q" => Quit,
"Q" => Quit,
_ => {
_ => {
println!("Unknown action: {action}");
if unknown_action() {
ask_action(moves)
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}}==
=={{header|Scala}}==