Bonjour, alors voici mon pb :
pDecrypter = new Computer(duplicates)
un nouvel objet de type Computer est créé
Computer::Computer(bool dupes):
Decrypter(dupes), total(0), mySmartString(0)
{
}
cela appelle constr Decrypter:ecrypter(bool dupes):
round(0),
duplicates (dupes)
{
}
et les variables membres mySmartString et total sont initialisées à 0
Ensuite il ya pDecrypter->Play();
A ce moment là pdecrypter affiche seulement les variables protected(de la classe DECRYPTER)
cependant il va bien dans Computer:lay
if (!mySmartString)
mySmartString = new SmartString(duplicates);
et là je me rends compte avec stupéfaction que mySmartString est rempli avec n'importe quoi!!! et ça me plante tout!! segmentation defaultPouvez vous m'aider et me dire ce qui s'est passé.
La variable total contient également n'importe quoi
En fait juste avant la ligne pDecrypter->Play (qui est dans game.cpp), pDecrypter est considéré alors comme un objet de type DECRYPTER avec la definition de DECRYPTER ( c normal qu'il ne soit pas considéré comme computer?)et quand je me debranche dans COMPUTER :: PLAY, alors le pointeur this est considéré comme un objet COMPUTER mais là "smartstring" ET "total" sont initialisés avec n'importe quoi.
J'ai donc appremment un pb de relation entre les classes Computer et Decrypter mais je ne vois pas c equi ne marche pas
Les classes human et computer sont des classes dérivées de Decrypter.
Voici le code en totalité:
1a) la classe Game
[code]
#ifndef GAME_HPP
#define GAME_HPP
#include "defvals.hpp"
#include "Gues1205.hpp"
#include "SmartString1201.hpp"
class Guess;
class Decrypter;
class Game
{
public:
Game();
~Game() {}
void Play();
static int howManyLetters;
static int howManyPositions;
private:
void DisplayTime(int secs);
bool VerifyComputerChoices();
bool duplicates;
Decrypter * pDecrypter;
};
#endif
[/code]
1b) l'implémentation de la classe Game
[code]
#include "game1103.hpp"
#include "defvals.hpp"
#include "decrypter1101.hpp"
#include "human1105.hpp"
#include "computer1109.hpp"
#include "SmartString1201.hpp"
#include<time.h>
#include<windows.h>
using namespace std;
char * Accent(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}
int Game::howManyLetters = 0;
int Game::howManyPositions = 0 ;
Game::Game()
{
for( ;; )
{
// Récupération du choix de l'utilisateur
// pour le nombre de lettres possible
while (howManyLetters < minLetters
|| howManyLetters > maxLetters)
{
cout << "Nombre de lettres ? (";
cout << minLetters << "-" << maxLetters << ") : ";
cin >> howManyLetters;
if ( howManyLetters < minLetters
|| howManyLetters > maxLetters)
{
cout << "Veuillez saisir un nombre compris entre ";
cout << minLetters <<" et " << maxLetters <<endl;
}
}
// Récupération du choix de l'utilisateur
// pour le nombre de positions
while (howManyPositions < minPos
|| howManyPositions > maxPos)
{
cout << "Nombre de positions ? (";
cout << minPos << "-" << maxPos << ") : ";
cin >> howManyPositions;
if ( howManyPositions < minPos
|| howManyPositions > maxPos)
{
cout << "Veuillez saisir un nombre de positions compris entre ";
cout << minPos << " et " << maxPos << endl;
}
}
char choice = ' ';
while ( choice != 'o' && choice != 'n' )
{
cout << "Permettre les doublons (o/n) ? ";
cin >> choice;
}
duplicates = choice == 'o'? true: false;
if ( ! duplicates
&& howManyPositions > howManyLetters)
{
cout << "Impossible de mettre " << howManyLetters << " lettres dans ";
cout << howManyPositions << " positions sans doublons !\n";
cout << Accent("Essayez à nouveau ... \n");
howManyLetters = 0; howManyPositions = 0 ;
continue;
}
choice = ' ';
while( choice!= 'h' && choice != 'c' )
{
cout << "Qui devine. (H)umain";
cout << " ou (C)alculateur ? (h/c)? ";
cin >> choice;
}
bool ok = choice == 'h' ?
true : VerifyComputerChoices();
if (ok)
{
if (choice == 'h')
pDecrypter = new Human(duplicates);
else
pDecrypter = new Computer(duplicates);
break;
}
}
}
void Game::DisplayTime(int totalSeconds)
{
int totalDays = totalSeconds / SecondsInDay;
int totalHours = totalSeconds / SecondsInHour;
int totalMinutes = totalSeconds / SecondsInMinute;
if ( totalDays > 1)
cout << totalDays << " jours ! " ;
else
if ( totalHours > 1)
cout << totalHours << " heures ! " ;
else
if ( totalMinutes > 1)
cout << totalMinutes << " minutes . " ;
else
cout << totalSeconds << " secondes. " ;
}
void Game::Play()
{
int start = time (NULL );
pDecrypter->Play();
//indique le temps passé
int end = time(NULL );
int totalSeconds = end - start;
cout << "\nTemps total passé, pour ce jeu : ";
DisplayTime(totalSeconds);
cout <<"\n";
howManyLetters = 0;
howManyPositions = 0;
}
bool Game::VerifyComputerChoices()
{
int totalGuesses = 1;
if (duplicates)
for(int i = 0; i < howManyPositions;i++)
totalGuesses *=howManyLetters;
else
for ( int i = howManyLetters; i > howManyLetters - howManyPositions;
i-- )
totalGuesses *=i;
int totalSeconds = totalGuesses/GUESSES_PER_SECOND;
if (totalSeconds > 2)
{
cout << "\n\nVous me demandez de deviner ";
cout << "parmi ";
cout << totalGuesses;
cout << " combinaisons possibles. " ;
cout << "\nJe peux venir à bout d'environ ";
cout << GUESSES_PER_SECOND;
cout << " combinaisons par seconde. ";
cout << Accent("Si l'énigme est difficile, ");
cout << "\nune seule proposition peut me prendre plus de ";
DisplayTime(totalSeconds);
char confirm = ' ';
while ( confirm != 'o' && confirm != 'n')
{
cout << Accent("\n\nEtes-vous sûr de vouloir continuer (o/n) ? ");
cin >> confirm;
}
if (confirm == 'n')
{
howManyLetters = 0 ;
howManyPositions = 0 ;
return false;
}
}
else
{
cout <<"Choix parmi " << totalGuesses;
cout << " combinaisons possibles...\n\n";
}
return true;
}
[/code]
2a) la classe Computer
[code]
#ifndef COMPUTER_HPP
#define COMPUTER_HPP
#include "SmartString1201.hpp"
#include "decrypter1101.hpp"
class SmartString;
class Computer : public Decrypter
{
public:
Computer (bool duplicates);
virtual ~Computer();
bool HandleFlag(char flag);
void Play() ;
private:
void GenerateAGuess();
bool IsConsistent(vector<char> guess);
Guess OfferGuess();
void ShowHelp();
SmartString * mySmartString;
int total;
};
#endif
[/code]
2b) l'implémentation de la classe Computer
[code]
#include "computer1109.hpp"
#include "game1103.hpp"
#include "SmartString1201.hpp"
#include<windows.h>
using namespace std;
char * AccentC(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}
Computer::Computer(bool dupes):
Decrypter(dupes), total(0), mySmartString(0)
{
}
Computer :: ~Computer()
{
}
void Computer::GenerateAGuess()
{
bool ok = true;
int counter = 0;
int start = time(NULL );
do
{
counter++;
total++;
if (counter % 10000 == 00)
cout << ".";
ok = mySmartString->GetNext();
if (!ok)
{
cout << "Quelquechose ne va pas !";
cout << " Recommencez\n";
round = 0;
delete mySmartString;
mySmartString = new SmartString(duplicates);
ShowHistory();
cout <<"\n\n";
history.clear();
continue;
}
} while ( !IsConsistent(mySmartString->GetString()) );
int end = time(NULL );
int seconds = end-start;
cout << "(" << counter;
cout << AccentC(" chaînes éliminées pour ce tour ; ");
cout << total << " total.)";
if (seconds > 1)
cout << "(" << seconds << " secondes]";
cout << "\n";
}
// Gestion des indicateurs de l'utilisateur ( -?,-<, etc.)
bool Computer::HandleFlag(char flag)
{
bool quit =false;
switch (flag)
{
case 's':
ShowHistory();
break;
case '?':
ShowHelp();
break;
case 'q':
quit = true;
break;
default:
cout << AccentC( "\nIndicateur inconnu.Ignoré.\n");
break;
}
return quit ;
}
bool Computer::IsConsistent(vector<char> theGuess)
{
if (!duplicates)
{
for(
vector<char>::const_iterator it =
theGuess.begin();
it!= theGuess.end();
it++)
{
int HowMany =
count(theGuess.begin(),theGuess.end(), *it);
if (HowMany > 1)
return false;
}
}
bool isValid = true ;
int correct;
int position;
for ( vector<Guess>::const_iterator it=
history.begin();
it != history.end();
it++)
{
vector <char> temp = it->GetString();
correct = 0;
position = 0;
for(int i = 0; i < Game::howManyLetters; i++)
{
int howManyInGuess =
count (theGuess.begin(),theGuess.end(),alpha[i]);
int howManyInAnswer =
count (temp.begin(),temp.end(),alpha[i]);
correct += min (howManyInGuess,howManyInAnswer);
}
for(int i = 0; i < Game::howManyPositions; i++)
{
if ( theGuess[i] = temp[i])
position ++;
}
if ( correct != it->GetScore().first ||
position != it->GetScore().second )
{
isValid = false;
break;
}
}
return isValid;
}
Guess Computer::OfferGuess()
{
vector<char> theGuess =
mySmartString->GetString();
round++;
int numCorrect, numInPosition;
cout <<"\n";
Display(theGuess);
cout << "Essai " << round << ". ";
cout << "Veuillez donner le score. \t";
cout << "Combien sont correctes ? ";
cin >> numCorrect;
cout << AccentC("\t\t\tVombien sont bien placées ?: ");
cin >> numInPosition;
Guess thisGuess(theGuess, numCorrect, numInPosition);
return thisGuess;
}
void Computer::Play()
{
if (!mySmartString)
mySmartString = new SmartString(duplicates);
vector<char> theGuess;
history.clear();
bool deletedCharacters = false;
for (;;)
{
Guess theGuess = OfferGuess();
history.push_back(theGuess);
if ( theGuess.GetScore().second ==
Game::howManyPositions)
break;
if (
! mySmartString->CanEliminateCharacters(theGuess) ||
! IsConsistent(mySmartString->GetString())
)
GenerateAGuess();
};
}
// Le joueur a entré -?
void Computer::ShowHelp()
{
cout << "\t-h Indice\n\t-s Afficher l'historique \n";
cout << "\t-? Aide\n\t-q Quitter\n" << endl;
}
[/code]
3a) la classe Human
[code]
#ifndef HUMAN_HPP
#define HUMAN_HPP
#include "decrypter1101.hpp"
class Human : public Decrypter
{
public:
Human (bool duplicates);
virtual ~Human();
vector<char> GetSolution() const;
bool HandleFlag(char flag);
bool IsValid (char c) const;
void Play() ;
void Score( vector<char> thisGuess, int & correct, int & position);
private:
void ShowHelp();
void ShowHint();
int hintCtr;
vector<char> solution;
};
#endif
[/code]
3b) l'implémentation de la classe Human
[code]
#include "human1105.hpp"
#include "game1103.hpp"
#include<string>
#include<windows.h>
using namespace std;
char * AccentH(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}
Human::Human(bool dupes):
Decrypter(dupes), hintCtr(0)
{
}
Human :: ~Human()
{
}
vector <char> Human::GetSolution() const
{
return solution;
}
// Gestion des indicateurs de l'utilisateur ( -?,-<, etc.)
bool Human::HandleFlag(char flag)
{
bool quit =false;
switch (flag)
{
case 'h':
ShowHint();
break;
case 's':
ShowHistory();
break;
case '?':
ShowHelp();
break;
case '!':
Display(GetSolution());
break;
case 'q':
quit = true;
break;
default:
cout << AccentH( "\nIndicateur inconnu.Ignoré.\n");
break;
}
return quit ;
}
bool Human::IsValid(char c) const
{
bool IsValid = false;
for (int i = 0 ; i < Game::howManyLetters ; i++)
if (alpha[i]== c)
IsValid = true;
return IsValid;
}
void Human::Play()
{
vector<char> thisGuess;
int correct = 0;
int position = 0;
bool quit = false;
round ++;
// alimentation du tableau solution
srand( (unsigned) time (NULL) );
for ( int i = 0 ; i < Game::howManyPositions;)
{ int nextValue = rand () % (Game::howManyLetters);
char theChar = alpha[nextValue];
// lettre déjà existante?
if ( ! duplicates && i > 0 )
{
vector<char>::iterator where =
find(solution.begin(), solution.end(), theChar);
if (where != solution.end())
continue;
}
solution.push_back(theChar);
i++;
}
while ( position < Game::howManyPositions)
{
thisGuess.clear();
string guess;
cout << "\nEssai " << round << ". Entrez -? ou ";
cout << Game::howManyPositions << " lettres entre ";
cout << alpha[0] << " et " << alpha[Game::howManyLetters-1] << " : ";
cin >> guess ;
if (guess[0] == '-')//c'est un indicateur
{
quit = HandleFlag(guess[1]);
if(quit)
break;
continue;
}
if ( guess.length() < Game::howManyPositions)
{
cout << "\n ** Veuillez entrer exactement " ;
cout << Game::howManyPositions << " lettres . ** \n";
continue;
}
bool lineIsValid = true;
for (int i = 0; i < Game::howManyPositions; i++)
{
lineIsValid = IsValid(guess[i]);
if ( ! lineIsValid)
break;
}
//création de la proposition pour l'affichage
if (lineIsValid)
for ( int i =0; i < Game::howManyPositions; i++)
thisGuess.push_back(guess[i]);
else
{
cout << "Veuillez entre uniquement des lettres entre ";
cout << alpha[0] << " et " << alpha[Game::howManyLetters-1] << "\n";
continue;
}
round++ ;
cout << "\nVotre proposition : ";
Display(thisGuess);
//calcul et affichage
Score(thisGuess,correct,position);
cout << "\t\t" << correct << " correcte(s), ";
cout << position << AccentH( " bien placee(s) \n");
// création d'un enregistrement et sauvegarde dans le vector history
Guess thisRound(thisGuess,correct,position);
history.push_back(thisRound);
}
if(!quit)
{
cout << "\nFelicitations ! Il vous a fallu ";
if (round <= 6)
cout << " seulement " ;
if (round-1 == 1)
cout << "un essai ! \n";
else
cout << round-1 << " essais. \n";
}
}
void Human::Score( vector<char> thisGuess, int & correct, int & position)
{
correct = 0;
position = 0;
// pour chaque lettre possible,
// combien sont dans la réponse et la proposition
for ( int i = 0; i < Game::howManyLetters; i++)
{
int howManyinGuess = count ( thisGuess.begin(), thisGuess.end(),
alpha[i]) ;
int howManyinAnswer = count(solution.begin(), solution.end(),
alpha [i]);
correct+= min(howManyinGuess,howManyinAnswer);
}
//Pour chaque proposition
// combien sont dans la solution
for ( int j = 0; j < Game::howManyPositions ; j++)
{ if (thisGuess[j] == solution[j])
position ++;
}
}
// Joueur humain, révéler cahque lettre une par une
void Human::ShowHint()
{
if (hintCtr<Game::howManyPositions)
{
cout << "\nIndice!! Position" << hintCtr+1;
cout <<": " << solution[hintCtr] << endl;
hintCtr++;
}
}
//Si le joueur entre -?
void Human::ShowHelp()
{
cout << "\t-h Indice\n\t-s Afficher l'historique \n";
cout << "\t-? Aide\n\t-q Quitter\n" << endl;
}
[/code]
4a) la classe Decrypter
[code]
#ifndef DECRYP_HPP
#define DECRYP_HPP
#include "defvals.hpp"
#include "Gues1205.hpp"
class Decrypter
{
public:
Decrypter(bool duplicates);
virtual ~Decrypter();
void Display( vector<char> charVec) const;
virtual bool HandleFlag(char flag) = 0 ;
virtual void Play() = 0 ;
virtual void ShowHelp() = 0;
void ShowHistory();
protected:
bool duplicates;
vector<Guess> history;
int round;
};
#endif
[/code]
4b) l'implémentation de la classe Decrypter
[code]
#include "decrypter1101.hpp"
Decrypter::Decrypter(bool dupes):
round(0),
duplicates (dupes)
{
}
Decrypter::~Decrypter()
{
}
void Decrypter::Display(vector<char> charVec) const
{
copy(
charVec.begin(),
charVec.end(),
ostream_iterator<char>(cout," "));
}
void Decrypter::ShowHistory()
{
for (
vector<Guess>::const_iterator it = history.begin();
it != history.end();
it++)
{
it->Display();
}
}
[/code]
5a) la classe SmartSrting
[code]
#ifndef SMARTSTRING_HPP
#define SMARTSTRING_HPP
#include "defvals.hpp"
#include "smartchr1203.hpp"
class Guess;
class SmartString
{
public:
SmartString(bool dupes);
virtual ~SmartString();
bool CanEliminateCharacters(const Guess& theGuess);
bool GetNext();
vector<char> GetString();
bool RemoveCurrentCharacters();
bool RemoveCurrentCharactersInEveryPosition();
private:
void ForceCharacters(const Guess& theGuess);
int CountForcedInGuess(const Guess& theGuess);
int CountUniqueLettersInGuess(const Guess & theGuess);
bool In(vector<char> vec, char target) const;
vector <char> deadCharacters;
bool duplicates;
vector<char> forcedCharacters;
vector<SmartChar> myString;
};
#endif
[/code]
5b) l'implémentation de la classe SmartString
[code]
#include "Gues1205.hpp"
#include "game1103.hpp"
#include "SmartString1201.hpp"
#include<windows.h>
using namespace std;
SmartString::SmartString(bool dupes):
duplicates(dupes)
{
for (int i = 0; i < Game::howManyPositions; i++)
{
int j;
if (duplicates)
j = 0 ;
else
j = i;
SmartChar theChar(j);
myString.push_back(theChar);
}
}
SmartString :: ~SmartString()
{
}
vector<char> SmartString::GetString()
{
vector<char> outString;
for (vector<SmartChar>::iterator it = myString.begin();
it != myString.end();
it ++)
{
char theChar = it->GetChar();
outString.push_back(theChar);
}
return outString;
}
bool SmartString::GetNext()
{
vector<char> outString;
vector<SmartChar>::reverse_iterator rit;
rit = myString.rbegin();
bool rollover = rit->Increment();
while(rollover)
{
rit ++;
if (rit == myString.rend())
return false;
else
{
rollover = rit->Increment();
}
}
return true;
}
// enlève le character se trouvant actuellment
// à une certaine position
bool SmartString::RemoveCurrentCharacters()
{
char theChar;
bool anyDeleted = false;
for (vector<SmartChar>::iterator it = myString.begin();
it != myString.end();
it ++)
{
theChar = it->GetChar();
if (! In(forcedCharacters, theChar) )
{
theChar = it->RemoveCurrent();
// "deadcharacters" ( caractères morts) évite la prise
// en compte de caractères déjà exclus
if ( ! In(deadCharacters, theChar) )
{
deadCharacters.push_back(theChar);
cout << "Elimination de " << theChar;
cout << " de la position actuelle" << endl;
anyDeleted = true;
}
}
}
return anyDeleted;
}
// enlève de n'importe quelle position le caractère
// se trouvant à une certaine position
bool SmartString::RemoveCurrentCharactersInEveryPosition()
{
char theChar;
bool anyDeleted = false;
vector<char> currentGuess;
for (vector<SmartChar>::iterator it = myString.begin();
it != myString.end();
it ++)
{
currentGuess.push_back(it->GetChar());
}
for (vector<char>::iterator itc = currentGuess.begin();
itc != currentGuess.end();
itc ++)
{
theChar = *itc;
if (! In(forcedCharacters, theChar) )
{
for (vector<SmartChar>::iterator it2 = myString.begin();
it2 != myString.end();
it2 ++)
{
it2->Remove(theChar);
if ( ! In(deadCharacters, theChar) )
{
deadCharacters.push_back(theChar);
cout << "Elimination de " << theChar;
anyDeleted = true;
}
}
}
}
return anyDeleted;
}
bool SmartString::CanEliminateCharacters(const Guess &theGuess)
{
bool anyDeleted = false;
ForceCharacters(theGuess);
int forcedInAnswer = CountForcedInGuess(theGuess);
int overall = theGuess.GetScore().first;
int inPos = theGuess.GetScore().second;
if (overall == 0 || overall == forcedInAnswer)
{
anyDeleted = RemoveCurrentCharactersInEveryPosition();
return anyDeleted; // nous avons effectivement éliminé des caractères
}
if (inPos == 0 )
{
anyDeleted = RemoveCurrentCharacters();
return anyDeleted; // nous avons effectivement éliminé des caractères
}
return false;
}
void SmartString::ForceCharacters(const Guess &theGuess)
{
int numDifferentLetters =
CountUniqueLettersInGuess (theGuess);
int score = theGuess.GetScore().first;
if ( score >= numDifferentLetters)
{
vector <char> theString = theGuess.GetString();
for (vector<char>::const_iterator it = theString.begin();
it != theString.end();
it ++)
{
if (In(forcedCharacters, *it) )
forcedCharacters.push_back(*it);
}
}
}
int SmartString::CountUniqueLettersInGuess(const Guess &theGuess)
{
vector<char> temp;
vector <char> theString = theGuess.GetString();
for (vector<char>::const_iterator it = theString.begin();
it != theString.end();
it ++)
{
if (! In(temp, *it) )
temp.push_back(*it);
}
// temp contient maintenant toues les lettres uniques
return temp.size();
}
int SmartString::CountForcedInGuess(const Guess &theGuess)
{
int howManyForcedInGuess = 0;
vector <char> theString = theGuess.GetString();
for (vector<char>::const_iterator it = theString.begin();
it != theString.end();
it ++)
{
if (In(forcedCharacters, *it) )
howManyForcedInGuess ++;
}
return howManyForcedInGuess;
}
bool SmartString::In(vector<char> vec , char target) const
{
vector<char>::iterator where =
find(vec.begin(), vec.end(), target);
return where != vec.end();
}
[/code]
6a) la classe SmartChar
[code]
#ifndef SMARTCHAR_HPP
#define SMARTCHAR_HPP
#include "defvals.hpp"
class SmartChar
{
public:
SmartChar(int letter = 0);
virtual ~SmartChar();
char GetChar() const;
bool Increment();
char RemoveCurrent();
bool Remove(char c);
private:
int myChar;
vector<char> myCharacters;
};
#endif
[/code]
6b) l'implémentation de la classe SmartChar
[code]
#include "game1103.hpp"
#include "smartchr1203.hpp"
#include<windows.h>
using namespace std;
char * AccentSC(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}
SmartChar::SmartChar(int letter):
myChar(letter)
{
for (int i = 0; i < Game::howManyLetters; i++)
{
myCharacters.push_back(alpha[i]);
}
}
SmartChar :: ~SmartChar()
{
}
char SmartChar::GetChar() const
{
return myCharacters[myChar];
}
bool SmartChar::Increment()
{
if (++myChar >= myCharacters.size() )
{
myChar = 0;
return true;
}
return false;
}
char SmartChar::RemoveCurrent()
{
char theChar = ' ';
if (myCharacters.size() > 1)
{
theChar = GetChar();
myCharacters.erase(myCharacters.begin() +myChar);
while ( myChar >= myCharacters.size() )
myChar--;
}
return theChar;
}
bool SmartChar::Remove (char theChar)
{
if (myCharacters.size() > 1)
{
vector<char>::iterator where =
find ( myCharacters.begin(), myCharacters.end(), theChar);
if ( where != myCharacters.end())
myCharacters.erase(where);
return true;
}
return false;
}
[/code]
7a) la classe Guess
[code]
#ifndef GUESS_HPP
#define GUESS_HPP
#include <vector>
#include "defvals.hpp"
using namespace std ;
class Guess
{
public:
Guess();
Guess(vector<char> guess , int howManyRight, int howManyInPosition);
~Guess(){};
pair<int,int> GetScore() const {return score;}
vector<char> GetString() const {return myString;}
void Display() const;
private:
vector<char> myString ;
pair<int,int> score;
};
#endif
[/code]
7b) l'implémentation de la classe Guess
[code]
#include "Gues1205.hpp"
Guess::Guess(vector<char>guess, int howManyRight, int howManyInPosition):
myString(guess),
score(howManyRight, howManyInPosition)
{
}
void Guess::Display() const
{
copy ( myString.begin(),
myString.end(),
ostream_iterator<char>(cout, " ") );
cout << "\t";
cout << score.first;
cout << " correcte(s), ";
cout << score.second;
cout << " bien placee(s) \n";
}
[/code]
8a) la classe defvals
[code]
#ifndef DEFVALS_HPP
#define DEFVALS_HPP
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <time.h>
#include <utility>
using namespace std;
const char alpha[]= "abcdefghijklmnopqrstuvwxyz";
const int minPos = 2;
const int maxPos = 10;
const int minLetters = 2;
const int maxLetters = 26;
const int SecondsInMinute = 60;
const int SecondsInHour = SecondsInMinute * 60;
const int SecondsInDay = SecondsInHour * 24;
const int GUESSES_PER_SECOND = 10000;
const int szInt = sizeof(int);
const int szChar = sizeof(char);
const int szBool = sizeof(bool);
#endif
[/code]
9b) le programme Main
[code]
#include <iostream>
# include <windows.h>
//programme gestionnaire pour montrer comment créer un jeu et démontre
// et comment l'ordinateur devine la solution
#include "defvals.hpp"
#include "game1103.hpp"
#include "Gues1205.hpp"
using namespace std;
char * AccentD(const char * mess) {
static char retour [80];
CharToOem (mess,retour);
return retour;
}
int main()
{
cout << "Decryptix. (c)Copyright 2000 Liberty ";
cout << "Associates, Inc. Version 11 \n\n " << endl;
cout << AccentD("Decryptix. (c) Copyright 2000 Liberty ");
cout << AccentD("Associates, Inc. Version 0.2\n ")<< endl;
cout << AccentD("Il y a deux façons de jouer à Decryptix : ");
cout << AccentD("en devinant un modèle créé par l'ordinateur, ");
cout << AccentD("ou en laissant l'ordinateur deviner le vôtre. \n");
cout << AccentD("Si vous devinez, l'ordinateur va penser à une\n");
cout << AccentD("suite de lettres (e.g., abcde).\n\n");
cout << AccentD("A chaque tour, vous faites une proposition et\n");
cout << AccentD("l'ordinateur vous dit combien de lettres sont \n");
cout << AccentD("correctes, et combien parmi celles-ci occupent\n");
cout << AccentD("la position exacte dans le modèle.\n\n");
cout << AccentD("Le but est de décoder l'énigme aussi rapidement \n");
cout << AccentD("que possible. Vous décidez combien de lettres \n");
cout << AccentD("peuvent êre utilisées et le nombre de positions\n");
cout << AccentD("(e.g., 5 lettres possibles dans 4 positions) \n");
cout << AccentD("ainsi que si le modèle peut ou non contenir \n");
cout << AccentD("des lettres en double (e.g., aabcd).\n\n");
cout << AccentD("Si l'ordinateur devine, vous pensez à une suite de\n");
cout << AccentD("lettres et donnez le score de chaque réponse.\n\n")
<< endl;
bool playAgain = true;
while ( playAgain )
{
char choice = ' ';
Game * g = new Game;
g->Play();
/* cout << "\nLa reponse : ";
g->Display(g->GetSolution());
cout << "\n\n" << endl;
*/
delete g;
while ( choice != 'o' && choice != 'n' )
{
cout << "\nRejouez (o/n) ? : ";
cin >> choice;
}
playAgain = choice == 'o'? true: false;
}
system ( "PAUSE");
return 0;
}
[/code]
Je vous remercie pour votre aide