模拟矩阵背景

时间:2012-09-14 17:08:36

标签: c++

我正在尝试模拟Matrix电影的背景,我编写了以下代码:

#include <iostream>
#include<windows.h>
#include <conio.h>
using namespace std;


void CursorPosition(short x, short y) {
    COORD coordScreen = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
}


void color(int nb) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), nb);
}
int main(int argc, char *argv[]) {
    unsigned int y[79];
    unsigned int ymin[79];
    unsigned int a, b;

    char i[36] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69,
    70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
    88, 89, 90};

    system("title The Matrix !");
    color(10);

    for (a = 0; a < 79; a++) {
        y[a] = 0;
        ymin[a] = 1;
    }

    do {
        for (unsigned int x = 0; x < 79; x++) {
            for (unsigned int j = 0; j < 5; j++) {
                a = rand() % 79;
                if (y[a] != 0) {
                    b = rand() % y[a] + ymin[a];
                    if (b < 26) {
                        if (b < ymin[a] + 2 & b > 2) color(2);
                        CursorPosition(a + 1, b);
                        cout << i[rand() % 36];
                        color(10);
                    }
                }
            }
            if (y[x] != 0 || (rand() % 50) == 0) {
                if ((rand() % (y[x] + 1)) < 20 & ymin[x] == 1) {
                    y[x]++;
                } else {
                    CursorPosition(x + 1, ymin[x]);
                    cout << " ";
                    ymin[x]++;
                    if (ymin[x] < 25) {
                        color(2);
                        CursorPosition(x + 1, ymin[x] + 1);
                        cout << i[rand() % 36];
                        color(10);
                    }
                }
                if ((y[x] + ymin[x]) < 26) {
                    CursorPosition(x + 1, y[x] + ymin[x]);
                    color(15);
                    cout << i[rand() % 36];
                    color(10);
                }
                if ((y[x] + ymin[x]) < 27) {
                    CursorPosition(x + 1, y[x] + ymin[x] - 1);
                    cout << i[rand() % 36];
                }
            }
            if (ymin[x] > 25) {
                ymin[x] = 1;
                y[x] = 0;
            }j
        }
        Sleep(35);
    } while (!kbhit());

    return 0;
}

您有什么建议来改进代码吗? 我的目的是这样的: enter image description here

1 个答案:

答案 0 :(得分:3)

您的代码不是写这些字符。它正在编写简单的字母字符0-9。

你需要的是半角假名字符

。   「   」   、   ・   ヲ   ァ   ィ   ゥ   ェ   ォ   ャ   ュ   ョ   ッ
    ー   ア   イ   ウ   エ   オ   カ   キ   ク   ケ   コ   サ   シ   ス   セ   ソ
    タ   チ   ツ   テ   ト   ナ   ニ   ヌ   ネ   ノ   ハ   ヒ   フ   ヘ   ホ   マ
    ミ   ム   メ   モ   ヤ   ユ   ヨ   ラ   リ   ル   レ   ロ   ワ   ン   ゙   ゚

你需要提升库'boundary部分来实现这些目标。

我找到了一些例子:

boost::locale::generator gen;
using namespace boost::locale::boundary;
std::string text="生きるか死ぬか、それが問題だ。";
ssegment_index map(word,text.begin(),text.end(),gen("ja_JP.UTF-8")); 
for(ssegment_index::iterator it=map.begin(),e=map.end();it!=e;++it) {
    std::cout << "Segment " << *it << " contains: ";
    if(it->rule() & word_none)
        std::cout << "white space or punctuation marks ";
    if(it->rule() & word_kana)
        std::cout << "kana characters ";
    if(it->rule() & word_ideo)
        std::cout << "ideographic characters";
    std::cout<< std::endl;
}

输出:

Segment 生 contains: ideographic characters
Segment きるか contains: kana characters 
Segment 死 contains: ideographic characters
Segment ぬか contains: kana characters 
Segment 、 contains: white space or punctuation marks 
Segment それが contains: kana characters 
Segment 問題 contains: ideographic characters
Segment だ contains: kana characters 
Segment 。 contains: white space or punctuation marks 

Source