// *******Up&Down Game*********
// 기존의 업앤다운 게임에 ASCII코드(33~126)를 활용하여 진행하는 게임
//
// 1. 33~126 사이에 랜덤 숫자 받기
// 2. 키보드로 입력가능한 특수문자를 포함한 문자 한개 입력
// 3. 정답과 비교하여 틀릴시 재입력 요구 및 라이프 차감
// 4. 랜덤문자와 같은문자 입력하면 클리어
// 5. 정답 맞추기 전에 라이프 전부 차감되면 게임오버
//
//
//
//
//
//
// ----함수목록----
// 1. int main(); =>메인함수
// 2. int ShowTitle(); =>타이틀 화면을 보여주고 선택지에 대한 정수를 반환
// 3. void ShowGame(); =>게임의 메인화면의 진행사항 화면을 출력
//
#include <stdio.h>
#include <Windows.h>
#include <conio.h> //_getch()함수를 사용하기 위해 쓰는 헤더
#include <time.h>
#include <stdlib.h>
int ShowTitle();
void ShowGame(int _maxLife, int _currentLife, int _gapLife, int _playerInput, int _answer);
const int CURSOR_MAX = 3;
const int ASCII_MAX = 126;
const int ASCII_MIN = 33;
int main()
{
srand((unsigned)time(NULL));
int gameMode = CURSOR_MAX;
bool isLoop = true;
int playerInput = 0;
int answer = rand() % (ASCII_MAX - ASCII_MIN + 1) + ASCII_MIN;
int maxLife = 10;
int currentLife = maxLife;
//게임시작화면
while (true)
{
isLoop = true;
while (isLoop)
{
gameMode = ShowTitle();
switch (gameMode)
{
case 0:
isLoop = false;
break;
case 1:
system("cls");
printf("== How to play ==");
printf("\n=> Input a correct character including Special symbols from keyboard before losing all of your life");
printf("\n=> Using characters of ASCII code no.33 ~ no.126\n\n");
Sleep(2000);
system("pause");
break;
case 2:
system("cls");
printf("\n Thanks for playing~");
Sleep(1500);
return 0;
}
}
//다음 반복문 사용을 위한 변수값 변경
isLoop = true;
//게임시작
while (isLoop)
{
char retry = 'a';
int gapLife = maxLife - currentLife;
//메인 게임진행 화면 출력
ShowGame(maxLife, currentLife, gapLife, playerInput, answer);
//정답 입력
printf("Just type your answer(No Enter) ");
playerInput = (int)_getch();
//정답 비교
if (answer == playerInput)
{
system("cls");
system("color 0a");
printf("Congratulation! you get the answer!\n");
printf("Answer is %c\n",(char)answer);
printf("Be ready to the next level!\n");
Sleep(2000);
system("color 07");
currentLife = --maxLife;
answer = rand() % (ASCII_MAX - ASCII_MIN + 1) + ASCII_MIN;
}
else
{
currentLife--;
//게임오버 체크
if (currentLife < 1)
{
//게임 오버
system("cls");
system("color 04");
printf("Game Over..\n");
printf("Do you wanna retry? (y/n)\n");
printf("-> ");
scanf_s("%c", &retry, 1);
system("color 07");
}
}
if (retry == 'y' || retry == 'Y')
{
maxLife = 10;
currentLife = maxLife;
}
else if (retry == 'n' || retry == 'N')
{
isLoop = false;
}
}
}
return 0;
}
//0 -> 게임시작 1 -> 게임설명 2 -> 게임종료
int ShowTitle()
{
int cursor = 0; // 타이틀 화면의 커서 좌표 변수
int key = 0; // _getch()함수를 통해서 ASCII코드 받아오는 변수
bool isLoop = true; // while문 제어용 변수
while (isLoop)
{
system("cls");
system("color 07");
printf(" ┌───────────────────────┐\n");
printf(" │ Up & Down │\n");
printf(" │ ver.ASCII │\n");
printf(" │ for Programmer │\n");
printf(" └───────────────────────┘\n");
printf(" use:↑, ↓, Enter\n\n");
switch (cursor)
{
case 0:
printf(" ▶ 1. Game Start\n");
printf(" 2. How to play\n");
printf(" 3. Exit Game\n");
break;
case 1:
printf(" 1. Game Start\n");
printf(" ▶ 2. How to play\n");
printf(" 3. Exit Game\n");
break;
case 2:
printf(" 1. Game Start\n");
printf(" 2. How to play\n");
printf(" ▶ 3. Exit Game\n");
break;
}
key = (int)_getch();
switch (key)
{
case 72:
cursor = (CURSOR_MAX + cursor - 1) % CURSOR_MAX;
break;
case 80:
cursor = (CURSOR_MAX + cursor + 1) % CURSOR_MAX;
break;
case 13:
isLoop = false;
break;
}
}
return cursor;
}
void ShowGame(int _maxLife, int _currentLife, int _gapLife, int _playerInput, int _answer)
{
system("cls");
printf("Level %d ======\n", (11 - _maxLife));
printf("Life : ");
for (int i = 0; i < _currentLife; i++)
{
printf("♥");
}
for (int i = 0; i < _gapLife; i++)
{
printf("♡");
}
printf("\n");
//힌트
if (_gapLife > 0)
{
if (_answer < _playerInput)
printf("%c is higher than answer\n", (char)_playerInput);
else if (_answer > _playerInput)
printf("%c is lower than answer\n", (char)_playerInput);
}
}