Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
hangman
#include <iostream> #include <string.h> #include <ctype.h> const int MAXLEN=256; class View; class Controller; class Model; class View { Model* m; const char*ascii[7]={ " o", "\n |", "\n/","|","\\", "\n/"," \\"}; public: View(Model* m); void update(); }; class Controller { public: char s[256]; Model*m; public: Controller(Model* m); void* go(); }; //state 0 loads profile //state 1 plays a game //state 2 displays win/lose //state 3 asks to play again //state 4 terminates class Model { char word[MAXLEN]; char chosen[MAXLEN]; char incletters[MAXLEN]; int wrong, right; char disp[MAXLEN]; View*v; Controller* cn; int lastcorrect; char lastguess; int code; int state; void* in; int wins, losses; char user[256]; void saveprofile(); public: Model(const char*s); int getstate(); int go(); int loadprofile(char*s); void putview(View*v); void putcontroller(Controller*cn); const char* display(); int winner(); int loser(); int gameover(); int getwrong(); const char* getincletters(); int getright(); const char*getword(); void loadword(); int restart(); public: int guess2(char c, int f); int getcode(); int getlastcorrect(); char getlastguess(); void guess(char c); public: const char *getdisplay(); }; View::View(Model* m) { this->m = m; } void View::update() { int hm; const char**a=ascii; switch(m->getstate()) { case(0): std::cout<<"Enter a user name: "; break; case(1): hm = m->getwrong(); std::cout<<hm<<" wrong, "<<m->getright()<<" right." <<std::endl<<std::endl; while(hm--) { std::cout<<(*a++); } std::cout<<std::endl<<std::endl; std::cout<<m->getdisplay()<<std::endl; if(m->getright()+m->getwrong()>0) { if(m->getlastcorrect()) { std::cout<<"Yes "<<m->getlastcorrect()<<" " <<m->getlastguess()<<std::endl; } else std::cout<<"No "<<m->getlastguess()<<std::endl; } std::cout<<"Incorrect letters: "<<m->getincletters() <<std::endl; std::cout<<"Enter a guess: "<<std::endl; break; case(2): if (m->winner()) std::cout<<"WINNER!"<<std::endl; else std::cout<<"Lost."<<std::endl; std::cout<<"The word was: "<<m->getword(); break; case(3): std::cout<<"Play again?(y/n) "<<std::endl; } } Model::Model(const char*s) { restart(); state=0; } int Model::getstate() {return state;} int Model::go() { v->update(); char*s=(char*)(cn->go()); switch(state){ case(0): loadprofile(s); state=1; break; case(1): guess(*s); break; case(2): state=3; case(3): if (toupper(*s)!='Y') state=4; else restart(); break; case(4): return 0; } return 1; } int Model::loadprofile(char*s) { return 0; } void Model::putview(View*v) { this->v=v; } void Model::putcontroller(Controller*cn) { this->cn=cn; } const char* Model::display() { return disp; } int Model::winner() { return (!strchr(disp,'_')&&((wrong)<=7)); } int Model::loser() { return (!winner()&&((wrong)>=7)); } int Model::gameover() { return winner()*2+loser(); } int Model::getwrong() { return wrong; } int Model::getright() { return right; } const char*Model::getword() { return word; } void Model::loadword() {strcpy(word,"elephant");} int Model::restart() { loadword(); wrong=0; right=0; for(int i=0;word[i];i++){ if(word[i]==' ') disp[i]=' '; else disp[i]='_'; disp[i+1]=0; } *incletters=0; *chosen=0; lastcorrect=0; lastguess=0; return 0; } int Model::guess2(char c, int f) { if (word[f]==0) return 0; if (toupper(word[f])==toupper(c)) { disp[f]=toupper(c); return guess2(c,f+1)+1; } else return guess2(c,f+1); } int Model::getcode() {return code; } int Model::getlastcorrect() { return lastcorrect; } char Model::getlastguess() { return lastguess; } void Model::guess(char c) { std::cout<<c<<std::endl; int r; char s[2]; s[0]=c; s[1]=0; if (strchr(chosen,c)) {code=-1;return;} if (!isalpha(c)) {code=-2; return;} chosen[right+wrong]=toupper(c); chosen[right+wrong+1]=0; r=guess2(c,0); lastcorrect=r; lastguess=c; if(r==0) { incletters[wrong]=c; wrong++; incletters[wrong]=0; } else {right++;} code= r; if (winner()) wins++; if (loser()) losses++; if (gameover()) { state=2; saveprofile(); } } const char* Model::getdisplay() { return disp; } const char* Model::getincletters() { return incletters; } void Model::saveprofile() {} Controller::Controller(Model* m) { this->m=m; } void* Controller::go() { switch(m->getstate()) { case(2): case(4): break; default: std::cin>>s; } return s; } int main() { Model m("elephant"); View v(&m); Controller cn(&m); m.putview(&v); m.putcontroller(&cn); while(m.go()) {} //m.guess('e'); //std::cout<<m.getdisplay()<<std::endl; return 0; }
run
|
edit
|
history
|
help
0
rvalue lifetime
Thing v.2
Test
trying to find if reinterpret_cast preserves calling convention
Sorting algorithm comparison
Template
sample ptr and obj ex
StreamParam
make_integer_sequence
#27