Sign in to follow this  
moneal2001

help developing better coding

Recommended Posts

moneal2001    768
if anyone could be kind enough to download my number guessing game source code and give me some feedback on how to improve the structure and general coding. it can be downloaded here: [url="http://www.mediafire.com/?4zlc9wipcsdp1"]http://www.mediafire.../?4zlc9wipcsdp1[/url] I can post the code here if no one wants to download it. any help would be greatly appreciated.


edit:

here is my main code

[code]#include "stdafx.h"
using namespace std;
string anWords[] = { "town", "house", "slant", "light", "orange", "time", "workout", "jumprope", "baby", "eraser"};
string word = "";
int misses = 6;
char playAgain = 'y';
int win = 0;

int _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));

while (playAgain != 'n')
{ word = anWords[nrand(0, 9)];
StartGame(word);
while (win != 1 && win != 2)
win = PlayGame(word, misses);
GameOver(win, word);
win = 0;
playAgain = PlayAgain(playAgain);

}

return 0;
}[/code]

this is my function code, was mostly doing this to see if i knew how to use multiple code files and headers:

[code]#include "stdafx.h"
using namespace std;
void StartGame(string word)
{
int s = word.length();
cout << "I have a word in my head. It is " << s << " letters long can you gess it.?" << endl;
}
char PlayAgain(char playAgain)
{

cout << "Play again? y or n " << endl;
cin >> playAgain;
while (playAgain != 'y' && playAgain != 'n')
{
cout << "Choice was not entered correctly please enter y or n again? ";
cin >> playAgain;
}

return playAgain;
}
int PlayGame(string word, int m )
{
int win = 0;
char guess;
int wordSize = word.length();
string guesses;
string playerWord;
for (int i = 0; i < wordSize; ++i)
{
playerWord += "_";
}
string tempWord = playerWord;
cout << playerWord << endl;
while (win != 1 && win != 2)
{
cout << "You have " << m << " guesses left." << endl;
cout << "Please guess a letter. " << endl;
cin >> guess;
unsigned int x;
for( x = 0; x < guesses.length(); ++x)
{
if (guess == guesses[x])
{
cout << "You have already guessed that letter." << endl;
cin >> guess;
break;
}
}
int i;
for (i = 0; i < wordSize; ++i)
{
if (guess == word[i])
playerWord[i] = guess;
}
if (playerWord == tempWord)
{
--m;
tempWord = playerWord;
}
else
tempWord = playerWord;

guesses += guess;
if (playerWord == word)
return 1;
else if (m == 0)
return 2;
else
{
cout << playerWord << endl;
cout << "You have guessed these letters so far: " << guesses << endl;
}

}
return 0;
}
void GameOver(int win, string word)
{
if (win == 1)
{
cout << "You won way to go." << endl;
cout << "You were right. The word was " << word << "." << endl;
}
else
{

cout << "Sorry you lost." << endl;
cout << "The word was " << word << "." << endl;
}
}[/code]

Share this post


Link to post
Share on other sites
Serapth    6671
Well, I tried, mediafire downloads are really freaking annoying. You will get a much better response if you pick a few key source files and post them here within CODE tags.

Share this post


Link to post
Share on other sites
moneal2001    768
[quote name='Serapth' timestamp='1329937373' post='4915600']
Well, I tried, mediafire downloads are really freaking annoying. You will get a much better response if you pick a few key source files and post them here within CODE tags.
[/quote]

Ill do that then everyone disregard that link and ill post it here when i get to my comp that has the code on it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this