Skip to content

Commit 4753cce

Browse files
committed
start to separate main file in others files
1 parent 2dd5cb5 commit 4753cce

25 files changed

+265
-167
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
main.exe
1+
main.exe
2+
.vscode
3+
*.o

add_new_word.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include "read_file.hpp"
5+
#include "save_file.hpp"
6+
7+
8+
void add_new_word()
9+
{
10+
std::cout << "Type the new word in capital letters: ";
11+
std::string new_word;
12+
std::cin >> new_word;
13+
14+
std::vector<std::string> words = read_file();
15+
words.push_back(new_word);
16+
17+
save_file(words);
18+
}

add_new_word.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void add_new_word();

draw_word.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <vector>
2+
#include <string>
3+
#include <cstdlib>
4+
#include <ctime>
5+
#include "read_file.hpp"
6+
7+
extern std::string secret_word;
8+
9+
void draw_word()
10+
{
11+
std::vector<std::string> words = read_file();
12+
13+
srand(time(NULL));
14+
int rand_number = rand() % words.size();
15+
16+
secret_word = words[rand_number];
17+
18+
}

draw_word.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void draw_word();

guess.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <vector>
4+
#include "have_letter.hpp"
5+
6+
extern std::map<char, bool> tried;
7+
extern std::vector<char> wrong_guesses;
8+
9+
void guess()
10+
{
11+
char guess;
12+
13+
std::cout << "Guess one letter: ";
14+
std::cin >> guess;
15+
16+
tried[guess] = true;
17+
18+
if(have_letter(guess))
19+
{
20+
std::cout << "The letter " << guess << " was in the secret word\n\n";
21+
}else
22+
{
23+
std::cout << "The letter " << guess << " wasn't in the secret word\n\n";
24+
wrong_guesses.push_back(guess);
25+
}
26+
}

guess.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void guess();

hanged.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <vector>
2+
3+
extern std::vector<char> wrong_guesses;
4+
5+
bool hanged()
6+
{
7+
return wrong_guesses.size() > 5;
8+
}

hanged.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
bool hanged();

have_letter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <string>
2+
3+
extern std::string secret_word;
4+
5+
bool have_letter(char guess)
6+
{
7+
for(char letter : secret_word)
8+
{
9+
if(guess == letter)
10+
{
11+
return true;
12+
}
13+
}
14+
return false;
15+
}

have_letter.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
bool have_letter(char guess);

header_print.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <iostream>
2+
3+
4+
void header_print()
5+
{
6+
std::cout << "***************************************\n";
7+
std::cout << "*** Welcome to the Hangman Game ***\n";
8+
std::cout << "***************************************\n\n";
9+
}

header_print.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void header_print();

main.cpp

Lines changed: 14 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,28 @@
55
#include <fstream>
66
#include <ctime>
77
#include <cstdlib>
8-
#include "main.hpp"
8+
// #include "main.hpp"
9+
#include "header_print.hpp"
10+
#include "draw_word.hpp"
11+
#include "hanged.hpp"
12+
#include "win.hpp"
13+
#include "print_wrong_guesses.hpp"
14+
#include "print_secret_word.hpp"
15+
#include "guess.hpp"
16+
#include "add_new_word.hpp"
17+
#include "have_letter.hpp"
18+
#include "read_file.hpp"
19+
#include "save_file.hpp"
20+
921

1022
using namespace std;
1123
string secret_word;
1224
map<char, bool> tried;
1325
vector<char> wrong_guesses;
1426

15-
void save_file(vector<string> new_list)
16-
{
17-
ofstream file;
18-
file.open("words.txt");
19-
20-
if(file.is_open())
21-
{
22-
file << new_list.size() << endl;
23-
24-
for(string word : new_list)
25-
{
26-
file << word << endl;
27-
}
28-
29-
file.close();
30-
}
31-
else
32-
{
33-
cout << "Erro! File can not be open\n";
34-
exit(0);
35-
}
36-
}
37-
38-
vector<string> read_file()
39-
{
40-
ifstream file;
41-
file.open("words.txt");
42-
43-
if(file.is_open())
44-
{
45-
46-
int number_of_words;
47-
file >> number_of_words;
48-
49-
vector<string> words;
50-
51-
for(int i=0; i<number_of_words; i++)
52-
{
53-
string read_word;
54-
file >> read_word;
55-
words.push_back(read_word);
56-
}
57-
58-
file.close();
59-
return words;
60-
}
61-
else
62-
{
63-
cout << "Erro! File can not be open\n";
64-
exit(0);
65-
}
66-
}
67-
6827
int main()
6928
{
70-
print_header();
29+
header_print();
7130

7231
draw_word();
7332

@@ -100,105 +59,3 @@ int main()
10059
}
10160
}
10261
}
103-
104-
bool have_letter(char guess)
105-
{
106-
for(char letter : secret_word)
107-
{
108-
if(guess == letter)
109-
{
110-
return true;
111-
}
112-
}
113-
return false;
114-
}
115-
116-
bool win()
117-
{
118-
for(char letter : secret_word)
119-
{
120-
if(!tried[letter]) return false;
121-
}
122-
return true;
123-
}
124-
125-
bool hanged()
126-
{
127-
return wrong_guesses.size() > 5;
128-
}
129-
130-
void print_header()
131-
{
132-
cout << "***************************************\n";
133-
cout << "*** Welcome to the Hangman Game ***\n";
134-
cout << "***************************************\n\n";
135-
}
136-
137-
void print_wrong_guesses()
138-
{
139-
cout << "Wrong guesses: ";
140-
for(char letter : wrong_guesses)
141-
{
142-
cout << letter << " ";
143-
}
144-
cout << endl;
145-
}
146-
147-
void print_secret_word()
148-
{
149-
for(char letter : secret_word)
150-
{
151-
if(tried[letter])
152-
{
153-
cout << letter << " ";
154-
}else
155-
{
156-
cout << "_ ";
157-
}
158-
}
159-
cout << endl;
160-
}
161-
162-
void guess()
163-
{
164-
char guess;
165-
166-
cout << "Guess one letter: ";
167-
cin >> guess;
168-
169-
tried[guess] = true;
170-
171-
if(have_letter(guess))
172-
{
173-
cout << "The letter " << guess << " was in the secret word\n\n";
174-
}else
175-
{
176-
cout << "The letter " << guess << " wasn't in the secret word\n\n";
177-
wrong_guesses.push_back(guess);
178-
}
179-
}
180-
181-
182-
183-
void draw_word()
184-
{
185-
vector<string> words = read_file();
186-
187-
srand(time(NULL));
188-
int rand_number = rand() % words.size();
189-
190-
secret_word = words[rand_number];
191-
192-
}
193-
194-
void add_new_word()
195-
{
196-
cout << "Type the new word in capital letters: ";
197-
string new_word;
198-
cin >> new_word;
199-
200-
vector<string> words = read_file();
201-
words.push_back(new_word);
202-
203-
save_file(words);
204-
}

main.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
bool have_letter(char guess);
2-
bool win();
3-
bool hanged();
4-
void print_header();
5-
void print_wrong_guesses();
6-
void print_secret_word();
7-
void guess();
8-
void draw_word();
9-
void add_new_word();
1+
// bool have_letter(char guess);
2+
// bool win();
3+
// bool hanged();
4+
// void print_header();
5+
// void print_wrong_guesses();
6+
// void print_secret_word();
7+
// void guess();
8+
// void draw_word();
9+
// void add_new_word();

print_secret_word.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <string>
4+
5+
extern std::string secret_word;
6+
extern std::map<char, bool> tried;
7+
8+
void print_secret_word()
9+
{
10+
for(char letter : secret_word)
11+
{
12+
if(tried[letter])
13+
{
14+
std::cout << letter << " ";
15+
}else
16+
{
17+
std::cout << "_ ";
18+
}
19+
}
20+
std::cout << std::endl;
21+
}

print_secret_word.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void print_secret_word();

print_wrong_guesses.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
extern std::vector<char> wrong_guesses;
5+
6+
void print_wrong_guesses()
7+
{
8+
std::cout << "Wrong guesses: ";
9+
for(char letter : wrong_guesses)
10+
{
11+
std::cout << letter << " ";
12+
}
13+
std::cout << std::endl;
14+
}

print_wrong_guesses.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void print_wrong_guesses();

0 commit comments

Comments
 (0)