๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ C Programming/๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ์ฆ๊ธฐ๋Š” C ์–ธ์–ด ์ฝ˜์„œํŠธ_๊ฐœ์ • 3ํŒ

[๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ์ฆ๊ธฐ๋Š” C ์–ธ์–ด ์ฝ˜์„œํŠธ - ๊ฐœ์ • 3ํŒ] 9์žฅ Mini Project ๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 1. 21.

# 9์žฅ Mini Project ๋ฌธ์ œ ํ’€์ด

 

 

367p_Mini Project_Pointer ์‚ฌ์šฉ X)

#include <stdio.h>
#include <stdlib.h>

char getinputt;

int main()
{
	char origin_map[10][10] = { {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'} };

	int index1 = 1;
	int index2 = 1;
	char tmp;

	origin_map[index1][index2] = '#';	//์‚ฌ์šฉ์ž
	origin_map[9][9] = 'G';				//๋ชฉํ‘œ

	printf("์™ผ์ชฝ ์ด๋™: a, ์šฐ์ธก ์ด๋™: d, ์œ„์ชฝ ์ด๋™: w, ์•„๋ž˜์ชฝ ์ด๋™: s\n");
	for (int i = 0; i < 10; i++)
	{
		for (int k = 0; k < 10; k++)
		{
			printf("%c ", origin_map[i][k]);
		}
		printf("\n");
	}

	while (1)
	{
		tmp = 0;
		getinputt = '0';
		int M1, M2, M3, M4;

		getinputt = (char)getch();

		if (getinputt == 'a')	//์ขŒ์ธก์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1][index2 - 1];
				origin_map[index1][index2 - 1] = tmp;
				index2--;
		}
		else if (getinputt == 'd')	//์šฐ์ธก์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1][index2 + 1];
				origin_map[index1][index2 + 1] = tmp;
				index2++;

				if (index1 == 9 && index2 == 9)
				{
					printf("์Šน๋ฆฌ๋ฅผ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค!\n");
					break;
				}
		}
		else if (getinputt == 's')    //์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1 + 1][index2];
				origin_map[index1 + 1][index2] = tmp;
				index1++;

				if (index1 == 9 && index2 == 9)
				{
					printf("์Šน๋ฆฌ๋ฅผ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค!\n");
					break;
				}
		}
		else    //์œ„์ชฝ์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1 - 1][index2];
				origin_map[index1 - 1][index2] = tmp;
				index1--;
		}

		srand(time(NULL));
		M1 = rand() % 10;
		M2 = rand() % 10;
		M3 = rand() % 10;
		M4 = rand() % 10;

		if (M1 != M3 || M4 != M2)
		{
			if (M1 != 9 && M2 != 9)
			{
				origin_map[M1][M2] = 'M';
			}
			if (M3 != 9 && M4 != 9)
			{
				origin_map[M3][M4] = 'M';
			}
		}

		if ((M1 == index1 && M2 == index2)||(M4 == index2 && M3 == index1))
		{
			printf("๊ฒŒ์ž„ ์‹คํŒจ");
			break;
		}

		printf("\n===== ์ด๋™ ๊ฒฐ๊ณผ =====\n");
		for (int i = 0; i < 10; i++)
		{
			for (int k = 0; k < 10; k++)
			{
				printf("%c ", origin_map[i][k]);
			}
			printf("\n");
		}
		printf("์™ผ์ชฝ ์ด๋™: a, ์šฐ์ธก ์ด๋™: d, ์œ„์ชฝ ์ด๋™: w, ์•„๋ž˜์ชฝ ์ด๋™: s\n");

		origin_map[M1][M2] = '0';
		origin_map[M3][M4] = '0';
	}

	return 0;
}

 

 

 

 

367p_Mini Project_Pointer ์‚ฌ์šฉ O)

#include <stdio.h>
#include <stdlib.h>

char getinputt;

void getinputt_init(char* b)
{
	*b = '0';
}

int main()
{
	char origin_map[10][10] = { {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'},
		{'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'} };

	int index1 = 1;
	int index2 = 1;
	char tmp;

	origin_map[index1][index2] = '#';	//์‚ฌ์šฉ์ž
	origin_map[9][9] = 'G';				//๋ชฉํ‘œ

	printf("์™ผ์ชฝ ์ด๋™: a, ์šฐ์ธก ์ด๋™: d, ์œ„์ชฝ ์ด๋™: w, ์•„๋ž˜์ชฝ ์ด๋™: s\n");
	for (int i = 0; i < 10; i++)
	{
		for (int k = 0; k < 10; k++)
		{
			printf("%c ", origin_map[i][k]);
		}
		printf("\n");
	}

	while (1)
	{
		tmp = 0;
		getinputt_init(&getinputt);
		int M1, M2, M3, M4;

		getinputt = (char)getch();

		if (getinputt == 'a')	//์ขŒ์ธก์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1][index2 - 1];
				origin_map[index1][index2 - 1] = tmp;
				index2--;
		}
		else if (getinputt == 'd')	//์šฐ์ธก์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1][index2 + 1];
				origin_map[index1][index2 + 1] = tmp;
				index2++;

				if (index1 == 9 && index2 == 9)
				{
					printf("์Šน๋ฆฌ๋ฅผ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค!\n");
					break;
				}
		}
		else if (getinputt == 's')    //์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1 + 1][index2];
				origin_map[index1 + 1][index2] = tmp;
				index1++;

				if (index1 == 9 && index2 == 9)
				{
					printf("์Šน๋ฆฌ๋ฅผ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค!\n");
					break;
				}
		}
		else    //์œ„์ชฝ์œผ๋กœ ์ด๋™
		{
				tmp = origin_map[index1][index2];
				origin_map[index1][index2] = origin_map[index1 - 1][index2];
				origin_map[index1 - 1][index2] = tmp;
				index1--;
		}

		srand(time(NULL));
		M1 = rand() % 10;
		M2 = rand() % 10;
		M3 = rand() % 10;
		M4 = rand() % 10;

		if (M1 != M3 || M4 != M2)
		{
			if (M1 != 9 && M2 != 9)
			{
				origin_map[M1][M2] = 'M';
			}
			if (M3 != 9 && M4 != 9)
			{
				origin_map[M3][M4] = 'M';
			}
		}

		if ((M1 == index1 && M2 == index2)||(M4 == index2 && M3 == index1))
		{
			printf("๊ฒŒ์ž„ ์‹คํŒจ");
			break;
		}

		printf("\n===== ์ด๋™ ๊ฒฐ๊ณผ =====\n");
		for (int i = 0; i < 10; i++)
		{
			for (int k = 0; k < 10; k++)
			{
				printf("%c ", origin_map[i][k]);
			}
			printf("\n");
		}
		printf("์™ผ์ชฝ ์ด๋™: a, ์šฐ์ธก ์ด๋™: d, ์œ„์ชฝ ์ด๋™: w, ์•„๋ž˜์ชฝ ์ด๋™: s\n");

		origin_map[M1][M2] = '0';
		origin_map[M3][M4] = '0';
	}

	return 0;
}