반응형

#pragma once


#include <conio.h>
#include <windows.h>

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


#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);
}

 

// myfunc

 

 

#include "stdafx.h"
 #include <stdio.h>
 #include "myfunc.h"
 #include <random>
 using namespace std;
 #include <conio.h>

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

 int main()
 {
  random_device rn;
  mt19937_64 rnd(rn());
  uniform_int_distribution<int> range(0, 2); // 1부터 45사이에서 출력한다.

  int board[3][3] = { 0 };
  int result[3][3] = { 0 };
  int count = 0;
  int ran1 = 0, ran2 = 0;
  int ran3 = 0, ran4 = 0;
  int tmp = 0;
  int inputkey = 0;
  int x = 0, y = 0;
  int gameFlag = 1;
  int resultcount = 0;
  int debugcount = 0;

  for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
    count++;
    board[i][j] = count;
    result[i][j] = count;
   }
   printf("\n");
  }
  count = 0;

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

   ran1 = range(rn);
   ran2 = range(rn);
   ran3 = range(rn);
   ran4 = range(rn);

   tmp = board[ran1][ran2];
   board[ran1][ran2] = board[ran3][ran4];
   board[ran3][ran4] = tmp;
  }

  while (gameFlag) {

   system("cls");
   
   resultcount = 0;
   for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
     if (board[i][j] == result[i][j])
      resultcount++;
    }
   }

   for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {

     if (board[i][j] == 9) {
      y = i; x = j;
      setColor(BLACK, BLACK);
      printf("%2d", board[i][j]);
     }
     else {
      if (board[i][j] % 2 == 0) {
       setColor(RED, WHITE);
       printf("%2d", board[i][j]);
      }
      else if (board[i][j] % 2 == 1) {
       setColor(BLUE, WHITE); printf("%2d", board[i][j]);
      }
     setColor(BLACK, WHITE);
     }

    }
    printf("\n");
   }
   Sleep(50);

   inputkey = _getch();
   if (inputkey == LEFT) {
    int tmp2 = 0;
    if ((x + 1) < 3) {
     tmp2 = board[y][x];
     board[y][x] = board[y][x + 1];
     board[y][x + 1] = tmp2;
     tmp2 = 0;
    }
   }
   else if (inputkey == RIGHT) {
    int tmp2 = 0;
    if ((x - 1) >= 0) {
     tmp2 = board[y][x];
     board[y][x] = board[y][x - 1];
     board[y][x - 1] = tmp2;
     tmp2 = 0;
    }
   }
   else if (inputkey == UP) {
    int tmp2 = 0;
    if ((y + 1) < 3) {
     tmp2 = board[y][x];
     board[y][x] = board[y + 1][x];
     board[y + 1][x] = tmp2;
     tmp2 = 0;
    }
   }
   else if (inputkey == DOWN) {
    int tmp2 = 0;
    if ((y - 1) >= 0) {
     tmp2 = board[y][x];
     board[y][x] = board[y - 1][x];
     board[y - 1][x] = tmp2;
     tmp2 = 0;
    }
   }
   else if (inputkey == SPACE) {
    for (int i = 0; i < 3; i++) {
     for (int j = 0; j < 3; j++) {
      debugcount++;
      board[i][j] = debugcount;
     }
    }
   }count = 0;

   if (resultcount > 8){
    gameFlag = 0;
    resultcount = 0;
    setColor(BLACK, WHITE);
    printf("정답 ! 완성하였습니다.");
   }
  }
  return 0;
 }

==============================================================================================

//교수님 소스

#include "stdafx.h"

#include "myFunc.h"
#include <stdlib.h>
#include <conio.h>
#include <random>

using namespace std;

#define MAX 5

#define LE 75
#define RI 77
#define UP 72
#define DO 80
#define SPACE 32

#define ME 0
#define COM 1

void swap(int *a, int *b)
{
 int tmp = *a;
 *a = *b;
 *b = tmp;
}

void dist(int board[MAX][MAX])
{
 random_device rn;
 mt19937_64 rnd(rn());
 uniform_int_distribution<int> range(0, MAX - 1);

 int count = 0;
 int dc1 = 0, dc2 = 0, dc3 = 0, dc4 = 0;
 for (int y = 0; y < MAX; y++)
 {
  for (int x = 0; x < MAX; x++)
  {
   board[y][x] = ++count;
   if (count == MAX * MAX) board[y][x] = 0;
  }
 }
 for (int i = 0; i < 1000; i++)
 {
  dc1 = range(rn);
  dc2 = range(rn);
  dc3 = range(rn);
  dc3 = range(rn);
  swap(board[dc1][dc2], board[dc3][dc4]);
 }
}

int main()
{
 // tic-tac-toe variables
 int board[MAX][MAX] = { 0, };
 int cursor_x = 0, cursor_y = 0;
 int inputKey = 0;

 dist(board);
 while (1)
 {
  // Drawing
  system("cls");
  printf("\n");
  for (int y = 0; y < MAX; y++)
  {
   printf("   ");
   for (int x = 0; x < MAX; x++)
   {
    if (board[y][x] == 0)
    {
     setColor(BLACK, WHITE);
     printf("    ");
     cursor_x = x;
     cursor_y = y;
    }
    else if (board[y][x] % 2 == 0)
    {
     setColor(RED, WHITE);
     printf("%3d ", board[y][x]);
    }
    else
    {
     setColor(BLUE, WHITE);
     printf("%3d ", board[y][x]);
    }

    setColor(BLACK, WHITE);
   }
   printf("\n");
  }

  // input
  inputKey = _getch();
  if (inputKey == LE)
  {
   if (cursor_x >= 0 && cursor_x < (MAX - 1))
   {
    board[cursor_y][cursor_x] = board[cursor_y][cursor_x + 1];
    board[cursor_y][cursor_x + 1] = 0;
    cursor_x--;
   }
  }
  else if (inputKey == RI)
  {
   if (cursor_x <= MAX - 1 && cursor_x > 0)
   {
    board[cursor_y][cursor_x] = board[cursor_y][cursor_x - 1];
    board[cursor_y][cursor_x - 1] = 0;
    cursor_x++;
   }
  }
  else if (inputKey == UP)
  {
   if (cursor_y >= 0 && cursor_y < (MAX - 1))
   {
    board[cursor_y][cursor_x] = board[cursor_y + 1][cursor_x];
    board[cursor_y + 1][cursor_x] = 0;
    cursor_y--;
   }
  }
  else if (inputKey == DO)
  {
   if (cursor_y <= MAX - 1 && cursor_y > 0)
   {
    board[cursor_y][cursor_x] = board[cursor_y - 1][cursor_x];
    board[cursor_y - 1][cursor_x] = 0;
    cursor_y++;
   }
  }
  else if (inputKey == SPACE)
  {
   // Cheat
   int tmpCount = 0;
   for (int i = 0; i < MAX; i++)
   {
    for (int j = 0; j < MAX; j++)
    {
     board[i][j] = ++tmpCount;
     if (tmpCount == MAX * MAX) board[i][j] = 0;
    }
   }
  }
 }
}

 

반응형

'Study > C' 카테고리의 다른 글

C언어 - 비트 연산  (0) 2018.08.29
C언어 - Tictactoe 게임  (0) 2018.08.28
C언어 - 배열 포인터  (0) 2018.08.28
C언어 - 포인터 2  (0) 2018.08.28
C언어 - 포인터  (0) 2018.08.28

+ Recent posts