mardi 2 décembre 2014

Lyric Game Bugs C++


Vote count:

0




I have been working on a lyric game that takes the lyrics from a text file and then converts them to an array of words. The text file is divided into songs by a certain arrangement of symbols.


Each song is enclosed by a "~(insertnumberhere)~' at the start and a "~!(insertnumberhere)~" at the end, eg. "~1~" "words words words" "~!1~"


It then takes this text and converts it to a string using the function I wrote: txtout(), then from a string to a vector of words using the function I wrote: getlyrics(), and then for each song, I convert the vector to an array for use in the game.


The game asks the player which song he would like to use to play, and he picks between 2 songs. Then the song is converted from the text file to the array of words using the previously mentioned steps.


Then it picks a random spot in the song and a random number of words to output. The user must then input the next words in the song. If he gets it correct, he gets points, otherwise, the game goes back to the start. The more words he adds, the more points he gets.


I believe the problems I am getting are not with my functions but rather with the for, if and while loops I use in the game itself. Once I reach those points I start getting weird situations where the incorrect lyrics text and prompt for user input keep repeating indefinitely and other strange errors. Any help would be appreciated.


The start of the game is the start of the main function. I will write out a mock text file to emulate the way my text file works but with fake songs to avoid any issues:


~1~ Words words words words words, words. Words! ~!1~


~2~ Hello Hello hello hi hi hi Hello, hi there, how are you. ~!2~



#include <iostream>
#include <random>
#include <fstream>
#include <istream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <array>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;

//function that converts text to lower case
string makelower(string text)
{
int iter = 0;
char cha;
string newtext;
while (iter < text.length())
{
cha = text[iter];
cha = tolower(cha);
newtext+=cha;
iter++;
}
return(newtext);
}

//function that outputs text from a text file as a string based on which number is inputed as the parameter
string txtout(string input)
{
//open a stream reader
ifstream fin;
//opens the text file in the stream reader
fin.open("songlyrics.txt");
//create a temporary string that reads the whole file
string temp;
//create a string to hold each individual song
string song;
//song number start defined by the paramater input at the beginning of function getlyrics()
string songnums = "~" + input + "~";
//song number end defined by the paramater input at the beginning of function getlyrics()
string songnume = "~!" + input + "~";
//used to differentiate between when to add characters to song string and when not to add characters
int add = 0;
//used as a pointer when reading each character in text file
char ch;
//while the end of file is not reached
while(!fin.eof())
{
//get character and add it to temporary string
fin.get(ch);
temp += ch;

//if there is an instance of the songnums string in the temporary string change add to 1
if(temp.find(songnums)!= -1)
{
add = 1;
//reset temp to save some space
temp = "";
}
//if there is an instance of the songnume string in the temporary string change add to 0
else if(temp.find(songnume)!= -1)
{
add = 0;
}
//if add is 1 add the character to the song string
if(add == 1)
{
song += ch;
}

}
//close the file
fin.close();
return(song);
}

//create a vector string function called getlyrics with one parameter: dat
vector <string> getlyrics(string dat)
{
//save dat as a new string data
//get the size of the data string to get number of characters
string data = dat;
int siz = data.size();
//used to keep track of letter position when creating words
string letter;
//used to keep track of previous letter position when creating words
string prevletter;
//used to keep track of current word
string word;
//creates a vector called lyrics to keep track of lyrics in order
vector <string> lyrics;

//for every letter in data string...
for(int i = 0; i<=siz; i++)
{
//set the letter for current place in data string
letter+=data[i];

//if we are on the first letter...
if(i == 0)
{
//... then the previous letter is empty
prevletter = "";
}
//otherwise, the previous letter is the previous character in data string
else
{
prevletter += data[i-1];
}
//if the letter is not equal to a space or a newline...
if(letter != " " && letter != "\n" && letter != "")
{
//then add the current letter to the word
word += data[i];
//reset the letter
letter = "";

}
//otherwise if the letter is a space or newline
else if(letter== " " || letter == "\n")
{
//if the previous letter not equal to a space or newline
if(prevletter!=" "||prevletter!="\n")
{
//then the word is finished and add it to the lyrics vector
lyrics.push_back(word);
//reset the word, letter, and add 1 to the word count
word = "";
letter = "";
}
}
//reset the previous letter
prevletter = "";
}
//return the vector lyrics
return lyrics;
}
int main()
{
//sets basic variables like quit, instance of srand, and point number
int quit = 0;
srand (time(NULL));
int points = 0;

//main game loop
while(quit == 0)
{
//check if the player wants to quit and convert answer to lower case using make lower function for convenience
string ans;
cout<<"Do you want to quit?"<<endl;
cin>> ans;
ans = makelower(ans);
if(ans == "yes" || ans == "y")
{
cout<<"Goodbye";
quit = 1;
}
//if the player doesn't want to quit...
else
{
//give the options that exist in the text file currently
cout<<endl<<"Pick a song:"<<endl<<"1) Sweatpants - Childish Gambino"<<endl<<"2) Slow Ride - Foghat"<<endl;
//if the song choice is out of range, reply that the song is not a valid song number
string sng;
cin>>sng;
int maximum = 2;
int minimum = 1;
int sngi;
istringstream(sng)>>sngi;
if(sngi > maximum || sngi < minimum)
{
cout<<"That is not a valid song number."<<endl;
}
//if it is a valid song number...
else
{
//create a new instance of getlyrics using our txtout function with the number of the song as the parameter
vector<string> song = getlyrics(txtout(sng));
//create a variable to save the size of the vector
int ssize = int32_t(song.size());
cout<<ssize;
//create a string array the same size as the vector
string song_[ssize];
int iter = 0;
cout<<ssize<<endl;
//for each entry in the vector, add it to the corresponding place in the new string array
for (auto c : song)
{
c = makelower(c);
song_[iter] = c;
iter++;

}
//reset the first entry in the array to an empty because it will have an artifact from the txtout function function
song_[0] = "";
//create a new while statement for actual game logic
int done = 0;
while(done == 0)
{
//pick a random sequence of random length from the selected song and iterate through each word
int counter = 0;
int randspot= rand()%(ssize-15) + 1;
int randseq = rand()%10 + 1;
while(counter<randseq)
{
cout<<song_[randspot]<<" ";
counter ++;
randspot++;
}
//request an input from the user
cout<<endl<<"Please enter a word or sequence of words from the song in order to reply."<<endl;
string reply;
//get input and convert it to lower case
getline(cin,reply);
reply = reply + " ";
reply = makelower(reply);
//take that reply and do the same thing we did to the song song. We should end up with an array of words.
vector<string> seq = getlyrics(reply);
int seqsize = int32_t(seq.size());
string seq_[seqsize];
int i = 0;
for (auto c : seq)
{
seq_[i] = c;
i++;
}
//make a variable to check for correctness default to 1
int correct = 1;
//for every word in the reply array
for (int i = 0; i<seqsize; i++)
{
//if the random spot is greater than the size of the song, then quit
//because the song is over
if(randspot>ssize)
{
cout<<"The song is over."<<endl;
done = 1;
}
//otherwise check if the our input is equal to the next word in the song array
else
{
//if it is, the reply is still equal to the word in the song array
if( seq_[i] == song_[randspot+1])
{
correct = 1*correct;
}
//if any word in the sequence is incorrect, then the reply is incorrect
else
{
correct = 0*correct;
}
//iterate through the next word from randspot
randspot++;
}
}
//if the whole sequence is correct, reward a certain amount of points
//wait for user input
if(correct == 1)
{
int anykey;
points = points + reply.length()*15;
cout<<"Correct, you gained "<<reply.length()*15<<" points."<<endl;
cout<<"You now have:"<<points<<" points"<<endl<<"Press any key to continue.";
cin>>anykey;
done = 1;
}
//if some of the sequence is incorrect reply with Incorrect lyrics and wait for user input
else
{
int anykey;
cout<<endl<<"Incorrect lyrics!"<<endl;
cin>>anykey;
done = 1;
}

}


}

}

}

return 0;
}


asked 27 secs ago







Lyric Game Bugs C++

Aucun commentaire:

Enregistrer un commentaire