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 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) { restart(s); state=0; } int getstate() {return state;} int 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 loadprofile(char*s) { return 0; } void putview(View*v) { this->v=v; } void putcontroller(Controller*cn) { this->cn=cn; } const char* display() { return disp; } int winner() { return (!strchr(word,'_')&&(right+wrong)<=7); } int loser() { return (!winner()&&right+wrong>=7); } int gameover() { return winner()*2+loser(); } int getwrong() { return wrong; } loadword() {strcpy(word,"elephant");} int 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 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 getcode() {return code; } int getlastcorrect() { return lastcorrect; } int getlastguess() { return lastguess; } void 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; } }; class View { Model* m; char*ascii[7]={ " o", "\n |", "\n/","|","\\", "\n/","\\"}; public: View(Model* m) { this->m = m; } int update() { int hm = m->getwrong(); char**a=ascii; while(hm--) { std::cout<<a; } std::cout<<std::endl; } }; class Controller { char s[256]; Model*m; public: Controller(Model* m) { this->m=m; } void* 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
#21.2
cross-platform sleep function
du
atoi on hex
Zero length array as a class member
problem_solution_1
auto decltype - Return Type Decltype
Revers find file in windows
vector destruction - visual studio
no-thread