-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication7.sln
More file actions
91 lines (82 loc) · 2.55 KB
/
Copy pathConsoleApplication7.sln
File metadata and controls
91 lines (82 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int generate_new_position() //난수 발생기 (배열 내에 위치하도록)
{
int a;
a = rand() % 3;
return a;
}
void print_patern(int patern_map[3][3]) // 화면 출력
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d", patern_map[i][j]);
}
printf("\n");
}
}
int main(){
int patern_map[3][3] = { 0, }; //맵 배열
int time_stack = 0; //점을 찍은 횟수
int current_x = 0; //현재 x좌표
int current_y = 0; //현재 y좌표
int new_x = 0; //새로운 x좌표
int new_y = 0; //새로운 y좌표
int numberOfDots = 0; //찍을 점의 갯수
int end = 0; //프로그램의 종료를 체크
srand(time(NULL));
while (end == 0)
{
printf("점의 수를 입력해주세요 (4 이상 9 이하):");
scanf_s("%d", &numberOfDots);
if (numberOfDots >= 4 && numberOfDots <= 9)
{
while (time_stack != numberOfDots)
{
if (time_stack == 0) //처음 시작점
{
current_x = generate_new_position();
current_y = generate_new_position();
patern_map[current_x][current_y] = time_stack + 1;
time_stack++;
}
else if (time_stack > 0) //시작점이 찍힌 이후
{
new_x = generate_new_position();
new_y = generate_new_position();
if (patern_map[new_x][new_y] != 0) //이미 있는 위치에 잡힌 경우
{
new_x = generate_new_position();
new_y = generate_new_position();
}
else if ((abs(new_x - current_x) == 2 && (abs(new_y - current_y) == 0 || abs(new_y - current_y) == 2)) && (patern_map[1][new_y] == 0)) //상하로 한번에 두칸씩 넘어가는걸 방지
{
new_x = generate_new_position();
new_y = generate_new_position();
}
else if ((abs(new_y - current_y) == 2 && (abs(new_x - current_x) == 0 || abs(new_x - current_x) == 2)) && (patern_map[new_x][1] == 0)) //좌우로 한번에 두칸씩 넘어가는걸 방지
{
new_x = generate_new_position();
new_y = generate_new_position();
}
else // 새로운 위치에 점 입력
{
current_x = new_x;
current_y = new_y;
patern_map[current_x][current_y] = time_stack + 1;
time_stack++;
}
}
}
print_patern(patern_map);
end++;
}
else
{
printf("4~9 사이의 수를 입력해주세요\n");
}
}
}