0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rustでズンドコキヨシ

Posted at

先駆者様: ズンドコキヨシ with Rust

TL;DR

$ ./target/debug/zundoko-cli
ズンズンドコドコドコズンズンズンズンドコ
キ・ヨ・シ!

コード

main.rs
use rand;

fn main() {
    println!("{}", ZundokoKiyoshi::build_str());
}

enum Zundoko {
    Zun,
    Doko,
}

impl ToString for Zundoko {
    fn to_string(&self) -> String {
        match self {
            Self::Zun => "ズン".to_string(),
            Self::Doko => "ドコ".to_string(),
        }
    }
}

struct ZundokoKiyoshi;

impl ZundokoKiyoshi {
    fn zundoko() -> Zundoko {
        match rand::random() {
            true => Zundoko::Zun,
            false => Zundoko::Doko,
        }
    }

    fn kiyoshi() -> String {
        "\nキ・ヨ・シ!".to_string()
    }

    fn build_str() -> String {
        let mut result = String::new();
        let mut cnt = 0;

        loop {
            let zundoko = Self::zundoko();
            result += &zundoko.to_string();

            match zundoko {
                Zundoko::Zun => cnt += 1,
                Zundoko::Doko if cnt < 4 => cnt = 0,
                _ => {
                    result += &Self::kiyoshi();
                    break;
                }
            }
        }

        result
    }
}

終わりに

Rust は触っていないとすぐお作法を忘れてしまいますね。
ズンドコキヨシはリハビリにはちょうどよかったです。

今回はちょっと冗長なコードで記述させていただきました。
でも先駆者様はすごくシンプルなコードで記述できているんですよね。見習いたいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?