//This file is terminal1d.h. #ifndef TERMINAL1DH #define TERMINAL1DH #include //for rand #include //for abs (Stroustrup p. 660) #include #include using namespace std; extern "C" { #include "term.h" } template class terminal1d: public vector { private: CHAR _background; public: terminal1d(CHAR initial_background = ' ') : vector(80, initial_background), _background(initial_background) { term_construct(); } ~terminal1d() {term_destruct();} CHAR background() const {return _background;} bool in_range(const iterator& it) const { return begin() <= it && it < end(); } static char key() {return term_key();} static void wait(int milliseconds) {term_wait(milliseconds);} static void beep() {term_beep();} static double distance(const iterator& it1, const iterator& it2) { return abs(it1 - it2); } static difference_type rand() {return #ifndef _MSC_VER std #endif ::rand() % 3 - 1;} static difference_type step(const iterator& it1, const iterator& it2) { return it1 > it2 ? -1 : it1 < it2; } typedef map keypad_t; static keypad_t keypad() { #ifdef _MSC_VER //Microsoft map does not have the constructor with 2 arguments. keypad_t k; k.insert(make_pair('h', -1)); //left k.insert(make_pair('l', 1)); //right return k; #else static const pair a[] = { make_pair('h', -1), //left make_pair('l', 1) //right }; static const size_t n = sizeof a / sizeof a[0]; return keypad_t(a, a + n); #endif } }; #endif