123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- extern crate pancurses;
- use self::pancurses::{
- initscr, endwin, noecho, has_colors, start_color, init_pair,
- Window,
- COLOR_GREEN, COLOR_BLACK
- };
- extern crate rand;
- use self::rand::Rng;
- use self::rand::prelude::SliceRandom;
- use std::{thread, time};
- use config::{Config};
- // Board control and interaction. The view & controller.
- mod board {}
- const ROWS: usize = 17;
- const COLS: usize = 24;
- pub struct Board {
- pub w: Window,
- attempts_left: u8
- }
- pub fn init_board() -> Board {
- let window = initscr();
- noecho();
- window_enable_colors();
- Board{w: window, attempts_left: 4}
- }
- // Different actions are printed to the console at different speeds (the terminal displaying output, vs the player "typing")
- const TERMINAL_PRINTING_SPEED: time::Duration = time::Duration::from_millis(20);
- const TYPING_SPEED: time::Duration = time::Duration::from_millis(70);
- impl Board {
- pub fn intro(&self) {
- self.w.clear();
- thread::sleep(time::Duration::from_millis(250));
- self.print_with_delay(0, String::from("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK"), true, TERMINAL_PRINTING_SPEED);
- self.move_cursor_to(1, 0);
- thread::sleep(time::Duration::from_millis(30));
- // TODO the rest of the intro
- }
- pub fn initialize_game(&self, conf: Config) {
- self.print_with_delay(0, String::from("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL"), false, TERMINAL_PRINTING_SPEED);
- self.print_with_delay(1, String::from("ENTER PASSWORD NOW"), false, TERMINAL_PRINTING_SPEED);
- self.print_with_delay(
- 3,
- self.attempts_display(),
- false,
- TERMINAL_PRINTING_SPEED
- );
- let border_hex = *border_hex_gen();
- let mut b = *garbage_board();
- let shuffled_words = copy_shuffle(&conf.words);
- // TODO: insert each word, of size N, into the array, with at least 1 character between each
- }
- pub fn end_window(&self) {
- endwin();
- }
- fn move_cursor_to(&self, y: i32, x: i32) {
- self.w.mv(y, x);
- self.w.refresh();
- }
- fn print_with_delay(&self, line: i32, string: String, skippable: bool, delay: time::Duration) {
- self.w.mv(line, 0);
- for c in string.chars() {
- self.w.addch(c);
- self.w.refresh();
- if skippable {
- // TODO keyboard skip
- }
- thread::sleep(delay);
- }
- }
- fn attempts_display(&self) -> String {
- format!("{} ATTEMPT(S) LEFT:{}", self.attempts_left, " *".repeat(self.attempts_left as usize))
- }
- }
- fn window_enable_colors() {
- if has_colors() {
- start_color();
- init_pair(1, COLOR_GREEN, COLOR_BLACK);
- }
- }
- fn border_hex_gen() -> Box<[u32; ROWS * 2]> {
- // Build the hex values, printed alongside the text and garbage.
- // TODO: this would make a nice generator, once those are stable.
- let mut rng = rand::thread_rng();
- let mut hex = rng.gen_range(0,200) + 63744;
- let mut ls: [u32; ROWS * 2] = [0; ROWS * 2];
- for i in 0..ROWS * 2 {
- ls[i] = hex;
- hex += 12;
- }
- Box::new(ls)
- }
- fn garbage_board() -> Box<[[char; COLS]; ROWS]> {
- let g: Vec<char> = "!@#$%^*()_-=+\\|/[]{}?\"\':;,.<>".chars().collect();
- let mut b: [[char; COLS]; ROWS] = [['\0'; COLS]; ROWS];
- let mut rng = rand::thread_rng();
- for i in 0..ROWS {
- for j in 0..COLS {
- b[i][j] = *g.choose(&mut rng).unwrap();
- }
- }
- Box::new(b)
- }
- fn copy_shuffle<T: Clone>(vec: &Vec<T>) -> Vec<T> {
- let mut rng = rand::thread_rng();
- let mut vec = vec.clone();
- vec.shuffle(&mut rng);
- vec
- }
|