별찍기 : 다중 for문을 이용하여 만들어보았다.
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
//별찍기 첫번째
for (int i = 1; i < 6; i++)
{
for (int j = 0; j < i; j++)
{
cout << "*" << " ";
}
cout << endl;
}
for (int i = 5; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
cout << "*" << " ";
}
cout << endl;
}
//별찍기 두번째
for (int i = 0; i < 5; i++)
{
for (int j = 4; j > i; j--)
{
cout << " ";
}
for (int q = 0; q < (i + 1); q++)
{
cout << "*" << " ";
}
for (int w = 0; w < i; w++)
{
cout << "*" << " ";
}
cout << endl;
}
int exit;
cin >> exit;
}
숫자야구게임 : 셔플을 사용하여 임의의 숫자를 넣었다.
#include<iostream>
#include<time.h>
using namespace std;
int main() {
srand(time(NULL));
int input[3];
int number[9];
int com[3];
int first = 1;
int ball = 0, strike = 0, count = 0;
for (int i = 0; i < 9; i++)
{
number[i] = i + 1;
}
cout << "숫자야구게임" << endl;
while (true)
{
//숫자 입력
cout << "자신의 숫자를 입력하세요 -> ";
for (int i = 0; i < 3; i++)
{
cin >> input[i];
}
if (first) //컴퓨터 숫자 지정 유무 확인
{
//컴퓨터 숫자 임의 지정(셔플 사용)
for (int i = 0; i < 10; i++)
{
int temp;
int num1 = rand() % 9;
int num2 = rand() % 9;
temp = number[num1];
number[num1] = number[num2];
number[num2] = temp;
}
for (int i = 0; i < 3; i++)
{
com[i] = number[i];
}
first = 0;
}
//검사중
for (int i = 0; i < 3; i++)
{
if (input[i] == com[i])
{
strike++;
}
for (int j = 2; j > i; j--)
{
if (input[i] == com[j])
{
ball++;
}
}
for (int j = 0; j < i; j++)
{
if (input[i] == com[j])
{
ball++;
}
}
}
//컴퓨터 숫자 출력
cout << "컴퓨터 숫자 -> ";
for (int i = 0; i < 3; i++)
{
cout << com[i];
}
cout << endl;
//볼,스트라이크 출력
count++;
cout << ball << "B" << strike << "S" << endl;
if (strike == 3)
{
cout << count << "번 만에 맞췄다." << endl;
count = 0;
}
ball = 0; //초기화
strike = 0; //초기화
}
}
숫자야구 ver.2
숫자 입력 시, 띄어쓰지 않아도 숫자 하나하나 분리하여 저장할 수 있게 됐다.
//검사중 부분의 코드를 훨씬 더 효율적으로 바꾸었다.
정답 맞힐 시, 프로그램 종료되도록 지시자 break를 사용했다.
#include<iostream>
#include<time.h>
using namespace std;
int main() {
srand(time(NULL));
int input;
int user[3];
int number[9];
int com[3];
int first = 1;
int ball, strike, count;
//이런 식으로 한번에 초기화도 가능하다.
ball = strike = count = 0;
for (int i = 0; i < 9; i++)
{
number[i] = i + 1;
}
cout << "숫자야구게임" << endl;
while (true)
{
//ball과 strike값의 중첩을 막기위해 초기화
ball = strike = 0;
//숫자 입력(더 이상 띄어쓰지않아도 하나씩 추출가능)
cout << "자신의 숫자를 입력하세요 -> ";
cin >> input;
user[0] = (int)(input / 100); //백의 자리
user[1] = (input % 100) / 10; //십의 자리
user[2] = input % 10; //일의 자리
if (first) //컴퓨터 숫자 지정 유무 확인
{
//컴퓨터 숫자 임의 지정(셔플 사용)
for (int i = 0; i < 10; i++)
{
int temp;
int num1 = rand() % 9;
int num2 = rand() % 9;
temp = number[num1];
number[num1] = number[num2];
number[num2] = temp;
}
for (int i = 0; i < 3; i++)
{
com[i] = number[i];
}
first = 0;
}
//검사중(훨씬 더 간단하게 줄일 수 있었다)
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (com[i] == user[j])
{
if (i == j)
strike++;
else
ball++;
}
}
}
//볼,스트라이크 출력
count++;
cout << ball << "B" << strike << "S" << endl;
if (strike == 3) //프로그램 종료 조건
{
cout << count << "번 만에 맞췄다." << endl;
break;
}
}
}
'코딩' 카테고리의 다른 글
Low High Seven 게임 (0) | 2022.10.19 |
---|---|
월남뽕 게임 (2) | 2022.10.18 |
로또번호 자동 생성기 (2) | 2022.10.15 |
가위바위보 게임 (2) | 2022.10.13 |
숫자 유추해서 맞춰보기 (0) | 2022.10.12 |