C++ When restarting my hangman game my score gets deleted along with player info which I dont want -
i'm making hangman program , works fine except when game ends restarts , makes user input user again , score gets restart. how fix need help.i want when restart game name , score previous game same. cant figure out algorithm so. know messed in main function used copy constructor can't figure way restart words. can me!
in player.h file have:
#ifndef player_h_ #define player_h_ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; class player { public: player(); void makeguess(char &guess); void win(); void lose(); char agree(); private: string name; int score; string mystring; char answer; }; #endif
in hangman .h file have
#ifndef hangman_h_ #define hangman_h_ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> #include "player.h" using namespace std; class hangman { public: hangman(); char play(); protected: player player2; vector<string> words; string the_word; string sofar; string used=""; int wrong; char guess; int count; int maxwrong=0; int x; void respondincorrectguess(); }; #endif
in hangman implementation file have:
#include "hangman.h" #include "player.h" hangman::hangman() { wrong=4; guess='a'; cout<<"welcome dangerous game of hangman! luck "<<endl; words.push_back("monkey"); words.push_back("hangman"); words.push_back("difficult"); srand(time(null)); x=rand()%3; the_word=words[x]; cout<<words[x]<<endl; sofar = string(words[x].size(),'-'); cout<<sofar<<endl; } char hangman::play() { while(1) { cout<<"the number of incorrect guesses have left is: "<<wrong<<endl; player2.makeguess(guess); respondincorrectguess(); if(wrong==maxwrong) player2.lose(); else if(sofar==words[x]) player2.win(); if(wrong==maxwrong||sofar==words[x]) { cout<<"the word was: "<<words[x]<<endl; if(player2.agree()=='y') { cout<<"player has agreed play again. restarting game.."<<endl; return 'y'; } } } } void hangman::respondincorrectguess() { if (words[x].find(guess) != string::npos) { cout << "that's right! " << guess << " in word.\n"; // update sofar include newly guessed letter (int = 0; < words[x].length(); ++i) if (the_word[i] == guess) { sofar[i] = guess; } cout<<sofar<<endl; } else { cout << "sorry, " << guess << " isn't in word.\n"; cout<<sofar<<endl; --wrong; } }
in player implementation file have
#include "player.h" player::player() { cout<<"enter name"<<endl; cin>>name; cout<<name<<" "; score=0; mystring="random"; } void player::makeguess(char &guess) { cout<<"enter guess"<<endl; cin >> mystring; guess=mystring[0]; cin.ignore(numeric_limits<streamsize>::max(),'\n'); if(mystring.size() > 1) { cout<<"error"<<endl; exit(1); } else { if((isdigit(guess))) { cout<<"error"<<endl; exit(1); } } guess = toupper(guess);//convert string first letter } void player::win() { cout<<"congratz win " <<name<<endl; ++score; cout<<"your score is: "<<score<<endl; } void player::lose() { cout<<"i'm sorry have been hunged"<<name<<endl; } char player::agree() { cout<<"would play again"<<endl; cout<<"enter y or y yes, n or n no"<<endl; cin>>answer; if(answer=='y'||answer=='y') { return 'y'; } else if(answer=='n'||answer=='n') exit(1); else { cout<<"invalid command. program exit"<<endl; exit(1); } }
and in main file have:
#include "player.h" #include "hangman.h" using namespace std; int main() { hangman game1; while(1) { if(game1.play()=='y') { game1=hangman();//the name/score gets restarted dont want want else restarted word continue; } } }
in line game1 = hangman();
creating new object hangman , assigning game
. meaning game1
had gone , start new game1. 1 alternative create constructor hangman
create new object hangman
same same player or create copy constructor. example(copy constructor):
hangman::hangman( const hangman& _hangman ){ this->player2 = _hangman.player2; }
and
if(game1.play()=='y') { game1=hangman(game1); continue; }
also, in c++11 can delegate constructors, meaning calling constructor constructor. let's say, in case default constructor initializes other aspects of hangman
, don't want write again, can:
hangman::hangman( const hangman& _hangman ) : hangman(){ this->player2 = _hangman.player2 }
Comments
Post a Comment