...
C언어 가위바위보 게임
입문게임이라 할 수 있는 가위바위보 게임을 간단하게마나 만들어 보았다.
컴퓨터에 승률 확률을 부여하고 컬러문자를 이용해 그래픽을 향상 시켰다.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#define ESC 0x1b
#define COL GetStdHandle(STD_OUTPUT_HANDLE)
#define BLACK SetConsoleTextAttribute(COL, 0x0000);
#define DARK_BLUE SetConsoleTextAttribute(COL, 0x0001);
#define GREEN SetConsoleTextAttribute(COL, 0x0002);
#define BLUE_GREEN SetConsoleTextAttribute(COL, 0x0003);
#define BLOOD SetConsoleTextAttribute(COL, 0x0004);
#define PURPLE SetConsoleTextAttribute(COL, 0x0005);
#define DARK_YELLOW SetConsoleTextAttribute(COL, 0x0006);
#define WHITE SetConsoleTextAttribute(COL, 0x0007);
#define GRAY SetConsoleTextAttribute(COL, 0x0008);
#define BLUE SetConsoleTextAttribute(COL, 0x0009);
#define LIGHT_GREEN SetConsoleTextAttribute(COL, 0x000a);
#define LIGHT_BLUE SetConsoleTextAttribute(COL, 0x000b);
#define RED SetConsoleTextAttribute(COL, 0x000c);
#define PLUM SetConsoleTextAttribute(COL, 0x000d);
#define YELLOW SetConsoleTextAttribute(COL, 0x000e);
#define LIGHT_WHITE SetConsoleTextAttribute(COL, 0x000f);
void gotoxy(int x, int y);
void game_rsp(int x, int y);
void picture_load(int x, int y);
int picture_patternimage(int x);
int player_num2;
typedef struct lastscore {
int win, loose, draw;
int time_num;
double avg;
}
LASTSCORE;
typedef struct gamesource {
char mod[3];
char mod_compare[3][3];
int mod_num[3];
char player;
int com;
int com_diff;
int random;
}
GAMESOURCE;
LASTSCORE lastscore1 = {
0,
0,
0,
0,
0
};
GAMESOURCE gamesource1;
int main() {
system("title 가위바위보 게임"); //제목 지정
system("color 09"); //배경색 글자색 설정
//gamesource.mod_compare[3][3] = {"하","중","상"};
int i, hit1;
strcpy(gamesource1.mod_compare[0], "하");
strcpy(gamesource1.mod_compare[1], "중");
strcpy(gamesource1.mod_compare[2], "상");
for (i = 0; i < 3; i++)
gamesource1.mod_num[i] = i;
//int player_num1, com_num1, com_num2, diff;//1=랜덤값대입 num1=문자열처리 num2=난이도 diff=확률
//double avg;
//char player, /*com2[5]*/ //mod[3];
//char gamep[3]={'1', '2','3'}, //hard[3][3]={"하","중","상"};
//int nump[3]={0,1,2};
srand(time(NULL));
//puts("┏┓┗┛┃┣┫━");
gotoxy(40, 3);
WHITE printf("컴퓨터 난이도 설정 [ ] ");
gotoxy(42, 4);
BLUE puts("┏━━━━━━━━━┓");
gotoxy(42, 5);
BLUE puts("┃ / / ┃");
gotoxy(45, 5);
LIGHT_GREEN printf("하");
gotoxy(52, 5);
YELLOW printf("중");
gotoxy(59, 5);
RED printf("상");
gotoxy(42, 6);
BLUE puts("┗━━━━━━━━━┛");
gotoxy(62, 3);
WHITE printf(""); //색깔 초기화 안하면 입력할때 색깔겹침
scanf("%s", gamesource1.mod);
system("cls");
for (i = 0; i < 3; i++) {
if (strcmp(gamesource1.mod, gamesource1.mod_compare[i]) == 0)
gamesource1.com_diff = gamesource1.mod_num[i];
}
do {
gotoxy(41, 3);
printf("당신이 낼 건 ? [ ] ");
gotoxy(42, 4);
BLUE puts("┏━━━━━━━━━┓");
gotoxy(42, 5);
BLUE puts("┃ / / ┃");
gotoxy(45, 5);
LIGHT_GREEN printf("가위");
gotoxy(52, 5);
YELLOW printf("바위");
gotoxy(59, 5);
RED printf("보");
gotoxy(42, 6);
BLUE puts("┃ / / ┃");
gotoxy(46, 6);
LIGHT_GREEN printf("1");
gotoxy(53, 6);
YELLOW printf("2");
gotoxy(60, 6);
RED printf("3");
gotoxy(42, 7);
BLUE puts("┗━━━━━━━━━┛");
gotoxy(35, 8);
LIGHT_BLUE puts("단축키(숫자)를 누르시면 됩니다. 1 ~ 3");
WHITE printf(""); //색깔 초기화
gotoxy(58, 3);
gamesource1.player = getche();
gamesource1.player -= 49;
system("cls");
gotoxy(1, 3);
//player_num2 = player_num1;
LIGHT_BLUE printf("당신 >\n");
if (gamesource1.com_diff == 0) {
gamesource1.com = rand() % 3 + 0;
int diff = rand() % 3 + 0;
gotoxy(101, 3);
YELLOW puts("< 컴퓨터");
if (diff == 0 || diff == 2) {
if (gamesource1.player == 0) {
game_rsp(gamesource1.com = 2, 1);
}
if (gamesource1.player == 1) {
game_rsp(gamesource1.com = 0, 1);
}
if (gamesource1.player == 2) {
game_rsp(gamesource1.com = 1, 1);
}
} else {
game_rsp(gamesource1.com, 1);
}
}
if (gamesource1.com_diff == 1) {
gamesource1.com = rand() % 3 + 0;
gotoxy(101, 3);
YELLOW puts("< 컴퓨터");
game_rsp(gamesource1.com, 1);
}
if (gamesource1.com_diff == 2) {
gamesource1.com = rand() % 3 + 0;
int diff = rand() % 3 + 0;
gotoxy(101, 3);
YELLOW puts("< 컴퓨터");
if (diff == 0 || diff == 2) {
if (gamesource1.player == 0) {
game_rsp(gamesource1.com = 1, 1);
}
if (gamesource1.player == 1) {
game_rsp(gamesource1.com = 2, 1);
}
if (gamesource1.player == 2) {
game_rsp(gamesource1.com = 0, 1);
}
} else {
game_rsp(gamesource1.com, 1);
}
}
putchar('\n');
Sleep(500);
LIGHT_GREEN
if (gamesource1.player == gamesource1.com) {
gotoxy(48, 11);
puts("* * * * *");
gotoxy(48, 12);
puts("* 비김! *");
gotoxy(48, 13);
puts("* * * * *");
lastscore1.draw++;
} else if (gamesource1.player == 0 && gamesource1.com == 2) {
gotoxy(48, 11);
puts("* * * * *");
gotoxy(48, 12);
puts("* 승리! *");
gotoxy(48, 13);
puts("* * * * *");
lastscore1.win++;
} else if (gamesource1.player == 1 && gamesource1.com == 0) {
gotoxy(48, 11);
puts("* * * * *");
gotoxy(48, 12);
puts("* 승리! *");
gotoxy(48, 13);
puts("* * * * *");
lastscore1.win++;
} else if (gamesource1.player == 2 && gamesource1.com == 1) {
gotoxy(48, 11);
puts("* * * * *");
gotoxy(48, 12);
puts("* 승리! *");
gotoxy(48, 13);
puts("* * * * *");
lastscore1.win++;
} else {
gotoxy(48, 11);
puts("* * * * *");
gotoxy(48, 12);
puts("* 패배! *");
gotoxy(48, 13);
puts("* * * * *");
lastscore1.loose++;
}
Sleep(300);
gotoxy(37, 0);
LIGHT_WHITE printf(">> 계속하려면 Tab를 누르세요 <<");
gotoxy(37, 1);
LIGHT_WHITE printf(" >> 끝내려면 Esc를 누르세요 <<");
lastscore1.time_num++;
hit1 = getch();
system("cls");
} while (hit1 != ESC);
LIGHT_BLUE puts("\n\n\n\t\t\t┏━━━━━━━━━━━━━━━━━━┓");
printf("\t\t\t┃ 난이도 : %s ┃\n", gamesource1.mod);
printf("\t\t\t┃ 가위바위보를 한 총 횟수 : %-3d ┃\n", lastscore1.time_num);
printf("\t\t\t┃ 이긴 횟수 : %-2d ┃\n", lastscore1.win);
printf("\t\t\t┃ 비긴 횟수 : %-2d ┃\n", lastscore1.draw);
printf("\t\t\t┃ 진 횟수 : %-2d ┃\n", lastscore1.loose);
lastscore1.avg = ((double) lastscore1.win / (lastscore1.win + lastscore1.draw + lastscore1.loose)) * 100.0;
puts("\t\t\t┃ ┃");
printf("\t\t\t┃ 승률 : %-3.lf%% ┃\n", lastscore1.avg);
puts("\t\t\t┗━━━━━━━━━━━━━━━━━━┛\n");
system("pause");
return 0;
}
void gotoxy(int x, int y) {
COORD Cur;
Cur.X = x;
Cur.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Cur);
}
void game_rsp(int x, int y) {
if (y == 1)
picture_patternimage(0);
switch (x) {
case 0:
//가위
picture_load(0, y);
break;
case 1:
//바위
picture_load(1, y);
break;
case 2:
//보
picture_load(2, y);
break;
default:
break;
}
}
void picture_load(int x, int y) {
char picture[3][20][35] = {
{
" ■■ ■■ ",
" ■ ■ ■ ■",
" ■ ■ ■ ■",
" ■ ■ ■ ■",
" ■ ■ ■ ■ ",
" ■ ■■ ■ ",
" ■■■ ■ ■ ",
" ■■■ ■ ■ ■ ",
"■ ■ ■ ■ ■ ",
"■ ■ ■ ■ ",
"■ ■ ■■■■■■■ ",
"■ ■ ■ ■ ",
"■■■ ■■■ ■ ",
"■ ■■■■ ■ ",
"■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ "
},
{
" ",
" ",
" ",
" ",
" ",
" ■■ ■■ ■■ ",
" ■■■ ■ ■ ■ ",
"■ ■ ■ ■ ■ ",
"■ ■ ■ ■ ■ ",
"■ ■ ■■■■■■■ ",
"■ ■ ■ ■ ",
"■■■ ■■■ ■ ",
"■ ■■■■ ■ ",
"■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ "
},
{
" ■■ ",
" ■■■ ■■■ ",
" ■ ■ ■ ■ ",
" ■ ■ ■ ■ ",
" ■■■ ■ ■ ■ ",
"■ ■ ■ ■ ■ ",
"■ ■ ■ ■ ■ ",
"■ ■ ■ ■ ■ ",
"■ ■ ■ ■ ■■ ",
"■ ■ ■ ■",
"■ ■ ■",
"■ ■ ■",
"■ ■ ■",
"■ ■ ■",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ ",
" ■ ■ "
}
};
int i;
if (y == 0) //플레이어
{
switch (x) {
case 0:
//가위
for (i = 0; i < 20; i++) {
gotoxy(4, i + 4);
LIGHT_BLUE printf("%35s\n", picture[0][i]);
}
break;
case 1:
//바위
for (i = 0; i < 20; i++) {
gotoxy(4, i + 4);
LIGHT_BLUE printf("%35s\n", picture[1][i]);
}
break;
case 2:
//보
for (i = 0; i < 20; i++) {
gotoxy(4, i + 4);
LIGHT_BLUE printf("%35s\n", picture[2][i]);
}
break;
default:
break;
}
} else //컴
{
switch (x) {
case 0:
//가위
for (i = 0; i < 20; i++) {
gotoxy(63, i + 4);
YELLOW printf("%35s\n", picture[0][i]);
}
break;
case 1:
//바위
for (i = 0; i < 20; i++) {
gotoxy(63, i + 4);
YELLOW printf("%35s\n", picture[1][i]);
}
break;
case 2:
//보
for (i = 0; i < 20; i++) {
gotoxy(63, i + 4);
YELLOW printf("%35s\n", picture[2][i]);
}
break;
default:
break;
}
}
}
int picture_patternimage(int x) {
int i, j;
if (x < 5) {
picture_load(1, 1);
picture_load(0, 0);
Sleep(150);
picture_load(0, 1);
picture_load(2, 0);
Sleep(150);
picture_load(2, 1);
picture_load(1, 0);
Sleep(150);
picture_load(0, 1);
picture_load(2, 0);
Sleep(150);
return picture_patternimage(++x);
} else {
for (i = 0; i < 20; i++) {
gotoxy(4, i + 4);
printf(" ");
gotoxy(63, i + 4);
printf(" ");
}
Sleep(400);
game_rsp(gamesource1.player, 0);
return 0;
}
}
인용한 부분에 있어 만일 누락된 출처가 있다면 반드시 알려주시면 감사하겠습니다
이 글이 좋으셨다면 구독 & 좋아요
여러분의 구독과 좋아요는
저자에게 큰 힘이 됩니다.