#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#define LEFT '4'
#define RIGHT '6'
#define UP '8'
#define DOWN '2'
#define ENTER '5'
#define CLEAR 0
#define NONE 0
#define MAPSIZE 19
#define PLAYER1 1
#define PLAYER2 2
#define PLAY 1
#define START 1
#define END 0
#define NO 0
#define OK 1
#define YES 1
#define OKTOTALCOUNT 4
#define FLAGTRUE 1
#define FLAGFALSE 0
void gotoxy(int x, int y);
int xy[MAPSIZE][MAPSIZE];
int map[MAPSIZE][MAPSIZE];
int x, y, x2 = 2;
int centerX=MAPSIZE/2, centerY=MAPSIZE/2;
int displayX=0, displayLineY=21, displayY=22;
char mapChar[]="┌┬┐├┼┤└┴┘";
void clearValue() {
int x,y;
for(y=0;y<MAPSIZE;y++) {
for(x=0;x<MAPSIZE;x++) {
map[y][x]=CLEAR;
}
}
}
int IsKill(int cnt) {
return cnt==OKTOTALCOUNT;
}
int TestFlag(int x,int y) {
if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return FLAGTRUE;
if(map[y][x]==OK) return FLAGTRUE;
return FLAGFALSE;
}
int SefeDeleteTest(int x,int y,int OtherPlayer) {
int result;
int cnt;
if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return OK;
if(xy[y][x]==OtherPlayer) return OK;
if(xy[y][x]==NONE) return NO;
cnt=0;
result=NO;
map[y][x]=OK;
if(TestFlag(x,y-1)==FLAGFALSE) {
if(SefeDeleteTest(x,y-1,OtherPlayer)) {
cnt=cnt + 1;
}
} else {
cnt=cnt+1;
}
if(TestFlag(x-1,y)==FLAGFALSE) {
if(SefeDeleteTest(x-1,y,OtherPlayer)) {
cnt=cnt + 1;
}
} else {
cnt=cnt+1;
}
if(TestFlag(x+1,y)==FLAGFALSE) {
if(SefeDeleteTest(x+1,y,OtherPlayer)) {
cnt=cnt + 1;
}
} else {
cnt=cnt+1;
}
if(TestFlag(x,y+1)==FLAGFALSE) {
if(SefeDeleteTest(x,y+1,OtherPlayer)) {
cnt=cnt + 1;
}
} else {
cnt=cnt+1;
}
if(IsKill(cnt)){
result=OK;
}
return result;
}
void DolDelete(int x,int y,int OtherPlayer) {
if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return;
if(xy[y][x]==OtherPlayer) return;
if(xy[y][x]==NONE) return;
xy[y][x]=CLEAR;
if(x+1<MAPSIZE) DolDelete(x+1,y,OtherPlayer);
if(x-1>=0) DolDelete(x-1,y,OtherPlayer);
if(y+1<MAPSIZE) DolDelete(x,y+1,OtherPlayer);
if(y-1>=0) DolDelete(x,y-1,OtherPlayer);
}
void RalDeleteTest(int x,int y,int OtherPlayer) {
clearValue();
if(SefeDeleteTest(x+1,y,OtherPlayer)==YES)
DolDelete(x+1,y,OtherPlayer);
clearValue();
if(SefeDeleteTest(x-1,y,OtherPlayer)==YES)
DolDelete(x-1,y,OtherPlayer);
clearValue();
if(SefeDeleteTest(x,y+1,OtherPlayer)==YES)
DolDelete(x,y+1,OtherPlayer);
clearValue();
if(SefeDeleteTest(x,y-1,OtherPlayer)==YES)
DolDelete(x,y-1,OtherPlayer);
}
void input(int Player) {
char c;
int LOOP=START;
int OtherPlayer;
if(Player == PLAYER1) {
OtherPlayer = PLAYER2;
} else {
OtherPlayer = PLAYER1;
}
while(LOOP!=END){
c = getch();
switch(c)
{
case RIGHT:
x=x+1;
if(x >= MAPSIZE) x=x-1;
gotoxy(x,y);
break;
case LEFT:
x=x-1;
if(x < 0 ) x=x+1;
gotoxy(x,y);
break;
case UP:
y=y-1;
if(y < 0 ) y=y+1;
gotoxy(x,y);
break;
case DOWN:
y=y+1;
if(y >= MAPSIZE ) y=y-1;
gotoxy(x,y);
break;
case ENTER:
if(xy[y][x] != NONE ) break;
xy[y][x]=Player;
RalDeleteTest(x,y,Player);
clearValue();
if(SefeDeleteTest(x,y,OtherPlayer)==YES){
xy[y][x]=CLEAR;
} else {
LOOP = END;
}
}
}
}
void printScreen(){
system("cls");
int x,y,n;
char c[3];
for(y=0;y<MAPSIZE;y++) {
for(x=0;x<MAPSIZE;x++) {
if(xy[y][x] == PLAYER1) {
-
} else if(xy[y][x] == PLAYER2 ) {
-
} else if(xy[y][x] == NONE ) {
n=(x+16)/17+(y+16)/17*3;
c[0]=mapChar[n*2];
c[1]=mapChar[n*2+1];
c[2]=0;
if((x-3)%6==0 && (y-3)%6==0) {
strcpy(c,"×");
}
-
}
}
-
}
gotoxy(displayX,displayLineY);
printf("UP:8 DOWN:2 LEFT:4 RIGHT:6 ENTER:5");
}
int gamePlayCheck(int Player)
{
//승패를 가른다.
return 1;
}
void InitMapData() {
int x,y;
for(y=0;y<MAPSIZE;y++) {
for(x=0;x<MAPSIZE;x++) {
xy[y][x]=CLEAR;
}
}
}
// 커서를 나타냅니다.
void SetCursorShow(void)
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ci;
GetConsoleCursorInfo( hConsole, &ci );
ci.dwSize=100;
ci.bVisible = TRUE;
SetConsoleCursorInfo( hConsole , &ci );
}
void main() {
int Player=PLAYER1,GamePlayFlag=PLAY;
InitMapData();
SetCursorShow();
clearValue();
printScreen();
x=centerX;
y=centerY;
while(GamePlayFlag!=END){
gotoxy(displayX,displayY);
printf("%d 님이 두실 차례입니다.",Player);
gotoxy(x,y);
input(Player);
printScreen();
if(Player==PLAYER1) {
Player=PLAYER2;
} else {
Player=PLAYER1;
}
GamePlayFlag = gamePlayCheck(Player);
}
gotoxy(displayX,displayY);
-
}
void gotoxy(int x , int y)
{
COORD Pos = { x*2 , y};
SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE), Pos);
}