활용

예 ) 점프 중력 처리

 

뛰려는 힘 P

시간을 받아올 T

중력 G

 

JUMP = P * T - 0.5 * G * T * T

 

 

 

 

'SGA 스터디 > C++ 코딩' 카테고리의 다른 글

strcpy 문자열 복사 , strcmp 문자열 비교 함수 작성  (0) 2015.10.12
공 허들넘기 ㅋㅋ  (0) 2015.08.20
카드 뒤집기  (0) 2015.08.14
월남뽕  (0) 2015.08.13
별찍기  (0) 2015.08.11

#include<iostream>

#include<windows.h> 

#include<conio.h> 

#include<time.h> 


using namespace std;


void gotoxy(int x, int y);

void map(); // 맵

void bPoint(); // 볼 위치

void jump(); // 볼 점프


int x = 2, y = 9, loopX = 0; // 볼 위치 x, y 변수

char inputKey = 0; // 입력키 확인

int hd[20], hdCount=0; // 허들, 허들 라운드 수

int hdCheck = 0; // 허들 충돌 체크 변수


int main() {

srand(time(NULL));


int rNum; // 임의의 수를 받을 변수 

// 허들 초기화

for (int i = 0; i < 20; i++){

rNum = rand() % 25 + 10;

hd[i] = rNum;

}


do

{

while (!kbhit())

{

system("cls");

map();

bPoint();

Sleep(200);

if (hdCheck == 1) break;

}

jump();

} while (hdCheck == 0);

gotoxy(2, 11);

cout << " 장애물에 충돌하여 게임을 종료합니다." << endl;



return 0;

}

void gotoxy(int x, int y)

{


COORD Pos = { x - 1, y - 1 };


SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);


}

void map() {

printf("공튕기기");

gotoxy(hd[hdCount], 9);

printf("▲");

gotoxy(2, 10);

printf("───────────────────\n");

}

void bPoint() {

gotoxy(x, y);

cout << "●";

if (x < 40 && loopX == 0) {

x += 2;

if (x == 38) {

loopX = 1;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

if (hd[hdCount] == x && y == 9) hdCheck = 1;

if (loopX == 0)

{

if ((hd[hdCount] + 1) == x && y == 9) {

hdCheck = 1;

}

}

else {

if ((hd[hdCount] - 1) == x && y == 9) {

hdCheck = 1;

}

}

}

else {

x -= 2;

if (x == 2) {

loopX = 0;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

if (hd[hdCount] == x && y == 9) hdCheck = 1;

if (loopX == 0)

{

if ((hd[hdCount] + 1) == x && y == 9) {

hdCheck = 1;

}

}

else {

if ((hd[hdCount] - 1) == x && y == 9) {

hdCheck = 1;

}

}

}

}

void jump() {

inputKey = getch();

if (inputKey == 72) {

if (loopX == 0) {

gotoxy(x - 2, y);

cout << "  ";

}

else {

gotoxy(x + 2, y);

cout << "  ";

}


if (y == 9) {

y--;

if (x < 40 && loopX == 0) {

x += 2;

if (x == 38) {

loopX = 1;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

}

else {

x -= 2;

if (x == 2) {

loopX = 0;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

}

gotoxy(x, y);

cout << "●";

Sleep(200);


gotoxy(x, y);

cout << "  ";

if (x < 40 && loopX == 0) {

x += 2;

if (x == 38) {

loopX = 1;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

}

else {

x -= 2;

if (x == 2) {

loopX = 0;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

}

gotoxy(x, y);

cout << "●";

Sleep(200);


gotoxy(x, y);

cout << "  ";

y++;

if (x < 40 && loopX == 0) {

x += 2;

if (x == 38) {

loopX = 1;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

if (hd[hdCount] == x && y == 9) hdCheck = 1;

if (loopX == 0)

{

if ((hd[hdCount] + 1) == x && y == 9) {

hdCheck = 1;

}

}

else {

if ((hd[hdCount] - 1) == x && y == 9) {

hdCheck = 1;

}

}

}

else {

x -= 2;

if (x == 2) {

loopX = 0;

hdCount++;

}

if (hdCount == 20)

hdCount = 0;

if (hd[hdCount] == x && y == 9) hdCheck = 1;

if (loopX == 0) 

{

if ((hd[hdCount] + 1) == x && y == 9) {

hdCheck = 1;

}

}

else {

if ((hd[hdCount] - 1) == x && y == 9) {

hdCheck = 1;

}

}

}

gotoxy(x, y);

cout << "●";

}

}

}


ㅡㅡ 구동화면 ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

그냥 공이 좌우로 정해진 공간 만큼 오가면서 랜덤으로 생성되는 장애물을 


화살표(↑) 키를 눌러 점프해 피하는 게임


보다시피 장애물에 닿으면 죽음 끝






'SGA 스터디 > C++ 코딩' 카테고리의 다른 글

c 자유 낙하 공식  (0) 2015.11.12
strcpy 문자열 복사 , strcmp 문자열 비교 함수 작성  (0) 2015.10.12
카드 뒤집기  (0) 2015.08.14
월남뽕  (0) 2015.08.13
별찍기  (0) 2015.08.11

#include<iostream>

#include<time.h>

#include<conio.h>

#include<string>

#include<Windows.h>

using namespace std;


void cardShuffle(); // 카드 셔플

void Display1();      // 뷰 페이지 1 ( 앞면 혹은 맞춘 카드 확인 화면 )

void Display2();      // 뷰 페이지 2 ( 뒤집은 화면 )

void selectPlay1(); // 첫번째 오픈카드 입력 처리 및 카드 진위여부 확인

void selectPlay2(); // 두번째 오픈카드 입력 처리 및 카드 진위여부 확인



string front[4][5] = { { "  ","  ","  ","  ","  " },

     { "  ","  ","  ","  ","  " },

            { "  ","  ","  ","  ","  " },

      { "  ","  ","  ","  ","  " } };

string back[4][5] = { { "★","●","◆","■","▲" },

 { "♠","♥","♣","☎","♨" },

   { "★","●","◆","■","▲" },

   { "♠","♥","♣","☎","♨" } };

int result[4][5] = { { 0, 1, 2, 3, 4 },

  { 5, 6, 7, 8, 9 },  

  { 0, 1, 2, 3, 4 }, 

   { 5, 6, 7, 8, 9 } };

int openCard[4][5] = { {0,}, }; // 오픈될 카드 0 이면 뒷면, 1 이면 앞면이 나오게될 배열

int resultX1 = 5, resultY1 = 6, resultX2 = 5, resultY2 = 6; // 첫번째 카드위치 변수 resultX1, resultY1 / 두번째 카드위치 변수 resultX2, resultY2

int playCount = 0; // 카드를 뒤집은 횟수

int openCount = 0; // 맞춘 카드수


void main()

{

// 도전 과제

// 카드 뒤집기

cardShuffle();

while(1)

{

Display1();

selectPlay1();

Display1();

selectPlay2();

Display2();

if (openCount == 20) break;

}

Display1();

cout << "            " << playCount << " 번 뒤집어 성공하셨습니다" << endl;


}

void cardShuffle() {


srand(time(NULL));


int row1, row2, col1, col2; // 카드 섞을 변수


for (int i = 0; i < 1000; i++ ){

row1 = rand() % 4;

row2 = rand() % 4;

col1 = rand() % 5;

col2 = rand() % 5;


swap(back[row1][col1], back[row2][col2]);

swap(result[row1][col1], result[row2][col2]);

}


}

void Display1() {

system("cls");

cout << endl;

cout << "                카드 뒤집기 게임" << endl;

cout << " 뒤집은 횟수 : " << playCount << endl;

cout << " ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;

//ㅡㅡ첫줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 0 && resultY1 == 0) || (resultX2 == 0 && resultY2 == 0) || openCard[0][0] == 1) { cout << back[0][0]; }

else { cout << front[0][0]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 1) || (resultX2 == 0 && resultY2 == 1) || openCard[0][1] == 1) { cout << back[0][1]; }

else { cout << front[0][1]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 2) || (resultX2 == 0 && resultY2 == 2) || openCard[0][2] == 1) { cout << back[0][2]; }

else { cout << front[0][2]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 3) || (resultX2 == 0 && resultY2 == 3) || openCard[0][3] == 1) { cout << back[0][3]; }

else { cout << front[0][3]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 4) || (resultX2 == 0 && resultY2 == 4) || openCard[0][4] == 1) { cout << back[0][4]; }

else { cout << front[0][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

//ㅡㅡ둘째줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 1 && resultY1 == 0) || (resultX2 == 1 && resultY2 == 0) || openCard[1][0] == 1) { cout << back[1][0]; }

else { cout << front[1][0]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 1) || (resultX2 == 1 && resultY2 == 1) || openCard[1][1] == 1) { cout << back[1][1]; }

else { cout << front[1][1]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 2) || (resultX2 == 1 && resultY2 == 2) || openCard[1][2] == 1) { cout << back[1][2]; }

else { cout << front[1][2]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 3) || (resultX2 == 1 && resultY2 == 3) || openCard[1][3] == 1) { cout << back[1][3]; }

else { cout << front[1][3]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 4) || (resultX2 == 1 && resultY2 == 4) || openCard[1][4] == 1) { cout << back[1][4]; }

else { cout << front[1][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

//ㅡㅡ셋째줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 2 && resultY1 == 0) || (resultX2 == 2 && resultY2 == 0) || openCard[2][0] == 1) { cout << back[2][0]; }

else { cout << front[2][0]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 1) || (resultX2 == 2 && resultY2 == 1) || openCard[2][1] == 1) { cout << back[2][1]; }

else { cout << front[2][1]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 2) || (resultX2 == 2 && resultY2 == 2) || openCard[2][2] == 1) { cout << back[2][2]; }

else { cout << front[2][2]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 3) || (resultX2 == 2 && resultY2 == 3) || openCard[2][3] == 1) { cout << back[2][3]; }

else { cout << front[2][3]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 4) || (resultX2 == 2 && resultY2 == 4) || openCard[2][4] == 1) { cout << back[2][4]; }

else { cout << front[2][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

//ㅡㅡ넷째줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 3 && resultY1 == 0) || (resultX2 == 3 && resultY2 == 0) || openCard[3][0] == 1) { cout << back[3][0]; }

else { cout << front[3][0]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 1) || (resultX2 == 3 && resultY2 == 1) || openCard[3][1] == 1) { cout << back[3][1]; }

else { cout << front[3][1]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 2) || (resultX2 == 3 && resultY2 == 2) || openCard[3][2] == 1) { cout << back[3][2]; }

else { cout << front[3][2]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 3) || (resultX2 == 3 && resultY2 == 3) || openCard[3][3] == 1) { cout << back[3][3]; }

else { cout << front[3][3]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 4) || (resultX2 == 3 && resultY2 == 4) || openCard[3][4] == 1) { cout << back[3][4]; }

else { cout << front[3][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

cout << " ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;


}


void selectPlay1() {


int x, y; // 0행 0열이 아닌 1행 1열로 시작하게 하기 위한 변수

cout << " 1번째 뒤집을 카드를 선택하세요 ( ex) 1 1 ~ 4 5 ) : ";

cin >> x >> y;

resultX1 = x - 1;

resultY1 = y - 1;

// 4행 5열 안에 값인지 확인해서 아니면 다시 입력

while (resultX1 > 3 || resultX1 < 0 || resultY1 > 4 || resultY1 < 0 || openCard[resultX1][resultY1] == 1) {

cout << " 잘못입력하셨거나 이미 오픈된 카드입니다." << endl;

cout << " 1번째 뒤집을 카드를 재선택하세요 ( ex) 1 1 ~ 4 5 ) : ";

cin >> x >> y;

resultX1 = x - 1;

resultY1 = y - 1;

}

}

void selectPlay2() {


playCount++; // 입력할때마다 증가

int x, y; // 0행 0열이 아닌 1행 1열로 시작하게 하기 위한 변수

cout << " 2번째 뒤집을 카드를 선택하세요 ( ex) 1 1 ~ 4 5 ) : ";

cin >> x >> y;

resultX2 = x - 1;

resultY2 = y - 1;

// 4행 5열 안에 값인지 확인해서 아니면 다시 입력

while (resultX2 > 3 || resultX2 < 0 || resultY2 > 4 || resultY2 < 0 || openCard[resultX2][resultY2] == 1 || (resultX1 == resultX2 && resultY1 == resultY2)) {

cout << " 잘못입력하셨거나 이미 오픈된 카드입니다." << endl;

cout << " 2번째 뒤집을 카드를 재선택하세요 ( ex) 1 1 ~ 4 5 ) : ";

cin >> x >> y;

resultX2 = x - 1;

resultY2 = y - 1;

}


// 카드를 맞추었다면 

if (result[resultX1][resultY1] == result[resultX2][resultY2]) {

openCard[resultX1][resultY1] = 1;

openCard[resultX2][resultY2] = 1;

openCount += 2;

}

}

void Display2() {


system("cls");

cout << endl;

cout << "                카드 뒤집기 게임" << endl;

cout << " 뒤집은 횟수 : " << playCount << endl;

cout << " ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;

//ㅡㅡ첫줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 0 && resultY1 == 0) || (resultX2 == 0 && resultY2 == 0) || openCard[0][0] == 1) { cout << back[0][0]; }

else { cout << front[0][0]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 1) || (resultX2 == 0 && resultY2 == 1) || openCard[0][1] == 1) { cout << back[0][1]; }

else { cout << front[0][1]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 2) || (resultX2 == 0 && resultY2 == 2) || openCard[0][2] == 1) { cout << back[0][2]; }

else { cout << front[0][2]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 3) || (resultX2 == 0 && resultY2 == 3) || openCard[0][3] == 1) { cout << back[0][3]; }

else { cout << front[0][3]; }

cout << " │ │ ";

if ((resultX1 == 0 && resultY1 == 4) || (resultX2 == 0 && resultY2 == 4) || openCard[0][4] == 1) { cout << back[0][4]; }

else { cout << front[0][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

//ㅡㅡ둘째줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 1 && resultY1 == 0) || (resultX2 == 1 && resultY2 == 0) || openCard[1][0] == 1) { cout << back[1][0]; }

else { cout << front[1][0]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 1) || (resultX2 == 1 && resultY2 == 1) || openCard[1][1] == 1) { cout << back[1][1]; }

else { cout << front[1][1]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 2) || (resultX2 == 1 && resultY2 == 2) || openCard[1][2] == 1) { cout << back[1][2]; }

else { cout << front[1][2]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 3) || (resultX2 == 1 && resultY2 == 3) || openCard[1][3] == 1) { cout << back[1][3]; }

else { cout << front[1][3]; }

cout << " │ │ ";

if ((resultX1 == 1 && resultY1 == 4) || (resultX2 == 1 && resultY2 == 4) || openCard[1][4] == 1) { cout << back[1][4]; }

else { cout << front[1][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

//ㅡㅡ셋째줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 2 && resultY1 == 0) || (resultX2 == 2 && resultY2 == 0) || openCard[2][0] == 1) { cout << back[2][0]; }

else { cout << front[2][0]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 1) || (resultX2 == 2 && resultY2 == 1) || openCard[2][1] == 1) { cout << back[2][1]; }

else { cout << front[2][1]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 2) || (resultX2 == 2 && resultY2 == 2) || openCard[2][2] == 1) { cout << back[2][2]; }

else { cout << front[2][2]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 3) || (resultX2 == 2 && resultY2 == 3) || openCard[2][3] == 1) { cout << back[2][3]; }

else { cout << front[2][3]; }

cout << " │ │ ";

if ((resultX1 == 2 && resultY1 == 4) || (resultX2 == 2 && resultY2 == 4) || openCard[2][4] == 1) { cout << back[2][4]; }

else { cout << front[2][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

//ㅡㅡ넷째줄ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

cout << "  ┌──┐ ┌──┐ ┌──┐ ┌──┐ ┌──┐" << endl;

cout << "  │ ";

if ((resultX1 == 3 && resultY1 == 0) || (resultX2 == 3 && resultY2 == 0) || openCard[3][0] == 1) { cout << back[3][0]; }

else { cout << front[3][0]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 1) || (resultX2 == 3 && resultY2 == 1) || openCard[3][1] == 1) { cout << back[3][1]; }

else { cout << front[3][1]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 2) || (resultX2 == 3 && resultY2 == 2) || openCard[3][2] == 1) { cout << back[3][2]; }

else { cout << front[3][2]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 3) || (resultX2 == 3 && resultY2 == 3) || openCard[3][3] == 1) { cout << back[3][3]; }

else { cout << front[3][3]; }

cout << " │ │ ";

if ((resultX1 == 3 && resultY1 == 4) || (resultX2 == 3 && resultY2 == 4) || openCard[3][4] == 1) { cout << back[3][4]; }

else { cout << front[3][4]; }

cout << " │" << endl;

cout << "  └──┘ └──┘ └──┘ └──┘ └──┘" << endl;

cout << " ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ" << endl;

Sleep(1000);

// 초기화

resultX1 = 5;

resultY1 = 6;

resultX2 = 5;

resultY2 = 6;

}

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

카드 뒤집기 게임으로 1행 1열부터 4행 5열까지 가진 4 X 5 행렬로 총 20장의 카드로 2장씩 각각 동일한

문양을 가지고 있고 숫자를 입력 ( ex) 1행 1열의 카드를 뒤집고 싶으면 1 1 를 입력 ) 해서 

카드를 확인해 볼 수 있고 또한 이미 오픈된 카드는 입력할 수 없고 해당 행렬을 벗어난 값을 입력할 수도 없다.

종료화면처럼 카드를 다 맞추면 몇번 뒤집어 성공한지 출력되며 게임이 종료된다.

시작화면 그림

중간화면 그림

종료화면 그림


↓ 소스파일 

카드뒤집기.cpp


'SGA 스터디 > C++ 코딩' 카테고리의 다른 글

strcpy 문자열 복사 , strcmp 문자열 비교 함수 작성  (0) 2015.10.12
공 허들넘기 ㅋㅋ  (0) 2015.08.20
월남뽕  (0) 2015.08.13
별찍기  (0) 2015.08.11
하이 로우 세븐 카드게임 ( High & Low 7 )  (0) 2015.08.11

#include<iostream>

#include<time.h>

#include<conio.h>

#include<string>

#include<Windows.h>

using namespace std;


void startView(); // 시작화면

void cardSelect(); // 카드선택

void gamePlay(); // 게임화면

void endView(); // 종료화면

string card[52] = { "◆A", "◆2", "◆3", "◆4", "◆5", "◆6", "◆7", "◆8", "◆9", "◆10", "◆J", "◆Q", "◆K",

  "♣A", "♣2", "♣3", "♣4", "♣5", "♣6", "♣7", "♣8", "♣9", "♣10", "♣J", "♣Q", "♣K",

  "♠A", "♠2", "♠3", "♠4", "♠5", "♠6", "♠7", "♠8", "♠9", "♠10", "♠J", "♠Q", "♠K",

  "♥A", "♥2", "♥3", "♥4", "♥5", "♥6", "♥7", "♥8", "♥9", "♥10", "♥J", "♥Q", "♥K" };

int money = 1000;  // 보유 금액

int slot1 = 0, slot2 = 0, slot3 = 0; // 슬롯1, 2, 3

int cSlot1 = 0, cSlot2 = 0, cSlot3 = 0; // 슬롯에 위치한 카드 확인

int inputKey = 0;  // 게임진행 키 

int bMoney = 0;  // 배팅 금액


void main()

{

// 과제 월남뽕

// 카드 3장을 출력하되 마지막 3번째 카드는 오픈하지않고

// 3번째 카드의 숫자가 오픈된 1, 2번째 카드 사이의 값인지 

// 여부에 따라 승패가 나뉜다

srand(time(NULL));

startView();

while(money > 0 && inputKey != 27){

cardSelect();

gamePlay();

}

endView();



}

void startView() {

cout << endl;

cout << "          월남뽕          " << endl;

cout << endl;

cout << "   게임을 시작하겠습니다." << endl;

cout << endl;

cout << "     P r e s s  K E Y" << endl;

cout << endl;

}

void cardSelect() {

do 

{

slot1 = rand() % 52;

slot2 = rand() % 52;

slot3 = rand() % 52;

if (slot1 != slot2 && slot1 != slot2 && slot2 != slot3) break;

} while (1);

cSlot1 = slot1 % 13;

cSlot2 = slot2 % 13;

cSlot3 = slot3 % 13;

void gamePlay() {

system("cls");

cout << endl;

cout << "       보유 금액 : " << money << "원 " << endl;

cout << endl;

cout << "            월남뽕          " << endl;

cout << endl;

cout << "     [ " << card[slot1] << " ]" << "\t " << "[ " << card[slot2] << " ]"<< endl;

cout << "            [  ?  ]" << endl;

cout << "  진행을 하시려면 아무키나 눌러주세요" << endl;

cout << " 게임을 종료하시려면 ESC키를 눌러주세요" << endl;

inputKey = getch();

cout << " 배팅 금액을 입력하세요 : ";

if (inputKey != 27) {

cin >> bMoney;

while (money < bMoney) {

cout << " 배팅 금액이 보유 금액보다 높습니다. " << endl;

cout << " 배팅 금액을 입력하세요 : ";

cin >> bMoney;

}

money -= bMoney;

if (cSlot1 < cSlot2) {

if (cSlot1 <= cSlot3 && cSlot3 <= cSlot2) {

money = money + (bMoney * 2);

}

}

else {

if (cSlot2 <= cSlot3 && cSlot3 <= cSlot1) {

money = money + (bMoney * 2);

}

}

}

}


void endView() {

system("cls");

cout << endl;

cout << endl;

cout << endl;

cout << "          월남뽕          " << endl;

cout << endl;

cout << "   게임이 종료되었습니다" << endl;

cout << endl;



if (money == 0) {

cout << "       파산하였습니다" << endl;

}

else {

cout << "    보유 금액 : " << money << "원" << endl;

}

cout << endl;

}

ㅡㅡ 구동화면 ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

기존 룰은 ? 카드가 첫번째와 두번째 나온 카드 사이 숫자인지 여부를 확인해 배팅을 한다

? 카드가 사이값의 카드가 나올 시 배팅금액의 두배를 돌려준다.

아니라고 생각된다면 0원을 배팅할 수 있고 올인나거나 ESC 키를 입력하면 게임이 종료된다.

사정이 생겨 다른 것을 해결하느라 시간이 없어 보이기에는 허접할 수 있으나

요구 조건은 만족했다 생각되어 나름 만족 하지만 허접한건 사실







'SGA 스터디 > C++ 코딩' 카테고리의 다른 글

공 허들넘기 ㅋㅋ  (0) 2015.08.20
카드 뒤집기  (0) 2015.08.14
별찍기  (0) 2015.08.11
하이 로우 세븐 카드게임 ( High & Low 7 )  (0) 2015.08.11
슬롯머신 게임  (0) 2015.08.11

+ Recent posts