#ifndef MANUAL_H #define MANUAL_H #include "wabbit.h" class manual: public virtual wabbit { public: manual(const terminal& init_t, unsigned init_x, unsigned init_y, char init_c): wabbit {init_t, init_x, init_y, init_c} {} void decide(int *dx, int *dy) const { struct keystroke { char c; int dx; //horizontal motion int dy; //vertical motion }; static const keystroke a[] { {'h', -1, 0}, //left {'j', 0, 1}, //down {'k', 0, -1}, //up {'l', 1, 0} //right }; if (const char k {key()}) { //If a key was pressed, for (const auto& stroke: a) { //search for it. if (k == stroke.c) { *dx = stroke.dx; *dy = stroke.dy; return; } } punish(); //Arrive here if bad key was pressed. } *dx = 0; //Arrive here if bad key or no key was pressed. *dy = 0; } void punish() const {beep();} //There is a guilty human. }; #endif