Created
March 6, 2015 21:52
-
-
Save JamesJinPark/5c0e928a7ff83e6a103f to your computer and use it in GitHub Desktop.
CIT 595 - HW5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* James Park | |
* CIT 595 HW #5 | |
*/ | |
#include <iostream> | |
#include <stdlib.h> | |
#include <string> | |
#include <string.h> | |
#include <fstream> | |
#include <time.h> | |
using namespace std; | |
/* | |
* Parameter: file to open | |
* Returns random secret word from file | |
*/ | |
string getSecret(char* fileName){ | |
string line = "James says there's nothing here!"; | |
ifstream myFile(fileName); | |
if(myFile.is_open()){ | |
//gets the total number of words in the txt file | |
int count = 0; | |
while(getline (myFile, line)){ | |
count++; | |
} | |
//sets a random secret number | |
srand(time(NULL)); | |
int iSecret = rand() % count + 1; | |
//clears all flags and sets file reader pointer to beginning | |
myFile.clear(); | |
myFile.seekg(0, myFile.beg); | |
//finds random secret word and line = secret word | |
int newCount = 0; | |
while(getline (myFile, line)){ | |
newCount++; | |
if(newCount == iSecret){ | |
break; | |
} | |
} | |
myFile.close(); | |
} | |
else cout << "Unable to open file!\n"; | |
return line; | |
} | |
int main(int argc, char *argv[]) { | |
//check the number of arguments | |
if (argc != 2){ | |
cout << "No dictionary file specified!" << endl; | |
return 0; | |
} | |
string secretWord = getSecret(argv[1]); | |
if(secretWord == "James says there's nothing here!"){ | |
return 0; | |
} | |
int z = 0; | |
while(secretWord[z]){ | |
secretWord[z] = toupper(secretWord[z]); | |
z++; | |
} | |
cout << "Welcome to Hangman!" << endl; | |
int length = secretWord.size() / sizeof(char); | |
string blanks; | |
for(int i = 0; i < length; i++){ | |
blanks += "_ "; | |
} | |
char userGuess = '\0'; | |
string previousGuess = "So far you guessed: "; | |
char previousGuesses[1024] = {'\0'}; | |
int k = 6; | |
bool guessedCorrect = false; | |
int j = 0; | |
int e = 0; | |
while(!guessedCorrect && k != 0){ | |
cout << blanks << endl; | |
if(userGuess != '\0'){ | |
cout << previousGuess << endl; | |
} | |
cout << "Incorrect guesses remaining: " << k << endl << endl; | |
//check to make sure user's guess is valid | |
while(1){ | |
cout << "Guess a letter: "; | |
cin >> userGuess; | |
if(isalpha(userGuess)){ | |
userGuess = toupper(userGuess); | |
} | |
else{ | |
cout << "Please enter a valid letter!" << endl; | |
continue; | |
} | |
int p = 0; | |
for(int i = 0; i < 1024; i++){ | |
if(userGuess == previousGuesses[i]){ | |
cout << "Sorry you already guessed that." << endl; | |
break; | |
} | |
p++; | |
} | |
if(userGuess != previousGuesses[p]){ | |
break; | |
} | |
} | |
bool isCorrect = false; | |
int w = 0; | |
for(int i = 0; i < secretWord.length(); i++){ | |
if(secretWord[i] == userGuess){ | |
blanks[w] = userGuess; | |
isCorrect = true; | |
e++; | |
if(e == length){ | |
guessedCorrect = true; | |
} | |
} | |
w += 2; | |
} | |
if(isCorrect){ | |
cout << "Right!" << endl << endl; | |
} | |
else{ | |
cout << "Wrong!" << endl << endl; | |
k--; | |
} | |
previousGuesses[j] = userGuess; | |
j++; | |
previousGuess += userGuess; | |
previousGuess += " "; | |
} | |
if (guessedCorrect){ | |
cout << "You win!" << endl; | |
} | |
else{ | |
cout << "Sorry! Too many incorrect guesses!" << endl; | |
} | |
cout << "The secret word was " << secretWord << "." << endl; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment