-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yuhui ZHI
authored and
Yuhui ZHI
committed
Nov 15, 2016
0 parents
commit d990505
Showing
15 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/obj | ||
matrix.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "Char.h" | ||
|
||
|
||
Char::Char(char c, int col) : ch(c), color(col) | ||
{ | ||
// nothing to do | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Char | ||
{ | ||
public: | ||
Char(char = '\0', int = -1); | ||
|
||
void setColor(int col) | ||
{ | ||
color = col; | ||
} | ||
|
||
int getColor() const | ||
{ | ||
return color; | ||
} | ||
|
||
char getChar() const | ||
{ | ||
return ch; | ||
} | ||
private: | ||
char ch; | ||
int color; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "Data.h" | ||
|
||
|
||
Data::Data() : data{}, pieces{} | ||
{ | ||
// nothing to do | ||
} | ||
|
||
Data::Data(int size, const Char & c) : data(size, c), pieces{String(size, true, size - 1)} | ||
{ | ||
// nothing to do | ||
} | ||
|
||
void Data::init(Generator & g) | ||
{ | ||
pieces.emplace_back(g.generateEmpty()); | ||
} | ||
|
||
bool Data::needNewString() const | ||
{ | ||
const auto & last = pieces.back(); | ||
if (last.getLen() == last.getPos()) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#ifndef DATA_H_ | ||
#define DATA_H_ | ||
|
||
#include "String.h" | ||
#include "Generator.h" | ||
#include "Char.h" | ||
#include <deque> | ||
|
||
|
||
class Data | ||
{ | ||
public: | ||
Data(); | ||
Data(int, const Char &); | ||
void init(Generator &); | ||
|
||
const Char * getElement(int pos) const | ||
{ | ||
return &data[pos]; | ||
} | ||
|
||
char getChar(int pos, const string & s = "string by default") | ||
{ | ||
return s[pos]; | ||
} | ||
|
||
template<typename ...StringType> | ||
void refresh(Generator & generator, int color_head, int color_body, int color_tail, int color_empty, StringType &&... st) | ||
{ | ||
if (needNewString()) | ||
{ | ||
const auto & it = pieces.back(); | ||
if (it.isEmpty()) | ||
{ | ||
pieces.emplace_back(generator.generateString(std::forward<StringType>(st)...)); | ||
} | ||
else | ||
{ | ||
pieces.emplace_back(generator.generateEmpty()); | ||
} | ||
} | ||
const auto & it = pieces.front(); | ||
if (it.getLen() + data.size() == it.getPos() + 1) | ||
{ | ||
pieces.pop_front(); | ||
} | ||
int size = data.size(); | ||
int pos, len, head, num, color, delta; | ||
constexpr int SIZE = sizeof...(st); | ||
for (auto it = pieces.begin(); it != pieces.end(); ++it) | ||
{ | ||
pos = it->getPos(); | ||
len = it->getLen(); | ||
head = (pos + 1 > size) ? (size - 1) : pos; | ||
num = (len - pos > 1) ? ((pos + 1 > size) ? size : (pos + 1)) : ((size - pos < 1) ? (len - pos + size - 1) : len); | ||
delta = pos - head; | ||
if (it->isEmpty()) | ||
{ | ||
data[size - 1 - head] = Char(generator.EMPTY, color_empty); | ||
} | ||
else | ||
{ | ||
for (int i = 0; i < num; ++i) | ||
{ | ||
if (delta == 0) | ||
{ | ||
color = color_head; | ||
} | ||
else if (len - delta == 1) | ||
{ | ||
color = color_tail; | ||
} | ||
else | ||
{ | ||
color = color_body; | ||
} | ||
if (SIZE == 0) | ||
{ | ||
data[size - 1 - head--] = Char(generator.generateChar(), color); | ||
} | ||
else | ||
{ | ||
data[size - 1 - head--] = Char(getChar((pos + 1 > size) ? (pos - size + 1 + i) : i, std::forward<StringType>(st)...), color); | ||
} | ||
++delta; | ||
} | ||
} | ||
it->setPos(it->getPos() + 1); | ||
} | ||
} | ||
private: | ||
std::deque<String> pieces; | ||
std::vector<Char> data; | ||
|
||
bool needNewString() const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "Generator.h" | ||
|
||
|
||
vector<char> Generator::codeLib | ||
{ | ||
'#', '$', '&', '(', ')', '*', '+', '-', | ||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | ||
'<', '>', '=', '?', '@', '!', '[', ']', '{', '}', '^', '_', '~', | ||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', | ||
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', | ||
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', | ||
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', | ||
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||
}; | ||
|
||
const char Generator::EMPTY{' '}; | ||
|
||
Generator::Generator() | ||
{ | ||
// nothing to do | ||
} | ||
|
||
Generator::Generator(int lines, float r) : e{}, bdStrOrEmpty(r), uiCode(0, codeLib.size() - 1), uiLenString(lines / 9, lines * 3 / 4), uiLenEmpty(lines / 2, lines * 3 / 2) | ||
{ | ||
// nothing to do | ||
} | ||
|
||
String Generator::generateElement() | ||
{ | ||
if (bdStrOrEmpty(e)) | ||
{ | ||
return generateString(); | ||
} | ||
return generateEmpty(); | ||
} | ||
|
||
String Generator::generateString() | ||
{ | ||
return String(uiLenString(e), false); | ||
} | ||
|
||
String Generator::generateString(const string & str) | ||
{ | ||
return String(str.size(), false); | ||
} | ||
|
||
String Generator::generateEmpty() | ||
{ | ||
return String(uiLenEmpty(e), true); | ||
} | ||
|
||
char Generator::generateChar() | ||
{ | ||
return codeLib[uiCode(e)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef GENERATOR_H_ | ||
#define GENERATOR_H_ | ||
|
||
#include "String.h" | ||
#include <vector> | ||
#include <random> | ||
|
||
using std::string; | ||
using std::vector; | ||
|
||
|
||
class Generator | ||
{ | ||
public: | ||
Generator(); | ||
explicit Generator(int, float = 0.3f); | ||
String generateElement(); | ||
String generateString(); | ||
String generateString(const string &); | ||
String generateEmpty(); | ||
char generateChar(); | ||
|
||
static const char EMPTY; | ||
private: | ||
static vector<char> codeLib; | ||
|
||
std::default_random_engine e; | ||
std::bernoulli_distribution bdStrOrEmpty; | ||
std::uniform_int_distribution<int> uiCode; | ||
std::uniform_int_distribution<int> uiLenString; | ||
std::uniform_int_distribution<int> uiLenEmpty; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "Scene.h" | ||
|
||
|
||
Scene::Scene(int cols, int lines, int time) : COLS{cols}, generator{lines}, millisec(time), scene(lines, nullptr), data(cols, Data(lines, Char(Generator::EMPTY, SysTool::COLOR_EMPTY))) | ||
{ | ||
std::for_each(scene.begin(), scene.end(), [=](Char **& p){ p = new Char *[cols]; }); | ||
for (int i = 0; i < data.size(); ++i) | ||
{ | ||
data[i].init(generator); | ||
for (int j = 0; j < scene.size(); ++j) | ||
{ | ||
scene[j][i] = const_cast<Char *>(data[i].getElement(lines - j - 1)); | ||
} | ||
} | ||
} | ||
|
||
Scene::~Scene() | ||
{ | ||
std::for_each(scene.begin(), scene.end(), [](Char **& p){ delete[] p; }); | ||
} | ||
|
||
void Scene::showScene() | ||
{ | ||
for (int i = 0; i < scene.size(); ++i) | ||
{ | ||
for (int j = 0; j < COLS; ++j) | ||
{ | ||
SysTool::setColor(scene[i][j]->getColor()); | ||
SysTool::setPosition(i, j); | ||
SysTool::print("%c", scene[i][j]->getChar()); | ||
} | ||
SysTool::refreshScreen(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef SCENE_H_ | ||
#define SCENE_H_ | ||
|
||
#include "Data.h" | ||
#include "Generator.h" | ||
#include "SysTool.h" | ||
#include <iostream> | ||
#include <thread> | ||
#include <chrono> | ||
|
||
|
||
class Scene | ||
{ | ||
public: | ||
~Scene(); | ||
Scene(int, int, int = SysTool::TIME_LOW); | ||
|
||
template<typename... StringType> | ||
void execute(StringType&&... st) | ||
{ | ||
while (true) | ||
{ | ||
char ch = SysTool::getchNonBlocking(); | ||
if (ch == 'q') | ||
{ | ||
break; | ||
} | ||
else if (ch == 'p') | ||
{ | ||
std::cin.get(); | ||
} | ||
else if (ch == 'j') | ||
{ | ||
millisec = (millisec == SysTool::TIME_LOW) ? millisec : millisec - 10; | ||
} | ||
else if (ch == 'k') | ||
{ | ||
millisec = (millisec == SysTool::TIME_HIGH) ? millisec : millisec + 10; | ||
} | ||
showScene(); | ||
std::for_each(data.begin(), data.end(), [&](Data & d){ d.refresh(generator, SysTool::COLOR_HEAD, SysTool::COLOR_BODY, SysTool::COLOR_TAIL, SysTool::COLOR_EMPTY, std::forward<StringType>(st)...); }); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(millisec)); | ||
} | ||
} | ||
private: | ||
const int COLS; | ||
Generator generator; | ||
vector<Char **> scene; | ||
vector<Data> data; | ||
int millisec; | ||
|
||
void showScene(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "String.h" | ||
|
||
|
||
String::String() : len{0}, empty{true}, pos{-1} | ||
{ | ||
// nothing to do | ||
} | ||
|
||
String::String(int l, bool e, int p) : len{l}, empty{e}, pos{p} | ||
{ | ||
// nothing to do | ||
} |
Oops, something went wrong.