First, thanks for advices.
About <ctype>, it was a typing mistake, sorry.
I corrected time.
I used <string> in this edition, but I think it could be used in a better way, no?
I am currently learning from a book named "A Beginners' C++", that is really a good book. Its teaching method is very good. It teaches how to think about coding and how to design programs. Unfortunately it has a bad style. It uses <iostream.h> for example, and some of its codes are considered wrong:
Code:
for(int i = 0 ; i < n ; i++) {…}
for(i = 33 ; i < m ; i++) {…}
It works fine in VC++6, but is non standard. It also says you can't make externs const.
I downloaded this book from the web. I also downloaded Bruce Eckel book and I want to read it after this one. Farsi translations of computer books are completely unreadable because of very bad translation (at least I can't understand them). So I am limited to free English books in the web.
Anyway, what should I do with _getche()? How can I make it portable? I don't know any method to the same job.
I included .exe file for those who can't compile it.
This is the new edition:
Code:
#include<iostream>
#include<cctype>
#include<string>
#include<iomanip>
#include<ctime>
#include<conio.h>
using namespace std;
const char ESC_KEY = 27;
const char BS_KEY = 8;
//Initializes screen and makes a random generated secret word
void initSecretWord(string &secretWord, const int CHARNUM){
srand(time(NULL));
secretWord.resize(CHARNUM);
for(int i = 0; i<CHARNUM;i++){
char rnd =(char)(rand()%26) + 'a';
secretWord[i] = rnd;
}
}
//Gets a guess word from player
bool getUserGuess(string &guessedWord, const int CHARNUM){
guessedWord.resize(CHARNUM);
for(int curserPos = 0 ; curserPos < CHARNUM; curserPos++){
char tempGuessChar =(char) _getch(); //What should I do with this?
if(tempGuessChar == ESC_KEY) return true; //Escape to exit program
//Making back-space functional
if(tempGuessChar == BS_KEY && curserPos>0){
curserPos--;
cout.put(BS_KEY);
cout.put(' ');
cout.put(BS_KEY);
}
if((tempGuessChar > 0)&& isalpha(tempGuessChar)&& !isdigit(tempGuessChar)){
tempGuessChar = (char)tolower(tempGuessChar);
cout.put(tempGuessChar);
guessedWord[curserPos] = tempGuessChar;
}
else curserPos--;
}
return false;
}
//Compares guessed work with secret word.
//In addition it returns true if the guessed and secret words matched.
int compare(string &secretWord, string &guessedWord, int &missPlaced, const int CHARNUM){
int correctPlace = 0;
missPlaced = 0;
string tempSecretWord;
tempSecretWord=secretWord;
for(int i = 0 ;i<CHARNUM;i++)
if(tempSecretWord[i] == guessedWord[i]){
tempSecretWord[i] = '\0';
correctPlace++;
}
for(int i=0 ;i<CHARNUM;i++)
for(int j=0;j<CHARNUM;j++)
if(tempSecretWord[i] == guessedWord[j]){
tempSecretWord[i] = '\0';
missPlaced++;
}
return correctPlace;
}
int playGame(string &secretWord, const int PADLEN, const int GAME_ROUNDS, const int CHARNUM){
cout.fill('-');
cout<<"Press ESC to see the secret combination and exit."<<endl
<<endl<<"Guess"<<setw(PADLEN)<<"Correct"<<setw(PADLEN)<<"Guess#"
<<setw(PADLEN)<<"Misplaced"<<endl;
string guessWord;
int correctPlace, missPlaced;
for(int round=1 ; round <= GAME_ROUNDS ; round++){
if(getUserGuess(guessWord, CHARNUM)) break;
correctPlace = compare(secretWord, guessWord, missPlaced, CHARNUM);
if( correctPlace == CHARNUM){ //If true, player won
return round;
}
cout<<setw(PADLEN-2)<<correctPlace<<setw(PADLEN)<<round
<<setw(PADLEN)<<missPlaced<<endl;
}
//Player lost or escaped!
return 0;
}
int main(){
const int gPADLEN = 14; //For aligning output
const int gCHARNUM = 3; //Number of characters to be guessed
const int gGAME_ROUNDS = 40;
string secretWord;
initSecretWord(secretWord, gCHARNUM);
int round = playGame(secretWord, gPADLEN, gGAME_ROUNDS, gCHARNUM);
if( round ){
cout<<endl<<"Correct! ";
const int SCORE = gGAME_ROUNDS / 3;
if(round < SCORE)
cout<<"Very good, you are very smart."<<endl;
else if ((round >= SCORE) && (round <(SCORE * 2)))
cout<<"Not bad."<<endl;
else
cout<<"Not good, you should try to guess faster."<<endl;
}
else
cout<<endl<<"You lost! The word was: "<<secretWord;
cout<<endl<<"Press a key to exit...";
_getch();
return 0;
}