-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordGame.cpp
60 lines (49 loc) · 1.15 KB
/
WordGame.cpp
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
#include <iostream>
#include <string>
using namespace std;
#include "WordGame.h"
#include "Player.h"
WordGame::WordGame(string title, string startWord) {
this->title = title;
this->startWord = startWord;
this->players = NULL; //코드 오류를 줄이기 위해 일반적으로 초기화한다.
}
WordGame::~WordGame() {
}
bool WordGame::createPlayers() {
cout << "끝말 잇기 게임을 시작합니다" << endl;
cout << "게임에 참가하는 인원은 몇명입니까? >> ";
int n;
cin >> n;
nPlayers = n;
players = new Player[n];
if (!players) {
return false;
}
string playerName;
for (int i = 0; i < n; i++) {
cout << "참가자의 이름을 입력하세요. 빈칸 없이 >> ";
cin >> playerName;
players[i].setName(playerName);
}
return true;
}
void WordGame::run() {
if (!createPlayers()) {
return;
}
string lastWord = startWord;
cout << "시작 단어는 " + lastWord + "입니다" << endl;
int next = 0;
while (true) {
string NewWord = players[next].sayWord();
if (!players[next].succeed(lastWord)) {
//단어가 연결이 되지 않으면 while을 벗어난다.
cout << players[next].getName() + "이(가) 졌습니다." << endl;
break;
}
next++;
next = next % nPlayers;
lastWord = NewWord;
}
}