/*RULE
처음에 맵, 플레이어, 수명(3개)이 표시된다
1. 사과가 떨어진다
2. 사과를 방향키의 좌우로 피한다
3. 만약 사과에 맞으면 수명이 1개씩 줄어든다
4. 수명이 아예 없어지면 게임이 끝나고 재시작과 끝내는 방법이 적힌다
5. S를 누르면 다시시작 F를 누르면 끝
6. 중간에 그만하고 싶으면 F를 눌러 끝낸다
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h> //_getch가 포함되어있는 헤더
#include <process.h>
#include <Windows.h>
#define LEFT 75
#define RIGHT 77
CRITICAL_SECTION cs;
enum
{
BLACK, // 0 : 검은색
DARK_BLUE, // 1 : 어두운 파랑
DARK_GREEN, // 2 : 어두운 초록
DARK_SKY_BLUE, // 3 : 어두운 하늘
DARK_RED, // 4 : 어두운 빨강
DARK_VOILET, // 5 : 어두운 보라
DARK_YELLOW, // 6 : 어두운 노랑
GRAY, // 7 : 회색
DARK_GRAY, // 8 : 어두운 회색
BLUE, // 9 : 파랑
GREEN, // 10 : 초록
SKY_BLUE, // 11 : 하늘
RED, //12 : 빨강
VIOLET, //13 : 보라
YELLOW, //14 : 노랑
WHITE, //15 : 하얀색
};
int px=7,py=14;
char str[10]={};
int bx,by;
void reset(){
//게임을 시작, 재시작시 초기 셋팅
px=7; py=14;
}
void move(int x, int y)
{
COORD pos= {x*2,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void map(int x, int y)
{
move(x,y);
printf("#");
}
void apple_rand()
{
srand(time(NULL));
bx=rand()%15;
by=rand()%1;
}
void stage()
{
for(int i = 0;i<=14;i++){
move(i,14); printf(" ");
}
move(px,py);
printf("* ");
move(16,0);
printf("%s",str);
move(16,1);
printf("position = %3d",px);
}
// unsigned _stdcall Thread1(void* arg)
void Thread1()
{
while(1){
apple_rand();
EnterCriticalSection(&cs);
move(bx,by);
printf("@");
LeaveCriticalSection(&cs);
for(int i=1;i<=14;i++)
{
EnterCriticalSection(&cs);
move(bx,by);
printf(" ");
by++;
move(bx,by);
printf("@");
LeaveCriticalSection(&cs);
Sleep(100);
if(by==14)
{
move(bx,by);
printf(" ");
}
}
Sleep(500);
}
}
int main(void)
{
InitializeCriticalSection(&cs);
HANDLE thread1;
int a=0;
system("mode con:cols=80 lines=40");
printf("press 's' to start");
char c;
for (;;){
if (_kbhit()){ //키보드 입력 확인 (true / false)
c = _getch();
if( c=='s'){
system("cls");
px=7;
py=14;
a=0;
strcpy(str," ♥♥♥ ");
thread1 = _beginthreadex(NULL, 0,Thread1, 0, 0, NULL);
break;
}
}
}
for(int i=0; i<=15; i++){
map(15,i);
map(i,15);
}
/////////////////////game start///////////////////////////////////////////
move(0,16);
printf("press 'f' to finish");
stage();
for (;;)
{
if (_kbhit()) //키보드 입력 확인 (true / false)
{
c = _getch();
if(c=='f'){
system("cls");
return 0;
}
if (c == -32){ //방향키가 입력되면?
c = _getch();
switch (c){
case LEFT: px--; break;
case RIGHT: px++; break;
}
if(px==15) px--;
else if(px==-1) px++;
move(17,6); printf(" ");
if(px==bx && py==by) //crash!!
{
a++;
move(17,6); printf("crash!");
}
if(a==1)
{
strcpy(str," ♡♥♥ ");
move(16,0);
printf("%s",str);
}
else if(a==2)
{
strcpy(str," ♡♡♥ ");
move(16,0);
printf("%s",str);
}
else if(a==3)
{
strcpy(str," ♡♡♡ ");
move(16,0);
printf("%s",str);
Sleep(100);
system("cls");
printf("You died");
printf("\npress 's' to restart \npress 'f' to finish\n\n\n\n\n\n\n\n\n");
}
stage();
}
}
}WaitForSingleObject(thread1, INFINITE);
DeleteCriticalSection(&cs);
// game over
}
c언어 문법
자료 구조 data structure
1. stack , queue 스택, 큐
2. sort 정렬
3. binary search 이진탐색
4. search - dfs / bfs
stack 스택 <-> queue 큐
스택 : 쌓는거 , 나중에 들어온게 먼저 나간다 후입선출, LIFO
*/
/*
int stack[50];
int top =-1; // 스택의 초기화 (비어있다)
// top : 마지막데이터의 위치
void push(int data)
{
stack[++top]=data;
}
int pop()
{
if(top==-1) printf("stack is empty!!");
return stack[top--];
}
int main()
{
push(5);
push(3);
push(5);
push(3);
while(top!=-1) //스택에 있는 전부 pop하기
{
printf("%d",pop());
}
}
*/
/*
#include <stdio.h>
char stack[100000];
int top=-1;
void push(char data) {
stack[++top]=data;
}
char pop() {
if(top!=-1)
return stack[top--];
}
int main() {
char a[1000000];
scanf("%s",a);
for(int i=0; a[i]!=NULL; i++) {
push(a[i]);
}
while(top!=-1) { //스택에 있는 전부 pop하기
printf("%c",pop());
}
return 0;
}
*/
#include <stdio.h>
int stack[100000];
int top=-1;
void push(int data)
{
stack[++top]=data;
}
int pop()
{
if(top!=-1)
return stack[top--];
}
int main()
{
int a,t;
int d=0;
scanf("%d",&a);
for(int i=1;i<=a;i++)
{
scanf("%d",&t);
if(t==0)
{
pop();
}
else
{
push(t);
}
}
while(top!=-1)
{
d+=pop();
}
printf("%d",d);
}