본문 바로가기

【그로잉】/…Programming

C게임 #3-2. 숫자 야구게임(下)

중복된 수를 없애는 버전입니다.

알고리즘

1. 0부터 9까지의 수를 입력받는다.

2. 답안 숫자가 포함되면 볼, 답안 숫자가 자릿수까지 일치할 경우 스트라이크 선언을 한다.

3. 만약 모든 숫자가 자릿수가 맞으면 클리어가 되며 12회 안에 클리어를 못할 경우 게임 오버가 된다.

4. 유저들의 입맛에 맞게 하드모드도 추가를 한다. (4자리 숫자, 17회)

#include <stdio.h>

#include <stdlib.h>

#include <time.h>


int main(){

//기본 변수 선언

int i, j, base[3], baser[3], baseh[4], baserh[4], numb[9];

int life = 0;

int str = 0, bal = 0;

int level;


//랜덤한 수 선언

srand(time(NULL));

do{for(i=0; i<3; i++){base[i] = rand()%9+1;}}

while(base[0] == base[1] || base[0] == base[2] || base[1] == base[2]);


do{for(i=0; i<4; i++){baseh[i] = rand()%9+1;}}

while(baseh[0] == baseh[1] || baseh[0] == baseh[2] || baseh[0] == baseh[3] || baseh[1] == baseh[2] || baseh[1] == baseh[3] || baseh[2] == baseh[3]);


//입력할 수 있는 수 선언

for(i=1; i<10; i++){numb[i]=i;}


//게임 대사

printf("====================\n  라스트 러쉬\n====================\n");

printf("===================================================================\n아레스의 꾐어 넘어가 저주를 받아 신의 죄인이 되어버린 크레토스.\n");

printf("드디어 그에 복수할 기회가 생기고 판도라의 상자를 열 수 있게 되었다.\n");

printf("그러나 봉인을 풀기 직전, 그를 방해하기 위해 아레스가 나타나는데...\n===================================================================\n");

printf("\n난이도를 입력해주세요.\n1 : 노멀 모드 : 3개의 숫자로 12번의 기회\n2 : 하드 모드 : 4개의 숫자로 17번의 기회\n");

scanf("%d", &level);

if(level==1){


//============================================================ 노 멀 모 드 ============================================================

printf("\n노멀 모드\n");

for(life=12; life>-1; life--){


//누적된 스트라이크와 볼 함수 초기화

str =0; bal = 0;

for(i=0; i<3; i++){baser[i] = 0;}


//입력할 수 있는 수

printf("\n====================\n진행 횟수 : %d\n남은 횟수 : %d\n할 수 있는 숫자 : ", 12-life, life);

for(i=1; i<10; i++){

if(!numb[i]==0){

printf("%d ", i);

}

}printf("\n====================\n");


//선택

do{

printf("봉인을 해제할 3가지의 수를 입력하시오 : ");

scanf("%d %d %d", &baser[0], &baser[1], &baser[2]);

}


//수가 중복되는지 확인

while(baser[0]==baser[1] || baser[0]==baser[2] || baser[1]==baser[2]);


//수가 일치하는지 확인

if(base[0]==baser[0]){str++;}

if(base[1]==baser[1]){str++;}

if(base[2]==baser[2]){str++;}


if(base[0]==baser[1] || base[0]==baser[2]){bal++;}

if(base[1]==baser[0] || base[1]==baser[2]){bal++;}

if(base[2]==baser[0] || base[2]==baser[1]){bal++;}


//모두 일치할 경우

if(str==3){

printf("\n================================================================================드디어 봉인이 해방되었다.");

printf("크레토스 : 마지막 일격!\n아레스 : 크허어어억!\n");

printf("크레토스 : 드디어 모든 게 끝이 났소. 이제 난 자유의 몸이 될 수 있는 것이오?\n");

printf("신 : 아니, 이제 당신은 아레스의 공백에 서 신의 자리에 서주어야 하오.\n");

printf("크레토스 : 그... 그런! 신의 자리라고? 난 인정할 수 없다!\n\n");

printf("그렇게 끝날줄만 알았던 크레토스의 여정, 그것은.. 이제 시작에 불과한 것이었다.\n================================================================================\n");

break;

}


//모두 불일치할 경우

else if(str == 0 && bal == 0){

printf("\n어느 숫자도 일치하지 않습니다.\n");

for(j=1; j<10; j++){

if(baser[0] == j){numb[j] = 0;}

if(baser[1] == j){numb[j] = 0;}

if(baser[2] == j){numb[j] = 0;}

}

}


//일부 일치할 경우

else{printf("\n완전한 일치 : %d\n불완전 일치 : %d\n", str, bal);}

}

}

else if(level==2){


//============================================================ 하 드 모 드 ============================================================

printf("\n하드 모드\n");

for(life=17; life>-1; life--){


//누적된 스트라이크와 볼 함수 초기화

str =0; bal = 0;

for(i=0; i<4; i++){baserh[i] = 0;}


//입력할 수 있는 수

printf("\n====================\n진행 횟수 : %d\n남은 횟수 : %d\n할 수 있는 숫자 : ", 17-life, life);

for(i=1; i<10; i++){

if(!numb[i]==0){

printf("%d ", i);

}

} printf("\n====================\n");


//선택

do{

printf("봉인을 해제할 4가지의 수를 입력하시오 : ");

scanf("%d %d %d %d", &baserh[0], &baserh[1], &baserh[2], &baserh[3]);

}


//수가 중복되는지 확인

while(baserh[0]==baserh[1] || baserh[0]==baserh[2] || baserh[0]==baserh[3] || baserh[1]==baserh[2] || baserh[1]==baserh[3] || baserh[2]==baserh[3]);


//수가 일치하는지 확인

if(baseh[0]==baserh[0]){str++;}

if(baseh[1]==baserh[1]){str++;}

if(baseh[2]==baserh[2]){str++;}

if(baseh[2]==baserh[3]){str++;}


if(baseh[0]==baserh[1] || baseh[0]==baserh[2] || baser[0]==baser[3]){bal++;}

if(baseh[1]==baserh[0] || baseh[1]==baserh[2] || baser[1]==baser[3]){bal++;}

if(baseh[2]==baserh[0] || baseh[2]==baserh[1] || baser[2]==baser[3]){bal++;}


//모두 일치할 경우

if(str==4){

printf("\n================================================================================드디어 봉인이 해방되었다.");

printf("크레토스 : 마지막 일격!\n아레스 : 크허어어억!\n");

printf("크레토스 : 드디어 모든 게 끝이 났소. 이제 난 자유의 몸이 될 수 있는 것이오?\n");

printf("신 : 아니, 이제 당신은 아레스의 공백에 서 신의 자리에 서주어야 하오.\n");

printf("크레토스 : 그... 그런! 신의 자리라고? 난 인정할 수 없다!\n\n");

printf("그렇게 끝날줄만 알았던 크레토스의 여정, 그것은.. 이제 시작에 불과한 것이었다.\n================================================================================\n");

break;

}


//모두 불일치할 경우

else if(str == 0 && bal == 0){

printf("\n어느 숫자도 일치하지 않습니다.\n");

for(j=1; j<10; j++){

if(baserh[0] == j){numb[j] = 0;}

if(baserh[1] == j){numb[j] = 0;}

if(baserh[2] == j){numb[j] = 0;}

if(baserh[3] == j){numb[j] = 0;}

}

}


//일부 일치할 경우

else{printf("\n완전한 일치 : %d\n불완전 일치 : %d\n", str, bal);}

}

}

else{printf("게임 실행 오류입니다. 다시 실행해주세요.\n");}


//그냥 게임오버

printf("\n====================\n G A M E   O V E R \n====================\n");

return 0;

}