C++ Code For Hangman Game in Spanish
1. Start by learning the basic Spanish vocabulary for programming.
2. Translate the C++ keywords, such as “if” and “while,” into Spanish.
3. Adjust the code to use Spanish words for functions, variables, and other elements.
4. Test your code with a Spanish-speaking audience for accuracy and clarity.
5. Embrace the challenges of coding in a foreign language, as it can enhance your language skills and broaden your horizons.
Hangman is a popular word guessing game that can be implemented in various programming languages, including C++. In this article, we will provide a basic C++ code for a Hangman game and show how to say it in Spanish.
C++ Code for Hangman Game:
Below is a simple C++ code for a Hangman game:
“`cpp
#include
#include
#include
#include
#include
using namespace std;
int main() {
const int MAX_WRONG = 8; // maximum number of incorrect guesses allowed
vector
words.push_back(“hangman”);
words.push_back(“programming”);
words.push_back(“computer”);
words.push_back(“language”);
srand(static_cast
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; // word to guess
int wrong = 0; // number of incorrect guesses
string soFar(THE_WORD.size(), ‘-‘); // word guessed so far
string used = “”; // letters already guessed
cout << "Welcome to Hangman. Good luck!n"; // main game loop while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) { cout << "nnYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.n"; cout << "nYou've used the following letters:n" << used << endl; cout << "nWord so far: " << soFar << endl; char guess; cout << "nEnter your guess: "; cin >> guess;
guess = tolower(guess); // convert to lowercase
while (used.find(guess) != string::npos) {
cout << "nYou've already guessed " << guess << endl; cout << "Enter your guess: "; cin >> guess;
guess = tolower(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos) {
cout << "That's right! " << guess << " is in the word.n"; // update soFar to include the guessed letter for (int i = 0; i < THE_WORD.length(); ++i) { if (THE_WORD[i] == guess) { soFar[i] = guess; } } } else { cout << "Sorry, " << guess << " is not in the word.n"; ++wrong; } } // end of game if (wrong == MAX_WRONG) { cout << "You've been hanged! The word was " << THE_WORD << endl; } else { cout << "Congratulations! You guessed the word: " << THE_WORD << endl; } return 0; } “`
Saying C++ Code for Hangman Game in Spanish:
To say the C++ code for a Hangman game in Spanish, you can simply translate the comments and variable names into Spanish. Here is the same code with Spanish comments:
“`cpp
#include
#include
#include
#include
#include
using namespace std;
int main() {
const int MAX_WRONG = 8; // máximo número de intentos incorrectos permitidos
vector
palabras.push_back(“ahorcado”);
palabras.push_back(“programación”);
palabras.push_back(“computadora”);
palabras.push_back(“lenguaje”);
srand(static_cast
random_shuffle(palabras.begin(), palabras.end());
const string LA_PALABRA = palabras[0]; // palabra a adivinar
int incorrecto = 0; // número de intentos incorrectos
string hastaAhora(LA_PALABRA.size(), ‘-‘); // palabra adivinada hasta ahora
string usados = “”; // letras ya adivinadas
cout << "Bienvenido al juego del ahorcado. ¡Buena suerte!n"; // bucle principal del juego while ((incorrecto < MAX_WRONG) && (hastaAhora != LA_PALABRA)) { cout << "nnTe quedan " << (MAX_WRONG - incorrecto) << " intentos incorrectos.n"; cout << "nHas usado las siguientes letras:n" << usados << endl; cout << "nPalabra hasta ahora: " << hastaAhora << endl; char intento; cout << "nIngresa tu intento: "; cin >> intento;
intento = tolower(intento); // convertir a minúscula
while (usados.find(intento) != string::npos) {
cout << "nYa has intentado con " << intento << endl; cout << "Ingresa tu intento: "; cin >> intento;
intento = tolower(intento);
}
usados += intento;
if (LA_PALABRA.find(intento) != string::npos) {
cout << "¡Correcto! " << intento << " está en la palabra.n"; // actualizar hastaAhora para incluir la letra adivinada for (int i = 0; i < LA_PALABRA.length(); ++i) { if (LA_PALABRA[i] == intento) { hastaAhora[i] = intento; } } } else { cout << "Lo siento, " << intento << " no está en la palabra.n"; ++incorrecto; } } // fin del juego if (incorrecto == MAX_WRONG) { cout << "¡Has sido ahorcado! La palabra era " << LA_PALABRA << endl; } else { cout << "¡Felicidades! Adivinaste la palabra: " << LA_PALABRA << endl; } return 0; } “`
With the above code, you can create a Hangman game in C++ and say it in Spanish. Have fun playing and learning!
Leave a Reply
You must be logged in to post a comment.