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; char*ascii[7]={ " o", "\n |", "\n/","|","\\", "\n/","\\"}; public: View(Model* m); int update(); }; class Controller { 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(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(); void loadword(); int restart(); private: int guess2(char c, int f); int getcode(); int getlastcorrect(); int getlastguess(); void guess(char c); }; View::View(Model* m) { this->m = m; } int View::update() { int hm = m->getwrong(); char**a=ascii; while(hm--) { std::cout<<a; } std::cout<<std::endl; } Model::Model(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(word,'_')&&(right+wrong)<=7); } int Model::loser() { return (!winner()&&right+wrong>=7); } int Model::gameover() { return winner()*2+loser(); } int Model::getwrong() { return wrong; } void Model::loadword() {strcpy(word,"elephant");} int Model::restart() { loadword(); strcpy(word,word2); 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; return 0; } private: 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); } public: int Model::getcode() {return code; } int Model::getlastcorrect() { return lastcorrect; } int Model::getlastguess() { return lastguess; } void Model::guess(char c) { 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; right+=r; if(r==0){ wrong++; } code= r; if (gameover()) state=2; } 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); m.putview(&v); while (m.go()) {} std::cout<<std::endl; return 0; }
run
|
edit
|
history
|
help
0
VC++ fail
#30.1
GetFinalPathNameByHandle Behaviour
ria
pi
StreamParam
virtual test
лаб1
auto decltype - Return Type Decltype
multiplie linked list numbers