U E D R , A S I H C RSS

Omok/상규

프로그램 소스

~cpp 
#include <iostream.h>
#include <conio.h>

#define UP_KEY 0x48
#define DOWN_KEY 0x50
#define LEFT_KEY 0x4B
#define RIGHT_KEY 0x4D
#define ESC_KEY 0x1B
#define SPACE_KEY 0x20

int check(int board[19][19], int x, int y, int n, int skip=-1);
int check33(int board[19][19], int x, int y);

void main()
{
	clrscr();
	for(int i=0;i<19;i++)
	{
		for(int j=0;j<19;j++) cout << " +";
		cout << endl;
	}

	int x=0, y=0;
	int key;
	int player=0;
	char playerchar[2]={'@', 'O'};
	int board[19][19]={0,};
	do {
		gotoxy((x+1)*2,y+1);
		key=getch();
		if(key==NULL)
		{
			key=getch();
			if(key==UP_KEY && y>0) y--;
			else if(key==DOWN_KEY && y<18) y++;
			else if(key==LEFT_KEY && x>0) x--;
			else if(key==RIGHT_KEY && x<18) x++;
		}
		else if(key==SPACE_KEY)
		{
			if(board[x][y]==0)
			{
				board[x][y]=player+1;
				if(check33(board,x,y)) board[x][y]=0;
				else
				{
					cout << playerchar[player] << '\b';
					if(check(board,x,y,5))
					{
						gotoxy(1,21);
						cout << playerchar[player] << " WIN";
						getch();
						return;
					}
					player=(player==0 ? 1 : 0);
				}
			}
		}
	} while(key!=ESC_KEY);
}

int check(int board[19][19], int x, int y, int n, int skip)
{
	int move[4][2][2]={ { { 1, 0 }, { -1, 0 } },
			{ { 0, 1 }, { 0, -1 } },
			{ { 1, 1 }, { -1, -1 } },
			{ { -1, 1 }, { 1, -1 } } };
	int count;
	int testx, testy;
	for(int i=0;i<4;i++)
	{
		if(i==skip) continue;
		count=1;
		for(int j=0;j<2;j++)
		{
			testx=x+move[i][j][0];
			testy=y+move[i][j][1];
			while(testx>=0 && testx<=18 && testy>=0 && testy<=18
				&& board[testx][testy]==board[x][y])
			{
				count++;
				testx+=move[i][j][0];
				testy+=move[i][j][1];
			}
		}
		if(count==n) return 1;
	}
	return 0;
}

int check33(int board[19][19], int x, int y)
{
	int move[4][2][2]={ { { 1, 0 }, { -1, 0 } },
			{ { 0, 1 }, { 0, -1 } },
			{ { 1, 1 }, { -1, -1 } },
			{ { -1, 1 }, { 1, -1 } } };
	int count;
	int testx, testy;
	for(int i=0;i<4;i++)
	{
		count=1;
		for(int j=0;j<2;j++)
		{
			testx=x+move[i][j][0];
			testy=y+move[i][j][1];
			while(testx>=0 && testx<=18 && testy>=0 && testy<=18
				&& board[testx][testy]==board[x][y])
			{
				count++;
				testx+=move[i][j][0];
				testy+=move[i][j][1];
			}
		}
		if(count==3)
		{
			if(check(board,x,y,3,i)) return 1;
			for(int j=0;j<2;j++)
			{
				testx=x+move[i][j][0];
				testy=y+move[i][j][1];
				while(testx>=0 && testx<=18 && testy>=0 && testy<=18
					&& board[testx][testy]==board[x][y])
				{
					if(check(board,testx,testy,3,i)) return 1;
					testx+=move[i][j][0];
					testy+=move[i][j][1];
				}
			}
		}
	}
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:54
Processing time 0.0222 sec