반응형

 



#include "stdafx.h"
#include <stdio.h>
#include "myfunc.h"
#include <conio.h>
#include <windows.h>

#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define SPACE 32


#define ON 1
#define OFF 0
#define MAX 5

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define IVORY 3
#define RED 4
#define PURPLE 5
#define YELLOW 6
#define WHITE 7
#define GRAY 8
#define SKYBLUE 9
#define YELLOWGREEN 10
#define WHITEIVORY 11
#define PINK 12
#define WHITEPURPLE 13
#define WHITEYELLOW 14
#define BOLDWHITE 15

#define BOARD_SIZE 5

void setColor(int background, int foreground)
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (background << 4) | foreground);
}

int main()
{
 
 int board[MAX][MAX] = { 0 };

 int player_x = 0, player_y = 0;
 int inputKey = 0;
 int led_count = 0;

 int boardsize = 0;
 while (1) {
  system("cls");
  
  setColor(BLACK, WHITE);
  printf(" %d X %d LED 켜기 \n", MAX, MAX);

  // 배열 출력
  for (int y = 0; y < MAX; y++) {
   for (int x = 0; x < MAX; x++)
   {
    if (player_x == x && player_y == y) {
     setColor(BLACK, PINK);
    }
    else if ((board[y][x]) == OFF) {

     setColor(BLACK, WHITE);
    }
    else{
     setColor(BLACK, BLUE);
    }
    printf("■"); 
   }
   printf("\n");
  }

  setColor(BLACK, WHITE);
  printf("켜진 LED 갯수 : %d\n",led_count);
  
  if (led_count == 25) {
   setColor(BLACK, WHITE);
   printf("축하합니다 모든 불이 켜졌습니다.");
  }
  //Sleep(100); // delay 함수
  inputKey = _getch(); // 키입력 함수

  // 좌우이동 및 제한
  if (inputKey == LEFT) {
   player_x--;
   if (player_x < 0) player_x = 0;
  }
  else if (inputKey == RIGHT) {
   player_x++;
   if (player_x > MAX - 1) player_x = MAX - 1;
  }
  else if (inputKey == UP) {
   player_y--;
   if (player_y < 0) player_y = 0;
  }
  else if (inputKey == DOWN) {
   player_y++;
   if (player_y > MAX - 1) player_y = MAX - 1;
  }

  else if (inputKey == SPACE){
 
   if (board[player_y][player_x] == ON) {
    board[player_y][player_x] = OFF;
    led_count--;
   }
   else {
    board[player_y][player_x] = ON;
    led_count++;
   }
   if (player_y < MAX - 1) {
    if (board[player_y + 1][player_x] == ON) {
     board[player_y + 1][player_x] = OFF;
     led_count--;
    }
    else {
     board[player_y + 1][player_x] = ON;
     led_count++;
    }
   }
   if (player_x < MAX - 1) {
    if (board[player_y][player_x + 1] == ON) {
     board[player_y][player_x + 1] = OFF;
     led_count--;
    }
    else {
     board[player_y][player_x + 1] = ON;
     led_count++;
    }
   }

   if (player_y > 0) {
    if (board[player_y - 1][player_x] == ON) {
     board[player_y - 1][player_x] = OFF;
     led_count--;
    }
    else {
     board[player_y - 1][player_x] = ON;
     led_count++;
    }
   }
   
   if (player_x > 0) {
    if (board[player_y][player_x - 1] == ON) {
     board[player_y][player_x - 1] = OFF;
     led_count--;
    }
    else {
     board[player_y][player_x - 1] = ON;
     led_count++;
    }
   }
   setColor(BLACK, BLUE);
  } 
 }
    return 0;
}

 

**

Window.h 헤더 파일 안에 Sleep()함수가 존재하며, 딜레이를 줄 수 있다.

반응형

+ Recent posts