2018-07-09 06:13:32 +10:00
|
|
|
extern crate rand;
|
2018-07-12 03:43:12 +10:00
|
|
|
extern crate crossterm;
|
2018-07-09 06:13:32 +10:00
|
|
|
|
2018-07-02 06:40:07 +10:00
|
|
|
mod map;
|
2018-07-09 06:13:32 +10:00
|
|
|
mod algorithm;
|
2018-07-12 03:43:12 +10:00
|
|
|
mod messages;
|
|
|
|
mod variables;
|
2018-07-02 06:40:07 +10:00
|
|
|
|
2018-08-12 01:58:15 +10:00
|
|
|
use self::crossterm::{ Crossterm, Screen};
|
|
|
|
use self::crossterm::terminal::{terminal, ClearType};
|
|
|
|
use self::crossterm::style::Color;
|
2018-07-09 06:13:32 +10:00
|
|
|
|
|
|
|
use self::variables::{Size, Position };
|
|
|
|
use self::messages::WELCOME_MESSAGE;
|
|
|
|
|
2018-08-03 20:01:04 +10:00
|
|
|
use std::io::Read;
|
2018-07-09 06:13:32 +10:00
|
|
|
use std::iter::Iterator;
|
|
|
|
use std::{thread, time};
|
|
|
|
|
2018-07-12 03:43:12 +10:00
|
|
|
fn main()
|
|
|
|
{
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
/// run the program
|
|
|
|
pub fn run()
|
|
|
|
{
|
2018-08-12 01:58:15 +10:00
|
|
|
// This is represents the current screen.
|
2018-08-15 07:00:20 +10:00
|
|
|
let screen = Screen::new(true);
|
2018-07-09 06:13:32 +10:00
|
|
|
|
2018-08-12 01:58:15 +10:00
|
|
|
// set size of terminal so the map we are going to draw is fitting the screen.
|
2018-08-12 22:51:08 +10:00
|
|
|
terminal(&screen).set_size(60,110);
|
2018-07-09 06:13:32 +10:00
|
|
|
|
2018-08-12 01:58:15 +10:00
|
|
|
print_welcome_screen(&screen);
|
2018-08-03 20:01:04 +10:00
|
|
|
|
2018-08-12 01:58:15 +10:00
|
|
|
start_algorithm(&screen);
|
2018-08-13 06:41:08 +10:00
|
|
|
drop(screen);
|
2018-07-09 06:13:32 +10:00
|
|
|
}
|
|
|
|
|
2018-08-12 01:58:15 +10:00
|
|
|
fn start_algorithm(screen: &Screen)
|
2018-07-09 06:13:32 +10:00
|
|
|
{
|
|
|
|
// we first want to switch to alternate screen. On the alternate screen we are going to run or firstdepthsearch algorithm
|
2018-08-12 22:51:08 +10:00
|
|
|
if let Ok(ref alternate_screen) = screen.enable_alternate_modes(true)
|
2018-08-12 01:58:15 +10:00
|
|
|
{
|
|
|
|
// setup the map size and the position to start searching for a path.
|
|
|
|
let map_size = Size::new(100, 40);
|
|
|
|
let start_pos = Position::new(10, 10);
|
|
|
|
|
|
|
|
// create and render the map. Or map border is going to have an █ look and inside the map is just a space.
|
|
|
|
let mut map = map::Map::new(map_size, '█', ' ');
|
|
|
|
map.render_map(&alternate_screen.screen);
|
|
|
|
|
|
|
|
// create the algorithm and start it on the alternate screen. Make sure to pass the refrence to the AlternateScreen screen.
|
|
|
|
let mut algorithm = algorithm::FirstDepthSearch::new(map, start_pos, &alternate_screen.screen);
|
|
|
|
algorithm.start();
|
|
|
|
}
|
2018-07-09 06:13:32 +10:00
|
|
|
}
|
|
|
|
|
2018-08-12 01:58:15 +10:00
|
|
|
fn print_welcome_screen(screen: &Screen)
|
2018-07-09 06:13:32 +10:00
|
|
|
{
|
2018-08-15 05:40:07 +10:00
|
|
|
let crossterm = Crossterm::new(&screen);
|
|
|
|
|
2018-07-09 06:13:32 +10:00
|
|
|
// create the handle for the cursor and terminal.
|
2018-08-15 05:40:07 +10:00
|
|
|
let terminal = crossterm.terminal();
|
|
|
|
let cursor = crossterm.cursor();
|
|
|
|
let input = crossterm.input();
|
2018-08-12 22:51:08 +10:00
|
|
|
|
|
|
|
// clear the screen and print the welcome message.
|
|
|
|
terminal.clear(ClearType::All);
|
|
|
|
cursor.goto(0, 0);
|
2018-08-13 06:41:08 +10:00
|
|
|
terminal.write(WELCOME_MESSAGE.join("\n\r"));
|
2018-08-12 22:51:08 +10:00
|
|
|
|
|
|
|
cursor.hide();
|
|
|
|
cursor.goto(0, 10);
|
2018-08-13 06:41:08 +10:00
|
|
|
terminal.write("The first depth search algorithm will start in: Seconds");
|
|
|
|
|
|
|
|
cursor.goto(0, 11);
|
|
|
|
terminal.write("\nPress `q` to abort the program");
|
2018-08-12 01:58:15 +10:00
|
|
|
|
2018-08-12 22:51:08 +10:00
|
|
|
let mut stdin = input.read_async().bytes();
|
2018-08-12 01:58:15 +10:00
|
|
|
|
2018-08-12 22:51:08 +10:00
|
|
|
// print some progress example.
|
|
|
|
for i in (1..5).rev() {
|
|
|
|
let a = stdin.next();
|
2018-08-12 01:58:15 +10:00
|
|
|
|
2018-08-12 22:51:08 +10:00
|
|
|
if let Some(Ok(b'q')) = a {
|
|
|
|
terminal.exit();
|
|
|
|
}
|
|
|
|
// print the current counter at the line of `Seconds to Go: {counter}`
|
|
|
|
cursor.goto(48, 10);
|
2018-08-15 07:00:20 +10:00
|
|
|
crossterm.style(format!("{}", i)).with(Color::Red).on(Color::Blue).paint(&screen);
|
2018-08-12 01:58:15 +10:00
|
|
|
|
2018-08-12 22:51:08 +10:00
|
|
|
// 1 second delay
|
|
|
|
thread::sleep(time::Duration::from_secs(1));
|
|
|
|
}
|
2018-07-02 06:40:07 +10:00
|
|
|
}
|