]> git.lizzy.rs Git - rs2048.git/blobdiff - src/main.rs
Add score display
[rs2048.git] / src / main.rs
index e04f92c38348d32ef34b63a4c1165ae7663cd825..d698ed4a1a60d43a8f0ae59f08b2df3ce211a29e 100644 (file)
@@ -1,7 +1,7 @@
 pub mod display;
 pub mod game;
 
-use crossterm::{cursor, event, execute, queue, terminal};
+use crossterm::{cursor, event, execute, queue, style, terminal};
 use game::{Board, Dir::*, Pos};
 use std::io::Write;
 
@@ -17,11 +17,18 @@ fn main() {
     board.spawn(&mut rng);
     board.spawn(&mut rng);
 
+    let mut score = 0;
+
     loop {
         queue!(
             stdout,
             terminal::Clear(terminal::ClearType::All),
-            cursor::MoveTo(0, 0)
+            cursor::MoveTo(0, 0),
+            style::SetAttribute(style::Attribute::Bold),
+            style::Print("Score: ".to_string()),
+            style::SetAttribute(style::Attribute::Reset),
+            style::Print(score.to_string()),
+            cursor::MoveToNextLine(1),
         )
         .unwrap();
         display::display_board(&mut stdout, &board).unwrap();
@@ -31,7 +38,7 @@ fn main() {
             match evt {
                 event::Event::Key(event::KeyEvent { code, .. }) => match code {
                     event::KeyCode::Char(ch) => {
-                        if board.step(match ch.to_ascii_lowercase() {
+                        if let Some(sc) = board.step(match ch.to_ascii_lowercase() {
                             'w' => Up,
                             'a' => Left,
                             's' => Down,
@@ -39,6 +46,7 @@ fn main() {
                             'q' => break,
                             _ => continue,
                         }) {
+                            score += sc;
                             board.spawn(&mut rng);
                         }
                     }