U E D R , A S I H C RSS

새싹교실/2015/새벽반/0518

참여자

강사 반페이지 학생
장우진/새벽반성훈원희 창민
O O X


진행상황


  • 배열과 포인터에 대한 설명
  • 포인터를 가지고 연산 하기
  • call by reference?
  • call by value?
  • 질의응답

과제


  • 틱택토!!!
  • 마지막 과제입니다. 어려운거 해봅시다
  • 한 줄 빙고를 만들면 이기는 간단한 게임
  • 사용자와 컴퓨터가 대전
  • 선 플레이어 설정 기능과 난이도 설정 기능 구현
  • 간단한 GUI...

박성훈

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

void playerturn();
void computerturn();
void hardcomputerturn();
int attack();
int protect();
int playerwin();
int computerwin();
int draw();
int full[9] = { 0, };
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };


int main(){

	int a = 0;
	int first;
	int level;
	int levelfirst;
	int replay;

	srand((unsigned)time(NULL));

		
		printf("난이도를 골라주세요. 1.easy 2.hard\n");
		scanf("%d", &level);

		printf("누가 먼저 할껀가요? 1.사람 2. 컴퓨터\n");
		scanf("%d", &first);


		if (level == 1 && first == 1)
			levelfirst = 1;
		else if (level == 1 && first == 2)
			levelfirst = 2;
		else if (level == 2 && first == 1)
			levelfirst = 3;
		else
			levelfirst = 4;


		system("cls");

		for (;;){
			printf("┌─┬─┬─┐\n");
			printf("│%c │%c │%c │\n", matrix[0][0], matrix[0][1], matrix[0][2]);
			printf("├─┼─┼─┤\n");
			printf("│%c │%c │%c │\n", matrix[1][0], matrix[1][1], matrix[1][2]);
			printf("├─┼─┼─┤\n");
			printf("│%c │%c │%c │\n", matrix[2][0], matrix[2][1], matrix[2][2]);
			printf("└─┴─┴─┘\n");



			if (levelfirst == 1)
			{
				playerturn();
				levelfirst = 2;

			}

			else if (levelfirst == 2)
			{
				computerturn();
				levelfirst = 1;

			}


			else if (levelfirst == 3)
			{
				playerturn();
				levelfirst = 4;
			}

			else if (levelfirst == 4)
			{
				if (full[4] == 0)
				{
					matrix[1][1] = 'X';
					full[4] = 2;
					levelfirst = 3;
				}
				else
				{
					hardcomputerturn();
					levelfirst = 3;
				}

			}




			if (playerwin() == 1){
				system("cls");
				printf("┌─┬─┬─┐\n");
				printf("│%c │%c │%c │\n", matrix[0][0], matrix[0][1], matrix[0][2]);
				printf("├─┼─┼─┤\n");
				printf("│%c │%c │%c │\n", matrix[1][0], matrix[1][1], matrix[1][2]);
				printf("├─┼─┼─┤\n");
				printf("│%c │%c │%c │\n", matrix[2][0], matrix[2][1], matrix[2][2]);
				printf("└─┴─┴─┘\n");
				printf("YOU WIN!!!\n");
				break;
			}
			if (computerwin() == 1){
				system("cls");
				printf("┌─┬─┬─┐\n");
				printf("│%c │%c │%c │\n", matrix[0][0], matrix[0][1], matrix[0][2]);
				printf("├─┼─┼─┤\n");
				printf("│%c │%c │%c │\n", matrix[1][0], matrix[1][1], matrix[1][2]);
				printf("├─┼─┼─┤\n");
				printf("│%c │%c │%c │\n", matrix[2][0], matrix[2][1], matrix[2][2]);
				printf("└─┴─┴─┘\n");
				printf("You lose...\n");
				break;
			}
			if (draw() == 1){
				system("cls");
				printf("┌─┬─┬─┐\n");
				printf("│%c │%c │%c │\n", matrix[0][0], matrix[0][1], matrix[0][2]);
				printf("├─┼─┼─┤\n");
				printf("│%c │%c │%c │\n", matrix[1][0], matrix[1][1], matrix[1][2]);
				printf("├─┼─┼─┤\n");
				printf("│%c │%c │%c │\n", matrix[2][0], matrix[2][1], matrix[2][2]);
				printf("└─┴─┴─┘\n");
				printf("Draw\n");
				break;
			}

			system("cls");
		}
		

		return 0;
	}


	void playerturn()
	{
		int place = 0;
		int playerturnfinish = 0;
		printf("번호를 고르시오.\n");

		for (; playerturnfinish != 1;){
			scanf("%d", &place);

			if (place == 1)
			{
				if (full[0] == 0)
				{
					matrix[0][0] = 'O';
					full[0] = 1;
					playerturnfinish = 1;
				}
				else if (full[0] == 1 || full[0] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 2)
			{
				if (full[1] == 0)
				{
					matrix[0][1] = 'O';
					full[1] = 1;
					playerturnfinish = 1;
				}
				else if (full[1] == 1 || full[1] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 3)
			{
				if (full[2] == 0)
				{
					matrix[0][2] = 'O';
					full[2] = 1;
					playerturnfinish = 1;
				}
				else if (full[2] == 1 || full[2] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 4)
			{
				if (full[3] == 0)
				{
					matrix[1][0] = 'O';
					full[3] = 1;
					playerturnfinish = 1;
				}
				else if (full[3] == 1 || full[3] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 5)
			{
				if (full[4] == 0)
				{
					matrix[1][1] = 'O';
					full[4] = 1;
					playerturnfinish = 1;
				}
				else if (full[4] == 1 || full[4] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 6)
			{
				if (full[5] == 0)
				{
					matrix[1][2] = 'O';
					full[5] = 1;
					playerturnfinish = 1;
				}
				else if (full[5] == 1 || full[5] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 7)
			{
				if (full[6] == 0)
				{
					matrix[2][0] = 'O';
					full[6] = 1;
					playerturnfinish = 1;
				}
				else if (full[6] == 1 || full[6] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 8)
			{
				if (full[7] == 0)
				{
					matrix[2][1] = 'O';
					full[7] = 1;
					playerturnfinish = 1;
				}
				else if (full[7] == 1 || full[7] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}
			else if (place == 9)
			{
				if (full[8] == 0)
				{
					matrix[2][2] = 'O';
					full[8] = 1;
					playerturnfinish = 1;
				}
				else if (full[8] == 1 || full[8] == 2)
				{
					printf("이미 기호가 존재합니다. 다른 번호를 눌러주세요.\n");
					continue;
				}
			}

		}
	}

	void computerturn()
	{
		int place = 0;
		int computerturnfinish = 0;
		

		for (; computerturnfinish != 1;){
			
			place = rand() % ((9 - 1) + 1)+1;

			if (place == 1)
			{
				if (full[0] == 0)
				{
					matrix[0][0] = 'X';
					full[0] = 2;
					computerturnfinish = 1;
				}
				else if (full[0] == 1 || full[0] == 2)
				{
					
					continue;
				}
			}
			else if (place == 2)
			{
				if (full[1] == 0)
				{
					matrix[0][1] = 'X';
					full[1] = 2;
					computerturnfinish = 1;
				}
				else if (full[1] == 1 || full[1] == 2)
				{
					continue;
				}
			}
			else if (place == 3)
			{
				if (full[2] == 0)
				{
					matrix[0][2] = 'X';
					full[2] = 2;
					computerturnfinish = 1;
				}
				else if (full[2] == 1 || full[2] == 2)
				{
					
					continue;
				}
			}
			else if (place == 4)
			{
				if (full[3] == 0)
				{
					matrix[1][0] = 'X';
					full[3] = 2;
					computerturnfinish = 1;
				}
				else if (full[3] == 1 || full[3] == 2)
				{
					
					continue;
				}
			}
			else if (place == 5)
			{
				if (full[4] == 0)
				{
					matrix[1][1] = 'X';
					full[4] = 2;
					computerturnfinish = 1;
				}
				else if (full[4] == 1 || full[4] == 2)
				{
					
					continue;
				}
			}
			else if (place == 6)
			{
				if (full[5] == 0)
				{
					matrix[1][2] = 'X';
					full[5] = 2;
					computerturnfinish = 1;
				}
				else if (full[5] == 1 || full[5] == 2)
				{
					
					continue;
				}
			}
			else if (place == 7)
			{
				if (full[6] == 0)
				{
					matrix[2][0] = 'X';
					full[6] = 2;
					computerturnfinish = 1;
				}
				else if (full[6] == 1 || full[6] == 2)
				{
					
					continue;
				}
			}
			else if (place == 8)
			{
				if (full[7] == 0)
				{
					matrix[2][1] = 'X';
					full[7] = 2;
					computerturnfinish = 1;
				}
				else if (full[7] == 1 || full[7] == 2)
				{
					
					continue;
				}
			}
			else if (place == 9)
			{
				if (full[8] == 0)
				{
					matrix[2][2] = 'X';
					full[8] = 2;
					computerturnfinish = 1;
				}
				else if (full[8] == 1 || full[8] == 2)
				{
					
					continue;
				}
			}

		}
	}

	int playerwin(){
		if (full[0] ==1 && full[1] ==1 && full[2] == 1)
			return 1;
		else if (full[3] == 1 && full[4] == 1 && full[5] == 1)
			return 1;
		else if (full[6] == 1 && full[7] == 1 && full[8] == 1)
			return 1;
		else if (full[0] == 1 && full[3] == 1 && full[6] == 1)
			return 1;
		else if (full[1] == 1 && full[4] == 1 && full[7] == 1)
			return 1;
		else if (full[2] == 1 && full[5] == 1 && full[8] == 1)
			return 1;
		else if (full[0] == 1 && full[4] == 1 && full[8] == 1)
			return 1;
		else if (full[2] == 1 && full[4] == 1 && full[6] == 1)
			return 1;

	}

	int computerwin(){
		if (full[0] == 2 && full[1] == 2 && full[2] == 2)
			return 1;
		else if (full[3] == 2 && full[4] == 2 && full[5] == 2)
			return 1;
		else if (full[6] == 2 && full[7] == 2 && full[8] == 2)
			return 1;
		else if (full[0] == 2 && full[3] == 2 && full[6] == 2)
			return 1;
		else if (full[1] == 2 && full[4] == 2 && full[7] == 2)
			return 1;
		else if (full[2] == 2 && full[5] == 2 && full[8] == 2)
			return 1;
		else if (full[0] == 2 && full[4] == 2 && full[8] == 2)
			return 1;
		else if (full[2] == 2 && full[4] == 2 && full[6] == 2)
			return 1;

	}

	int draw()
	{
		if (full[0] != 0 && full[1] != 0 && full[2] != 0 && full[3] != 0 && full[4] != 0 && full[5] != 0 && full[6] != 0 && full[7] != 0 && full[8] != 0)
		{
			return 1;
		}
	}

	void hardcomputerturn()
	{

		int place = 0;
		int computerturnfinish = 0;


		for (; computerturnfinish != 1;){

			place = rand() % ((9 - 1) + 1) + 1;
			
			if (attack() == 1)
			{
				computerturnfinish = 1;
				break;
			}
			else if (protect() == 1)
			{
				computerturnfinish = 1;
				break;
			}

					if (place == 1)
					{
						if (full[0] == 0)
						{
							matrix[0][0] = 'X';
							full[0] = 2;
							computerturnfinish = 1;
						}
						else if (full[0] == 1 || full[0] == 2)
						{

							continue;
						}
					}
					else if (place == 2)
					{
						if (full[1] == 0)
						{
							matrix[0][1] = 'X';
							full[1] = 2;
							computerturnfinish = 1;
						}
						else if (full[1] == 1 || full[1] == 2)
						{
							continue;
						}
					}
					else if (place == 3)
					{
						if (full[2] == 0)
						{
							matrix[0][2] = 'X';
							full[2] = 2;
							computerturnfinish = 1;
						}
						else if (full[2] == 1 || full[2] == 2)
						{

							continue;
						}
					}
					else if (place == 4)
					{
						if (full[3] == 0)
						{
							matrix[1][0] = 'X';
							full[3] = 2;
							computerturnfinish = 1;
						}
						else if (full[3] == 1 || full[3] == 2)
						{

							continue;
						}
					}
					else if (place == 5)
					{
						if (full[4] == 0)
						{
							matrix[1][1] = 'X';
							full[4] = 2;
							computerturnfinish = 1;
						}
						else if (full[4] == 1 || full[4] == 2)
						{

							continue;
						}
					}
					else if (place == 6)
					{
						if (full[5] == 0)
						{
							matrix[1][2] = 'X';
							full[5] = 2;
							computerturnfinish = 1;
						}
						else if (full[5] == 1 || full[5] == 2)
						{

							continue;
						}
					}
					else if (place == 7)
					{
						if (full[6] == 0)
						{
							matrix[2][0] = 'X';
							full[6] = 2;
							computerturnfinish = 1;
						}
						else if (full[6] == 1 || full[6] == 2)
						{

							continue;
						}
					}
					else if (place == 8)
					{
						if (full[7] == 0)
						{
							matrix[2][1] = 'X';
							full[7] = 2;
							computerturnfinish = 1;
						}
						else if (full[7] == 1 || full[7] == 2)
						{

							continue;
						}
					}
					else if (place == 9)
					{
						if (full[8] == 0)
						{
							matrix[2][2] = 'X';
							full[8] = 2;
							computerturnfinish = 1;
						}
						else if (full[8] == 1 || full[8] == 2)
						{

							continue;
						}
					}
				}
			

		}


	

	int attack()
	{
		if ((full[0] ==2 && full[1] ==2) &&full[2]==0)
		{
			full[2] = 2;
			matrix[0][2] = 'X';
			return 1;
		}
		else if ((full[0] == 2 && full[2] == 2) && full[1] == 0)
		{
			full[1] = 2;
			matrix[0][1] = 'X';
			return 1;
		}
		else if ((full[1] == 2 && full[2] == 2) && full[0] == 0)
		{
			full[0] = 2;
			matrix[0][0] = 'X';
			return 1;
		}
		else if ((full[3] == 2 && full[4] == 2) && full[5] == 0)
		{
			full[5] = 2;
			matrix[1][2] = 'X';
			return 1;
		}
		else if ((full[3] == 2 && full[5] == 2) && full[4] == 0)
		{
			full[4] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if ((full[5] == 2 && full[4] == 2) && full[3] == 0)
		{
			full[3] = 2;
			matrix[1][0] = 'X';
			return 1;
		}
		else if ((full[6] == 2 && full[7] == 2) && full[8] == 0)
		{
			full[8] = 2;
			matrix[2][2] = 'X';
			return 1;
		}
		else if ((full[6] == 2 && full[8] == 2) && full[7] == 0)
		{
			full[7] = 2;
			matrix[2][1] = 'X';
			return 1;
		}
		else if ((full[8] == 2 && full[7] == 2) && full[6] == 0)
		{
			full[6] = 2;
			matrix[2][0] = 'X';
			return 1;
		}
		else if ((full[0] == 2 && full[3] == 2) && full[6] == 0)
		{
			full[6] = 2;
			matrix[2][0] = 'X';
			return 1;
		}
		else if ((full[0] == 2 && full[6] == 2) && full[3] == 0)
		{
			full[3] = 2;
			matrix[1][0] = 'X';
			return 1;
		}
		else if ((full[6] == 2 && full[3] == 2) && full[3] == 0)
		{
			full[3] = 2;
			matrix[0][0] = 'X';
			return 1;
		}
		else if ((full[1] == 2 && full[4] == 2) && full[7] == 0)
		{
			full[7] = 2;
			matrix[2][1] = 'X';
			return 1;
		}
		else if ((full[1] == 2 && full[7] == 2) && full[4] == 0)
		{
			full[4] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if ((full[7] == 2 && full[4] == 2) && full[1] == 0)
		{
			full[1] = 2;
			matrix[0][1] = 'X';
			return 1;
		}
		else if ((full[2] == 2 && full[5] == 2) && full[8] == 0)
		{
			full[8] = 2;
			matrix[2][2] = 'X';
			return 1;
		}
		else if ((full[2] == 2 && full[8] == 2) && full[5] == 0)
		{
			full[5] = 2;
			matrix[1][2] = 'X';
			return 1;
		}
		else if ((full[8] == 2 && full[5] == 2) && full[2] == 0)
		{
			full[2] = 2;
			matrix[0][2] = 'X';
			return 1;
		}
		else if ((full[0] == 2 && full[4] == 2)&& full[8] == 0)
		{
			full[8] = 2;
			matrix[2][2] = 'X';
			return 1;
		}
		else if ((full[0] == 2 && full[8] == 2)&& full[4] == 0)
		{
			full[4] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if ((full[8] == 2 && full[4] == 2) && full[0] == 0)
		{
			full[0] = 2;
			matrix[0][0] = 'X';
			return 1;
		}
		else if ((full[2] == 2 && full[4] == 2) && full[6] == 0)
		{
			full[8] = 2;
			matrix[2][0] = 'X';
			return 1;
		}
		else if ((full[2] == 2 && full[6] == 2)&& full[4] == 0)
		{
			full[8] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if ((full[6] == 2 && full[4] == 2) && full[2] == 0)
		{
			full[2] = 2;
			matrix[0][2] = 'X';
			return 1;
		}
		else
		{
			return 0;
		}
	}
	int protect()
	{
		if (full[0] == 1 && full[1] == 1 && full[2] == 0)
		{
			full[2] = 2;
			matrix[0][2] = 'X';
			return 1;
		}
		else if (full[0] == 1 && full[2] == 1 && full[1] == 0)
		{
			full[1] = 2;
			matrix[0][1] = 'X';
			return 1;
		}
		else if (full[1] == 1 && full[2] == 1 && full[0] == 0)
		{
			full[0] = 2;
			matrix[0][0] = 'X';
			return 1;
		}
		else if (full[3] == 1 && full[4] == 1 && full[5] == 0)
		{
			full[5] = 2;
			matrix[1][2] = 'X';
			return 1;
		}
		else if (full[3] == 1 && full[5] == 1 && full[4] == 0)
		{
			full[4] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if (full[5] == 1 && full[4] == 1 && full[3] == 0)
		{
			full[3] = 2;
			matrix[1][0] = 'X';
			return 1;
		}
		else if (full[6] == 1 && full[7] == 1 && full[8] == 0)
		{
			full[8] = 2;
			matrix[2][2] = 'X';
			return 1;
		}
		else if (full[6] == 1 && full[8] == 1 && full[7] == 0)
		{
			full[7] = 2;
			matrix[2][1] = 'X';
			return 1;
		}
		else if (full[8] == 1 && full[7] == 1 && full[6] == 0)
		{
			full[6] = 2;
			matrix[2][0] = 'X';
			return 1;
		}
		else if (full[0] == 1 && full[3] == 1 && full[6] == 0)
		{
			full[6] = 2;
			matrix[2][0] = 'X';
			return 1;
		}
		else if (full[0] == 1 && full[6] == 1 && full[3] == 0)
		{
			full[3] = 2;
			matrix[1][0] = 'X';
			return 1;
		}
		else if (full[6] == 1 && full[3] == 1 && full[3] == 0)
		{
			full[3] = 2;
			matrix[0][0] = 'X';
			return 1;
		}
		else if (full[1] == 1 && full[4] == 1 && full[7] == 0)
		{
			full[7] = 2;
			matrix[2][1] = 'X';
			return 1;
		}
		else if (full[1] == 1 && full[7] == 1 && full[4] == 0)
		{
			full[4] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if (full[7] == 1 && full[4] == 1 && full[1] == 0)
		{
			full[1] = 2;
			matrix[0][1] = 'X';
			return 1;
		}
		else if (full[2] == 1 && full[5] == 1 && full[8] == 0)
		{
			full[8] = 2;
			matrix[2][2] = 'X';
			return 1;
		}
		else if (full[2] == 1 && full[8] == 1 && full[5] == 0)
		{
			full[5] = 2;
			matrix[1][2] = 'X';
			return 1;
		}
		else if (full[8] == 1 && full[5] == 1 && full[2] == 0)
		{
			full[2] = 2;
			matrix[0][2] = 'X';
			return 1;
		}
		else if (full[0] == 1 && full[4] == 1 && full[8] == 0)
		{
			full[8] = 2;
			matrix[2][2] = 'X';
			return 1;
		}
		else if (full[0] == 1 && full[8] == 1 && full[4] == 0)
		{
			full[4] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if (full[8] == 1 && full[4] == 1 && full[0] == 0)
		{
			full[0] = 2;
			matrix[0][0] = 'X';
			return 1;
		}
		else if (full[2] == 1 && full[4] == 1 && full[6] == 0)
		{
			full[8] = 2;
			matrix[2][0] = 'X';
			return 1;
		}
		else if (full[2] == 1 && full[6] == 1 && full[4] == 0)
		{
			full[8] = 2;
			matrix[1][1] = 'X';
			return 1;
		}
		else if (full[6] == 1 && full[4] == 1 && full[2] == 0)
		{
			full[2] = 2;
			matrix[0][2] = 'X';
			return 1;
		}
		else
		{
			return 0;
		}
	}

정창민


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:56
Processing time 0.0965 sec