#include <ctime>
#include <iostream>
using namespace std;
int main() {
string word = "start";
int count = 0;
cout << "끝말잇기 시작: " << word << "\n";
while (true) {
// 입력 받기 전 설정
clock_t startTime = clock();
cout << "단어를 입력하세요: ";
string new_word;
cin >> new_word;
int n = word.size();
// 기존 문자와 입력 문자의 앞 뒷 글자 확인
bool rool = (word[n - 1] == new_word[0]); // new_word.front() 가능
clock_t endTime = clock();
if ((endTime - startTime) / CLOCKS_PER_SEC >= 10) { // TimeAttack 10초
cout << "시간 초과!! 게임 종료. \n입력한 단어 개수: " << count << "\n";
break;
}
// 앞 뒷 글자 일치 할 경우
if (rool) {
word += (" --> " + new_word);
count++;
}
// 틀린 입력시
else {
cout << "틀린 입력입니다.\n";
}
cout << word << endl;
}
}
#include <ctime>
#include <iostream>
using namespace std;
int main() {
string current_word = "start";
int word_count = 0;
cout << "끝말잇기 시작: " << current_word << "\n";
while (true) {
// 입력 받기 전 설정
clock_t start_time = clock();
cout << "단어를 입력하세요: ";
string new_word;
cin >> new_word;
// 입력된 단어의 길이 확인
if (new_word.length() <= 1) {
cout << "두 글자 이상의 단어를 입력하세요.\n";
continue;
}
// 입력된 단어를 모두 소문자로 변환
for (int i = 0; i < new_word.length(); i++) {
new_word[i] = tolower(new_word[i]);
}
// 이미 사용된 단어인지 확인
if (new_word.front() != current_word.back()) {
cout << "틀린 입력입니다.\n";
continue;
}
// TimeAttack 10초
clock_t end_time = clock();
if ((end_time - start_time) / CLOCKS_PER_SEC >= 10) {
cout << "시간 초과!! 게임 종료. \n입력한 단어 개수: " << word_count
<< "\n";
break;
}
// 끝말잇기 규칙에 맞으면 단어 추가
current_word += (" --> " + new_word);
word_count++;
cout << current_word << endl;
}
return 0;
}
1. clock_t와 CLOCKS_PER_SEC
2. string
3. while 문
4. if-else 문
입력된 단어가 한글자 이하인 경우 또는 이미 사용된 단어인 경우 기능 추가
대소문자가 섞인 경우 기능 추가
변수명을 모호하지 않게 수정, 들여쓰기 수정
마무리
분명.. 작성할 땐 이건 진짜 완벽하다 생각할 수 있게 열심히 고쳤는데!!!
집 와서 벨로그 작성하기 전에 한 번만 더 확인해 보자 싶어서 보니까
수정하고 싶은 것도 추가할 수 있겠다고 생각한 것도 또 생겼다!
그래도 tolower와 toupper 함수에 대해 알았으니 만족한다.
tolower: C++의 문자열 함수 중 하나로, 문자열의 모든 알파벳을 소문자로 변환
toupper: C++의 문자열 함수 중 하나로, 문자열의 모든 알파벳을 대문자로 변환
tolower는 소문자로 바꿔주는 거도 toupper는 대문자로 변환해주는 거라고 생각하면 이해하기 편하다.(진짜 그런거지만)
계속해서 짧은 코드에서도 수정할 수 있는 것과 추가할 수 있는 것에 대한 생각을 하고 실행에 옮기면 새로운 것들을 배울 수 있어서 좋은 것 같다.
수정한 코드에서도 개선사항이 아직 많겠지만 여기까지만..
이상!