]> git.lizzy.rs Git - rs2048.git/commitdiff
Add score display
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 12 Sep 2022 19:24:24 +0000 (21:24 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 12 Sep 2022 19:24:24 +0000 (21:24 +0200)
rust-toolchain.toml [deleted file]
src/display.rs
src/game.rs
src/main.rs

diff --git a/rust-toolchain.toml b/rust-toolchain.toml
deleted file mode 100644 (file)
index ff3a1a1..0000000
+++ /dev/null
@@ -1,2 +0,0 @@
-[toolchain]
-channel = "nightly-2022-07-24"
index fba426af462e4c403e52fa6c14e8717dde8d4ba1..16e6fb9e6868c1eaea7111d4f94e02c0666adf8a 100644 (file)
@@ -53,26 +53,15 @@ fn write_line(stdout: &mut std::io::Stdout, vec: &[u32], mode: Mode) -> crosster
             _ => {}
         };
 
-        if let Mode::Data_ = mode {
-            if n == 0 {
-                queue!(stdout, Print(" ".repeat(FIELD_WIDTH - 2)))?;
-            } else {
-                queue!(
-                    stdout,
-                    Print(format!("{:^w$}", 1 << n, w = FIELD_WIDTH - 2))
-                )?;
-            }
-        } else {
-            queue!(
-                stdout,
-                Print(match mode {
-                    Mode::Roof_ | Mode::Base_ => "━".repeat(FIELD_WIDTH),
-                    Mode::Floor => "─".repeat(FIELD_WIDTH),
-                    Mode::Empty => " ".repeat(FIELD_WIDTH - 2),
-                    Mode::Data_ => panic!("unreachable"),
-                })
-            )?;
-        }
+        queue!(
+            stdout,
+            Print(match mode {
+                Mode::Roof_ | Mode::Base_ => "━".repeat(FIELD_WIDTH),
+                Mode::Floor => "─".repeat(FIELD_WIDTH),
+                Mode::Data_ if n != 0 => format!("{:^w$}", 1 << n, w = FIELD_WIDTH - 2),
+                Mode::Empty | Mode::Data_ => " ".repeat(FIELD_WIDTH - 2),
+            })
+        )?;
 
         match mode {
             Mode::Data_ | Mode::Empty => {
index 19940cd2566ba67e0cae7f2c31f16626f90fd834..bec487a360542ab8c1acb6c0192900788ca982e5 100644 (file)
@@ -15,7 +15,7 @@ impl Swap for Pos {
 pub struct Field(RefCell<u32>);
 
 enum MergeResult {
-    Merged,
+    Merged(u32),
     Replaced,
     Blocked,
     Empty,
@@ -49,7 +49,7 @@ impl Field {
             *s += 1;
             *o = 0;
 
-            return MergeResult::Merged;
+            return MergeResult::Merged(1 << *s);
         }
 
         MergeResult::Blocked
@@ -78,7 +78,7 @@ impl Board {
         }
     }
 
-    pub fn step(&self, dir: Dir) -> bool {
+    pub fn step(&self, dir: Dir) -> Option<u32> {
         let dir = match dir {
             Dir::Up => -Pos::Y,
             Dir::Down => Pos::Y,
@@ -94,7 +94,7 @@ impl Board {
 
         let start = (dir + Pos::ONE) / 2 * (self.size - Pos::ONE);
 
-        let mut moved = false;
+        let mut score = None;
 
         for row in 0..len_row {
             let start_row = start + row * step_row;
@@ -106,21 +106,19 @@ impl Board {
                     let field2 = self.get(start_row + col2 * step_col);
 
                     match field1.merge(field2) {
-                        MergeResult::Merged => {
-                            moved = true;
+                        MergeResult::Merged(sc) => {
+                            score = Some(score.unwrap_or(0) + sc);
                             break;
                         }
-                        MergeResult::Replaced => {
-                            moved = true;
-                        }
+                        MergeResult::Replaced => score = Some(score.unwrap_or(0)),
                         MergeResult::Blocked => break,
-                        MergeResult::Empty => {}
+                        MergeResult::Empty => continue,
                     }
                 }
             }
         }
 
-        moved
+        score
     }
 
     pub fn get(&self, pos: Pos) -> &Field {
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);
                         }
                     }