Share c++ hangman source code

lyviet.tan

New member
### C ++ Mã nguồn Hangman

**Tổng quan**

Bài viết này cung cấp mã nguồn cho một trò chơi Hangman đơn giản được viết bằng C ++.Trò chơi được chơi bằng cách đoán các chữ cái trong một từ bí mật.Nếu người chơi đoán một chữ cái một cách chính xác, nó được tiết lộ trong từ.Nếu người chơi đoán một chữ cái không chính xác, một bộ phận cơ thể sẽ được thêm vào bản vẽ Hangman.Trò chơi kết thúc khi người chơi đoán từ bí mật hoặc người treo cổ được hoàn thành.

**Mã nguồn**

Sau đây là mã nguồn cho trò chơi Hangman:

`` `C ++
#include <Istream>
#include <String>

sử dụng không gian tên STD;

// từ bí mật để đoán.
chuỗi const secret_word = "Hangman";

// Số lượng các bộ phận cơ thể để vẽ.
const int num_body_parts = 6;

// Bản vẽ của người treo cổ.
const char* Hangman_Drawing [] = {
" +--- +",
" / |",
" / |",
" / |",
" / |",
" / |",
" /__ |",
};

// Hàm để vẽ người treo cổ.
void drawHangman (int numbodypartsdrawn) {
for (int i = 0; i <numbodypartSdrawn; i ++) {
cout << Hangman_Drawing << endl;
}
}

// Chức năng để có được một lá thư từ người chơi.
char getLetterFromplayer () {
thư char;
cout << "Nhập một chữ cái:";
cin >> thư;
thư trở lại;
}

// Hàm để kiểm tra xem một chữ cái có trong từ bí mật không.
Bool IsletterInsecretword (Char Letter, Const String & Secretword) {
for (int i = 0; i <secretword.length (); i ++) {
if (secretword == thư) {
trả lại đúng;
}
}
trả lại sai;
}

// Hàm để cập nhật bản vẽ Hangman.
void updateHangMandRawing (int numbodypartsdrawn) {
Drawhangman (numbodypartsdrawn);
}

// Chức năng để chơi trò chơi Hangman.
void playHangman () {
// Khởi tạo số lượng các bộ phận cơ thể được rút ra.
int numbodypartsdrawn = 0;

// Bắt đầu vòng lặp trò chơi.
while (true) {
// Nhận một lá thư từ người chơi.
Thư char = getLetterFromplayer ();

// Kiểm tra xem chữ cái nằm trong từ bí mật.
if (isletterInsecretword (chữ cái, secret_word)) {
// Bức thư nằm trong từ bí mật, vì vậy hãy cập nhật bản vẽ.
Cập nhậtHangMandRawing (numbodypartsdrawn);
} khác {
// Bức thư không nằm trong từ bí mật, vì vậy hãy thêm một phần cơ thể vào bản vẽ.
numbodypartsdrawn ++;
}

// Kiểm tra xem trò chơi đã kết thúc.
if (numbodypartsdrawn == num_body_parts) {
// Trò chơi đã kết thúc, vì vậy người chơi đã thua.
cout << "Bạn thua!"<< Endl;
phá vỡ;
} if if (secret_word.find (thư) == chuỗi :: npos) {
// Trò chơi đã kết thúc, vì vậy người chơi đã thắng.
cout << "Bạn thắng!"<< Endl;
phá vỡ;
}
}
}

// Hàm chính.
int main () {
// Chơi trò chơi Hangman.
PlayHangman ();

trả lại 0;
}
`` `

** Đầu ra **

Sau đây là một ví dụ về đầu ra của trò chơi Hangman:

`` `
Nhập một lá thư: A

+---+
/ |
/ |
/ |
/ |
/ |
/__ |

Nhập
=======================================
### C++ Hangman Source Code

**Overview**

This article provides the source code for a simple hangman game written in C++. The game is played by guessing letters in a secret word. If the player guesses a letter correctly, it is revealed in the word. If the player guesses a letter incorrectly, a body part is added to a hangman drawing. The game ends when the player either guesses the secret word or the hangman is completed.

**Source Code**

The following is the source code for the hangman game:

```c++
#include <iostream>
#include <string>

using namespace std;

// The secret word to be guessed.
const string SECRET_WORD = "hangman";

// The number of body parts to draw.
const int NUM_BODY_PARTS = 6;

// The drawing of the hangman.
const char* HANGMAN_DRAWING[] = {
" +---+",
" / |",
" / |",
" / |",
" / |",
" / |",
" /__|",
};

// The function to draw the hangman.
void drawHangman(int numBodyPartsDrawn) {
for (int i = 0; i < numBodyPartsDrawn; i++) {
cout << HANGMAN_DRAWING << endl;
}
}

// The function to get a letter from the player.
char getLetterFromPlayer() {
char letter;
cout << "Enter a letter: ";
cin >> letter;
return letter;
}

// The function to check if a letter is in the secret word.
bool isLetterInSecretWord(char letter, const string& secretWord) {
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord == letter) {
return true;
}
}
return false;
}

// The function to update the hangman drawing.
void updateHangmanDrawing(int numBodyPartsDrawn) {
drawHangman(numBodyPartsDrawn);
}

// The function to play the hangman game.
void playHangman() {
// Initialize the number of body parts drawn.
int numBodyPartsDrawn = 0;

// Start the game loop.
while (true) {
// Get a letter from the player.
char letter = getLetterFromPlayer();

// Check if the letter is in the secret word.
if (isLetterInSecretWord(letter, SECRET_WORD)) {
// The letter is in the secret word, so update the drawing.
updateHangmanDrawing(numBodyPartsDrawn);
} else {
// The letter is not in the secret word, so add a body part to the drawing.
numBodyPartsDrawn++;
}

// Check if the game is over.
if (numBodyPartsDrawn == NUM_BODY_PARTS) {
// The game is over, so the player has lost.
cout << "You lose!" << endl;
break;
} else if (SECRET_WORD.find(letter) == string::npos) {
// The game is over, so the player has won.
cout << "You win!" << endl;
break;
}
}
}

// The main function.
int main() {
// Play the hangman game.
playHangman();

return 0;
}
```

**Output**

The following is an example of the output of the hangman game:

```
Enter a letter: a

+---+
/ |
/ |
/ |
/ |
/ |
/__|

Enter a
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top