Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <math.h>
- #include <ctime>
- #include <Windows.h>
- using namespace std;
- class CPlayer
- {
- string name;
- int lives;
- vector<string> guessed;
- int wins;
- string word;
- string fakeword;
- public:
- int getguess(){return guessed.size();}
- string getguess(int i){return guessed.at(i);}
- string tellword(){return word;}
- string tellfword(){return fakeword;}
- int telllives(){return lives;}
- int tellwins(){return wins;}
- string tellname(){return name;}
- void setname(string a = "Player"){name = a;}
- bool incwin(bool a);
- void declives(int a = 1){lives -= a;}
- void setword(string a);
- void setlives(unsigned int a = 16){lives = a;}
- int guess(string a, bool b);
- void reset(){lives = 16; word.clear(); fakeword.clear(); guessed.clear();}
- }player[2];
- void CPlayer::setword(string a)
- {
- word = a;
- for(unsigned int i = 0; i < a.length(); i++)
- {
- fakeword += '_';
- }
- }
- int CPlayer::guess(string a, bool b)
- {
- if((a.at(0) < 65 || a.at(0) > 122) || (a.at(0) > 90 && a.at(0) < 97))
- {
- cout << "You can only guess letters: ";
- return 3;
- }
- if(a.length() == 1)
- {
- guessed.push_back(a);
- bool flag = false;
- for(unsigned int i = 0; i < (player[b].word.length()); i++)
- {
- if(a[0] == player[b].word.at(i) || a[0] == player[b].word.at(i) + 32 || a[0] == player[b].word.at(i) - 32)
- {
- player[b].fakeword.at(i) = player[b].word.at(i);
- flag = true;
- if(player[b].fakeword == player[b].word)
- {
- return (incwin(!b) == true ? 2 : 0);
- }
- }else if(i == player[b].word.length() - 1 && flag == false)
- {
- declives();
- }
- }
- }else
- {
- for(int i = 0; i < player[b].word.length(); i++)
- {
- if(player[b].word.length() != a.length() || player[b].word.at(i) != a.at(i) && player[b].word.at(i) != a.at(i) + 32 && player[b].word.at(i) != a.at(i) -32)
- {
- declives(2);
- return 1;
- }
- }
- return (incwin(!b) == true ? 2 : 0);
- }
- if(player[!b].lives == 0)
- {
- incwin(b);
- return 0;
- }
- return 1;
- }
- bool CPlayer::incwin(bool a)
- {
- player[a].wins ++;
- cout << player[a].name << " wins!" << endl;
- cout << player[1].name << "'s word was " << player[1].word << "." << endl;
- cout << "Current score: " << player[a].name << " " << player[a].wins << " : " << player[!a].name << " " << player[!a].wins << endl;
- cout << "Play again? (y/n): ";
- while(true)
- {
- string ans;
- getline(cin, ans);
- if(ans.length() == 1)
- {
- if(ans.at(0) == 89 || ans.at(0) == 121)
- {
- return 1;
- }else if (ans.at(0) == 78 || ans.at(0) == 110)
- {
- return 0;
- }else
- {
- cout << "y/n : ";
- }
- }else
- {
- cout << "y/n : ";
- }
- ans.clear();
- }
- }
- void print(bool a);
- void lines(bool a, int b = 0, bool c = true, int d = 1);
- void boxes (bool a, string b, int c);
- //Lots of comments to make this garbage readable.
- //No I don't care that it's garbage, move along.
- int main(int argc, char* argv[])
- {
- string scroller;
- cout << "Hangman WIP © Fear 2013";
- getline(cin, scroller);
- cout << "Feel free to submit word lists so I can add them in.";
- getline(cin, scroller);
- cout << "This version only has the Animal library.";
- getline(cin, scroller);
- cout << endl << endl << endl;
- //Get username
- cout << "Username: ";
- string a;
- getline(cin, a);
- player[0].setname(a);
- player[1].setname("Hangbot");
- while(true) // LOOP A
- {
- player[0].reset();
- player[1].reset();
- player[1].setlives(99999999);
- cout << "Easy(1), Medium(2), Hard(3): ";
- string qdiffi;
- int idiffi;
- stringstream ss;
- while(true)
- {
- getline(cin, qdiffi);
- if(qdiffi.length() == 1)
- {
- ss << qdiffi;
- if(ss >> idiffi)
- {
- player[0].setlives(16-pow(2.0,idiffi));
- break;
- }
- }
- }
- //Get word list #
- fstream dict;
- string dir = argv[0];
- for(int i = 0; i < dir.length(); i++)
- {
- if(dir.at(i) == '\\')
- {
- dir.insert(i, "\\");
- i++;
- }
- }
- int j = dir.find("\\Hangman.exe");
- dir.replace(j, dir.back(), "DICTIONARY0.txt");
- dict.open(dir);
- vector<string> wordlist;
- if(dict.good())
- {
- //Load contents
- string temp;
- while(getline(dict, temp))
- {
- wordlist.push_back(temp);
- }
- }else
- {
- cout << "Error locating dictionary file, exiting." << endl;
- cin.get();
- return 0;
- }
- //Get bot's word
- srand(time(NULL));
- int randword = rand() % wordlist.size();
- player[1].setword(wordlist.at(randword));
- //Get player's word
- cout << "Your word (letters only, no spaces): ";
- string a;
- bool flaga = false;
- while(!flaga)
- {
- getline(cin, a);
- if(a.size() >= 2)
- {
- for(unsigned int i = 0; i < a.length(); i++)
- {
- if(a.at(i) < 65 || a.at(i) > 122)
- {
- cout << "Letters only!: ";
- break;
- }else if(i == a.length() - 1)
- {
- //nested loops are the worst
- flaga = !flaga;
- break;
- }
- }
- }else
- {
- cout << "Your word must be 2 or more characters long!: ";
- }
- }
- player[0].setword(a);
- bool bturn = false;
- bool flagb = true;
- while(flagb) // LOOP B
- {
- print(bturn);
- while(flagb)
- {
- cout << "Guess a letter: ";
- string pguess;
- if(!bturn)
- {
- getline(cin, pguess);
- }else
- {
- bool flagc = false;
- while(!flagc)
- {
- pguess = rand() % 26 + 97;
- if(player[bturn].getguess() >= 1)
- {
- for(int i = 0; i < player[bturn].getguess(); i++)
- {
- if(pguess == player[bturn].getguess(i))
- {
- break;
- }else if(i == player[bturn].getguess() - 1)
- {
- flagc = true;
- break;
- }
- }
- }else
- {
- flagc = true;
- }
- }
- Sleep(500);
- cout << pguess << endl;
- Sleep(500);
- }
- int a = player[bturn].guess(pguess, !bturn);
- if(a == 0)
- {
- return 0;
- }else if(a == 1)
- {
- break;
- }else if(a == 2)
- {
- flagb = false;
- break;
- }
- }
- bturn = !bturn;
- }
- }
- }
- void print(bool a)
- {
- cout << endl;
- //x = player word or name, depending on which is longer:
- int x = ((player[a].tellname().length() > player[a].tellword().length()) ? player[a].tellname().length() : player[a].tellword().length());
- string name = player[a].tellname();
- string word = player[a].tellword();
- lines(a, 0, true, x); //see lines function
- boxes(a, name, x);
- if(!a)
- {
- stringstream ss;
- ss << player[a].telllives();
- string templives = ss.str();
- boxes(a, "Lives: " + templives, x);
- boxes(a, "Word: " + word, x); //see boxes function
- }
- boxes(a, "Revealed: " + player[a].tellfword(), x);
- boxes(a, "Used:", x);
- vector<string> temp;
- for(int i = 0; i < player[a].getguess(); i++) temp.push_back(player[a].getguess(i)); //download
- string ins; //empty string
- for(int i = 0; i < temp.size() && temp.size() > 0; i++) //while there is still content..
- {
- ins = ins + temp.at(i); //add each letter to the string one by one...
- ins.append(" "); //add a space...
- if(ins.size() == 16 + ((name.length() % 2 == 0) ? name.length() : name.length() + 1))
- {
- boxes(a, ins, x); //print it...
- ins.clear(); //then clear it...
- }
- }
- if(ins.size() > 0) //if there is any remaining letters..
- {
- boxes(a, ins, x); //then print them!
- }
- }
- //a = player, b = subtraction from length calculation, c = optional newline, d = x (from earlier)
- void lines(bool a, int b, bool c, int d)
- {
- for(unsigned int i = 0; i < ((( (d % 2 == 0) ? 24 : 23) + d - 1) - b); i++)
- {
- cout << "-";
- }
- if(c)
- {
- cout << endl;
- }
- }
- //a = player, b = non-modifiable content length, c = x (from earlier)
- void boxes(bool a, string b, int c)
- {
- cout << "|";
- for(unsigned int i = 0; i < (( (c % 2 == 0) ? 22 : 21) + c - 1) - b.length(); i++)
- {
- if(i == (22 + c - b.length())/2)
- {
- cout << b;
- }
- cout << " ";
- }
- cout << "|" << endl;
- cout << "|";
- lines(a, 2, false, c);
- cout << "|" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement