#include #include #include #include #include #include //for copy, fill, find, greater, random_shuffle, sort #include "printable.h" #include "terminal.h" using namespace std; void f(); typedef terminal terminal_t; class closer_to { const terminal_t::iterator it; public: closer_to(const terminal_t::iterator& initial_it) throw() : it(initial_it) {} bool operator()(terminal_t::difference_type d1, terminal_t::difference_type d2) const throw() { return dist(it, it + d1) < dist(it, it + d2); } }; int main(int argc, char **argv) { int status = EXIT_FAILURE; srand(static_cast(time(0))); try { f(); status = EXIT_SUCCESS; } catch (const exception& e) { cerr << argv[0] << ": " << e.what() << "\n"; } catch (...) { cerr << argv[0] << ": main caught unexpected exception\n"; } return status; } void f() { const terminal_t term('.'); const terminal_t::difference_type down = term.down(); const terminal_t::iterator center(term, term.xmax() / 2, term.ymax() / 2); terminal_t::iterator it = center; //copy constructor it[1] = it[0] = 'X'; //Move one step from the center towards the begin, and write a 'Y'. const terminal_t::difference_type d = step(center, term.begin()); it += d; *it = 'Y'; //Move one step from the center away from the begin, and write a 'Z'. it = center; it -= d; *it = 'Z'; //Move a random step away from the center, and write an 'R'. it = center; it += term.rand(); *it = 'R'; it = term.begin(); string s = "qwertyuiopasdfghjklzxcvbnm"; copy(s.begin(), s.end(), it); it += down; copy(s.begin(), s.end(), it); sort(it, it + s.size()); it += down; copy(s.begin(), s.end(), it); sort(it, it + s.size(), greater()); it += down; copy(s.begin(), s.end(), it); random_shuffle(it, it + s.size()); //Display the characters of the string in order of increasing distance //from it0. const terminal_t::iterator it0 = it + 3 * term.xmax() / 4; s = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; vector v; for (terminal_t::iterator it1 = term.begin(); it1 != term.end(); ++it1) { v.push_back(it1 - it0); } sort(v.begin(), v.end(), closer_to(it0)); vector::const_iterator it1 = v.begin(); for (string::iterator it2 = s.begin(); it2 != s.end(); ++it1, ++it2) { it0[*it1] = *it2; } //Midpoint between begin and center: term.begin()[(center - term.begin()) / 2] = 'm'; it += 2 * down; //Move two lines down. s = "Please type printable characters ending with a q: "; it = copy(s.begin(), s.end(), it); for (; it != term.end(); ++it) { char c; //uninitialized variable while ((c = term.key()) == '\0') { } if (c == 'q') { break; } *it = c; } fill(term.begin(), term.end(), term.background()); //Clear the screen. s = "Etch-a-sketch: please type hjklq for left, down, up, right, quit."; copy(s.begin(), s.end(), term.begin()); it = find(term.begin(), term.end(), 's'); if (it != term.end()) { *it = 'S'; } const terminal_t::keypad_t k = term.keypad(); for (it = center;;) { *it = 'X'; char c; //uninitialized variable while ((c = term.key()) == '\0') { } if (c == 'q') { break; } const terminal_t::keypad_t::const_iterator i = k.find(c); //i->first is the char, i->second is its difference_type if (i == k.end() || !it.in_range(i->second)) { term.beep(); } else { it += i->second; } } term.beep(); term.wait(1000); }