1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
// passage par copie car random_shuffle agit sur place
std::string shuffleWord(std::string word) {
std::random_shuffle(word.begin(), word.end());
return word;
}
// retourne l'id du joueur suivant
int nextPlayerId(int current, int nbPlayer) {
return (current+1) % nbPlayer;
}
struct Player {
const std::string name;
int score;
// ctor
explicit Player(const std::string& name_): name(name_), score(0) { }
}
int main() {
const int NB_PLAYERS = 2;
bool keepPlaying = true; // controle la boucle principale
std::vector<Player> players; // tableau contenant tous les joueurs
int idCurrentPlayer = 0; // 0 = joueur 1, 1 = joueur 2, etc...
std::cout << "saisissez vos noms " << endl;
// pour chaque joueur
for(int i=0; i<NB_PLAYERS; ++i) {
std::string name;
// on lui demande de saisir son nom
std::cout << "joueur " << (i+1) << " : ";
std::cin >> name;
// on crée une instance de Player que l'on ajoute au tableau de joueurs
players.emplace_back(name);
// equivalent à
// Player player(name);
// players.push_back(player);
// mais évite des copies
}
// pour remplacer ton label / goto
while(keepPlaying) {
std::string wordToGuess; // mot à deviner
std::string shuffledWord; // le même mot mais mélangé
std::string playerGuess; // la proposition du joueur
// on récupère l'id du joueur suivant (c'est lui qui devra trouver le mot)
int nextPlayer = nextPlayerId(idCurrentPlayer, NB_PLAYERS);
// on utilise le tableau pour récupérer le nom du joueur courant
std::cout << players[idCurrentPlayer].name << " saisissez un mot :";
// on enregistre le mot saisi et on le mélange
std::cin >> wordToGuess;
shuffledWord = shuffleWord(wordToGuess);
// Je te laisse continuer...
// Puis on passe au joueur suivant
idCurrentPlayer = nextPlayer;
}
return 0;
} |
Partager