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(); }; class Model { char word[MAXLEN]; char chosen[MAXLEN]; int wrong, right; char disp[MAXLEN]; View*v; Controller* cn; int lastcorrect; int lastguess; int code; int state; void* in; int wins, losses; 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(); int getright(); void loadword(); int restart(); public: int guess2(char c, int f); int getcode(); int getlastcorrect(); int 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<<"There are "<<m->getlastcorrect()<<" " <<m->getlastguess()<<std::endl; } else std::cout<<"No "<<m->getlastguess()<<std::endl; } std::cout<<"Enter a guess: "<<std::endl; break; case(2): 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): if (toupper(*s)!='Y') state=3; else restart(); break; case(3): 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; } 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; } *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; } int 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) wrong++; else {right++;} code= r; if (gameover()) state=2; } const char* Model::getdisplay() { return disp; } Controller::Controller(Model* m) { this->m=m; } void* Controller::go() { switch(m->getstate()) { case(3): 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
class with unique_ptr to vector
wcstombs_s wide string conversion with multibyte chars and locale + concatenation
algorithm_1
Even Odd
Waiting for Multiple Objects
new
dump_own_process.cpp
Zero
std override
pi