๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[inflearn ๊ฐ์] ๊ธฐ๋ณธ ๋ฌธ๋ฒ - ์ผ๊ตฌ ๊ฒ์ ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/C++
[inflearn ๊ฐ์] ๊ธฐ๋ณธ ๋ฌธ๋ฒ - ์ผ๊ตฌ ๊ฒ์
์ง์ง์ํ์นด 2023. 6. 30. 01:06728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ ์ด์ํธ๋ฝ ๊ฒ์์์นด๋ฐ๋ฏธ ๋์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> C++ Let's Make Games
๐ซง ์ผ๊ตฌ ๊ฒ์ 1
// Chapter1_10
#include <iostream>
using namespace std;
int main()
{
int Player1, Player2, Player3; // ํ๋ ์ด์ด 3๋ช
int Computer1, Computer2, Computer3; // ์ปดํจํฐ 3๋
int cnt = 1;
int strike = 0;
int ball = 0;
srand((unsigned int)time(0));
Computer1 = rand() % 9 + 1;
Computer2 = rand() % 9 + 1;
Computer3 = rand() % 9 + 1;
cout << "์ซ์ ์ผ๊ตฌ ๊ฒ์ ์์" << endl;
while (true) {
cout << "1 ~ 9 ์ซ์ ์
๋ ฅ : " << endl;
cin >> Player1;
cin >> Player2;
cin >> Player3;
if (Computer1 == Player1) {
strike++;
}
else if (Computer1 == Player2) {
ball++;
}
else if (Computer1 == Player3) {
ball++;
}
if (Computer2 == Player2) {
strike++;
}
else if (Computer2 == Player1) {
ball++;
}
else if (Computer2 == Player3) {
ball++;
}
if (Computer3 == Player3) {
strike++;
}
else if (Computer3 == Player1) {
ball++;
}
else if (Computer3 == Player2) {
ball++;
}
cout << "Strike : " << strike << endl;
cout << "Ball : " << ball << endl;
// strike 3๋ฒ์ด๋ฉด ์์
if (strike == 3) {
break;
}
cnt++;
}
return 0;
}
๐ซง ์ผ๊ตฌ ๊ฒ์ 2
// Chapter1_10
#include <iostream>
using namespace std;
int main()
{
srand((unsigned int)time(0));
int iNumber[9] = {};
// 1 ~ 9 ์ซ์ ์ค์
for (int i = 0; i < 9; i++) {
iNumber[i] = i + 1;
}
// ์ค์ ๋ ์ซ์ ์๊ธฐ
int iTemp, idx1, idx2;
for (int i = 0; i < 100; ++i) {
idx1 = rand() % 9;
idx2 = rand() % 9;
iTemp = iNumber[idx1];
iNumber[idx1] = iNumber[idx2];
iNumber[idx2] = iTemp;
}
cout << "*\t*\t*" << endl;
//cout << iNumber[0] << "\t" << iNumber[1] << "\t" << iNumber[2] << endl;
int iStrike = 0;
int iBall = 0;
int iInput[3] ;
int iGameCnt = 1;
while (true) {
cout << iGameCnt << " ํ" << endl;
cout << "1 ~ 9 ์ฌ์ด์ ์ซ์ ์ค 3๊ฐ ์
๋ ฅ (0์ ์ข
๋ฃ) : ";
cin >> iInput[0] >> iInput[1] >> iInput[2];
if (iInput[0] == 0 || iInput[1] == 0 || iInput[2] == 0) {
break;
}
else if (iInput[0] < 0 || iInput[0] > 9 || iInput[1] < 0 || iInput[1] > 9 || iInput[2] < 0 || iInput[2] > 9) {
cout << "์๋ชป๋ ์ซ์ ์
๋ ฅํจ" << endl;
continue;
}
else if (iInput[0] == iInput[1] || iInput[1] == iInput[2] || iInput[0] == iInput[2]) {
cout << "์ค๋ณต๋ ์ซ์ ์
๋ ฅํจ" << endl;
continue;
}
// ๋งค๋ฒ Strike์ Ball ์๊ฐ ๋ฌ๋ผ์ง๋ฏ๋ก 0์ผ๋ก ์ด๊ธฐํ
iStrike = iBall = 0;
// ๋ง์ถฐ์ผํ ์ซ์์ ์ธ๋ฑ์ค
for (int i = 0; i < 3; ++i) {
// ์
๋ ฅ๋ฐ์ ์ซ์์ ์ธ๋ฑ์ค
for (int j = 0; j < 3; ++j) {
// ๋ง์ถฐ์ผํ ์ซ์์ ์ฒซ๋ฒ์งธ, ์
๋ ฅ๋ฐ์ ์ซ์์ ์ฒซ, ๋, ์ธ๋ฒ์งธ ๋น๊ตํด์ ๊ฐ์ ๊ฐ ์ฐพ๊ธฐ
if (iNumber[i] == iInput[j]) {
// ๊ฐ์ด ๊ฐ์ ๊ฐ์ด ์์ ๊ฒฝ์ฐ Strike ์๋๋ฉด Ball ์ด๋ฏ๋ก ์๋ฆฌ ๊ฐ์์ง ์ฒดํฌ
// i์ j ๋ ๋ง์ถฐ์ผํ ์ซ์์ ์ธ๋ฑ์ค์ ์
๋ ฅ ๋ฐ์ ์ซ์์ ์ธ๋ฑ์ค
// ์ธ๋ฑ์ค๊ฐ ์๋ก ๊ฐ๋ค๋ฉด strike ์ฆ๊ฐ, ์๋๋ผ๋ฉด ball
if (i == j)
++iStrike;
else
++iBall;
// ์
๋ ฅ๋ฐ์ ์ซ์์ ๋ง์ถฐ์ผํ ์ซ์๋ฅผ ๋น๊ตํ ๋ ๊ฐ์ ์๊ฐ ์์ผ๋ฉด
// ๋์ด์ ๊ฐ์ ์๋ ์์ผ๋ฏ๋ก ๋ค์ ๋ง์ถฐ์ผํ ์ซ์๋ก ๋ฐ๋ก ๋น๊ตํจ
break;
}
}
}
if (iStrike == 3) {
cout << "์ซ์ ๋ชจ๋ ๋ง์ถค" << endl;
break;
}
else if (iStrike == 0 && iBall == 0) {
cout << "Out" << endl;
}
else {
cout << iStrike << " Strike " << iBall << " Ball " << endl;
}
++iGameCnt;
}
return 0;
}
728x90
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[inflearn ๊ฐ์] ๋น๊ณ ๊ฒ์ - ๋น๊ณ ๊ฒ์ 1 (0) | 2023.07.03 |
---|---|
[inflearn ๊ฐ์] ๊ธฐ๋ณธ ๋ฌธ๋ฒ - ์ซ์ํผ์ฆ๊ฒ์ (0) | 2023.06.30 |
C++ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํค๋ ํ์ผ (0) | 2023.06.29 |
[inflearn ๊ฐ์] ๊ธฐ๋ณธ ๋ฌธ๋ฒ - ๋ก๋ ๋ง๋ค๊ธฐ (0) | 2023.06.29 |
[inflearn ๊ฐ์] ๊ธฐ๋ณธ ๋ฌธ๋ฒ - ๋ฐฐ์ด (0) | 2023.06.29 |
Comments