From 26632ddb4db187efa09d9b89fc7fd22670b7c660 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Tue, 18 Jun 2019 13:33:56 -0500 Subject: [PATCH] print board and move with the arrow keys --- src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c7f4143..3b5b8ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,76 @@ Author: @kirbylife license: GPLv2 */ -fn main() { +extern crate ncurses; + +use ncurses::*; + +const ROWS: usize = 15; +const COLUMNS: usize = 10; +const MINES: i32 = ((ROWS * COLUMNS) as i32 * 20 / 100); + + +fn main() { + let mut running = true; + let mut board = [['-'; COLUMNS]; ROWS]; + let mut pivot = [0 as usize; 2]; + initscr(); + raw(); + + keypad(stdscr(), true); + noecho(); + + while running == true { + clear(); + for (i, row) in board.iter().enumerate() { + for (j, item) in row.iter().enumerate() { + if pivot[0] == i && pivot[1] == j { + addstr(format!("<{}>", item).as_ref()); + } + else { + addstr(format!(" {} ", item).as_ref()); + } + } + match i { + 0 => addstr("\t To move use the arrow keys"), + 1 => addstr("\t To flag a box, press 'f'"), + 2 => addstr("\t To reveal a box, press 'Space'"), + 3 => addstr("\t To exit, press 'q'"), + _ => addstr(""), + }; + addstr("\n"); + } + let ch = getch(); + if ch == KEY_UP { + // Go up + if pivot[0] > 0 { + pivot[0] -= 1; + } + } + else if ch == KEY_DOWN { + // Go down + if pivot[0] < ROWS-1 { + pivot[0] += 1; + } + } + else if ch == KEY_LEFT { + // Go left + if pivot[1] > 0 { + pivot[1] -= 1; + } + } + else if ch == KEY_RIGHT { + // Go Right + if pivot[1] < COLUMNS-1 { + pivot[1] += 1; + } + } + else if ch == 'q' as i32 || ch == 'Q' as i32 { + // if you press q, the game ends + break; + } + refresh(); + } + endwin(); }