//http://poj.org/problem?id=1077
//Eight
//daun
#include <iostream>
#include <conio.h>
#include <winsock2.h>
#include <time.h>
#include <stdio.h>
using namespace std;
enum {BLACK,D_BLUE,D_GREEN,D_SKYBLUE,D_RED,D_VIOLET,D_YELLOW,GRAY,D_GRAY,BLUE,GREEN,SKYBLUE,RED,VIOLET,YELLOW,WHITE,};
void exchange(int *pa, int *pb);
void show(int[][4]);
void SetColor(int backcolor, int fontcolor);
int answer(int arr[][4]);
void start(int *pa, int *pb, int arr[][4]);
int main()
{
srand(time(NULL));
system("color F0");
char type;
int arr[4][4] = {0};
int x =0, y=0 ;
start(&x,&y,arr);
while(1)
{
SetColor(WHITE,VIOLET);
cout <<"\n\n UP : U or u, DOWN : J or j, RIGHT : K or k, LEFT : H or h"<<endl;
cout <<"\nIf you want to quit, Press the Z or z \n\n"<<endl;
show(arr);
switch(type=getch())
{
case 'U':
case 'u':
if((x-1)>=0)
exchange(&arr[x][y],&arr[x--][y]);
else
{
SetColor(WHITE,RED);
cout <<"\nCan't move"<<endl;
}
break;
case 'J':
case 'j':
if((x+1)<=3)
exchange(&arr[x][y],&arr[x++][y]);
else
{
SetColor(WHITE,RED);
cout<<"\nCan't move"<<endl;
}
break;
case 'K':
case 'k':
if((y+1)<=3)
exchange(&arr[x][y],&arr[x][y++]);
else
{
SetColor(WHITE,RED);
cout<<"\nCan't move"<<endl;
}
break;
case 'H':
case 'h':
if((y-1)>=0)
exchange(&arr[x][y],&arr[x][y--]);
else
{
SetColor(WHITE,RED);
cout<<"\nCan't move"<<endl;
}
break;
case 'z':
case 'Z':
return 0;
default:
SetColor(WHITE,RED);
cout<<"\nYou press wrong key"<<endl;
}
if(answer(arr)==16)
{
cout<<"Enswer!!!";
return 0;
}
Sleep(10);
system("cls");
}
return 0;
}
void exchange(int *pa, int *pb)
{
int temp;
temp = *pa;
*pa = *pb;
*pb = temp;
}
void show(int arr[][4])
{
SetColor(WHITE,BLACK);
for(int a = 0; a < 4 ; a++)
{
for(int b = 0 ; b <4 ; b++)
{
if(arr[a][b]==16)
cout<< 'X' <<"\t";
else
cout << arr[a][b] << "\t";
}
cout << endl;
}
}
void SetColor(int backcolor, int fontcolor)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), backcolor*16+fontcolor);
}
int answer(int arr[][4])
{
int count=1;
for(int a = 0 ; a < 4; a++)
for(int b = 0; b<4 ; b++)
if(arr[a][b]==count)
count++;
return count;
}
void start(int *pa, int *pb, int arr[][4])
{
for(int i = 1 ; i < 17; i++)
{
while(1)
{
*pa = rand()%4;
*pb = rand()%4;
if(arr[*pa][*pb])
continue;
arr[*pa][*pb]=i;
break;
}
}
}
'Code > Algorism' 카테고리의 다른 글
[ALGORISM]poj 1298 (0) | 2011.09.03 |
---|---|
[ALGORISM]poj 2853 (0) | 2011.09.03 |
[ALGORISM]poj 1163 (0) | 2011.09.03 |
[ALGORISM]poj 1003 (0) | 2011.09.03 |
[ALGORISM] poj 3085 (0) | 2011.09.03 |