![article thumbnail image](https://blog.kakaocdn.net/dn/2FaWJ/btqEo7NHZV9/NAg2rofhkCkt288SCovx80/img.png)
반응형
- 0부터 24까지 겹치지 않게 무작위로 섞어서 출력한다. 단, 0은 언제나 맨 오른쪽 최하단에 위치해야 한다.
- 사용자로부터 방향키를 입력받음과 동시에 0을 해당 방향으로 한 칸 이동시킨다. 예를 들어 왼쪽(←) 버튼을 누른 경우, 0의 왼쪽에 있는 22와 자리를 바꾼다.
- 0이 가장자리에 위치하는 경우, 예를 들어, 0이 상단 사진의 21번 자리에 있다면, 왼쪽(←) 버튼과 아래쪽(↓) 버튼을 눌렀을 때 아무런 위치 변화가 일어나서는 안 된다.
https://www.youtube.com/watch?v=e8aU3ExfA2Q&feature=youtu.be
※ 공부 중에 만들어 본 코드입니다. 코드가 매끄럽지 않으며, 검색하지 않고 최대한 알고 있는 내용으로만 구현했으니 참고용으로만 봐 주세요!
#include <iostream>
#include <conio.h>
#include <time.h>
#include <Windows.h>
using namespace std;
void changeLocation(int dest, int sour, int* arr)
{
int temp = arr[dest];
arr[dest] = arr[sour];
arr[sour] = temp;
}
int main()
{
srand(time(NULL));
int locationZero = 24;
int arr[25];
for (int i = 0; i < 24; i++)
arr[i] = i + 1;
for (int i = 0; i < 77; i++)
{
int dest, sour, temp;
dest = rand() % 24;
sour = rand() % 24;
changeLocation(dest, sour, arr);
}
arr[24] = 0;
while (true)
{
system("cls");
int locationZeroNow = locationZero;
for (int i = 0; i < 25; i++)
{
cout << arr[i] << '\t';
if ((i + 1) % 5 == 0) cout << endl << endl;
}
int key;
key = _getch();
if (key == 224)
key = _getch();
switch (key)
{
case 72:
if (locationZero < 5) break;
locationZero -= 5;
break;
case 75:
if (locationZero % 5 == 0) break;
locationZero -= 1;
break;
case 77:
if (locationZero % 5 == 4) break;
locationZero += 1;
break;
case 80:
if (locationZero > 19) break;
locationZero += 5;
break;
case 27:
// Esc, 임시 종료 조건
return 0;
default:
continue;
}
{
int dest = locationZeroNow, sour = locationZero;
changeLocation(dest, sour, arr);
}
}
return 0;
}
반응형