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"> |
<syntaxhighlight lang="rust"> |
||
extern crate rand; |
|||
use clearscreen::clear; |
|||
use std::collections::HashMap; |
use std::collections::HashMap; |
||
use std::fmt; |
use std::fmt; |
||
⚫ | |||
use rand::seq::SliceRandom; |
use rand::seq::SliceRandom; |
||
Line 12,214: | Line 12,216: | ||
let pos = board |
let pos = board |
||
.iter() |
.iter() |
||
.position(|&cell| |
.position(|&cell| matches!(cell, Cell::Card(value) if value == i)) |
||
Cell::Card(value) if value == i => true, |
|||
⚫ | |||
⚫ | |||
.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); |
||
clear().expect("failed to clear screen"); |
|||
} |
} |
||
Action::Quit => { |
Action::Quit => { |
||
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 |
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 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}"); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} else { |
|||
print!("Unknown move, try again: "); |
|||
io::stdout().flush().unwrap(); |
|||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
let mut action = String::new(); |
let mut action = String::new(); |
||
Line 12,346: | Line 12,349: | ||
"Q" => Quit, |
"Q" => Quit, |
||
_ => { |
_ => { |
||
if unknown_action() { |
|||
ask_action(moves) |
ask_action(moves, true) |
||
} else { |
|||
⚫ | |||
⚫ | |||
} |
} |
||
} |
} |
||
} |
|||
⚫ | |||
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}}== |
=={{header|Scala}}== |