#include <sys/bios.h>
#include <stdio.h>
#include <stdlib.h>

/*vg^CvιΎ*/
int getrnd(int type);
void shuffle(int sh);
void init();
void title();
void game();
void mv();
int hnt();

/*ΟιΎ*/
static int SHUF;
static int i,j;
static int xx=3,yy=3;
static int sx=3,sy=3;
static char sw1='',sw2='';
static int pz[4][4];

static int cx,cy;

static int ttt;

void main(){
	init();		/*ϊ»*/
	title();	/*^Cg\¦*/
	shuffle(SHUF);	/*zρπVbt*/

	game();

	for (i=0;i<4;i++) {
		for (j=0;j<4;j++) {
			if (pz[i][j]<10) { sw1='a'; }
			else			 { sw2='b';}
		}
	}

}

static void init(){
	SHUF=50;
	cx=0;
	cy=0;
	text_screen_init();

	for (i=0;i<4;i++) {
		for (j=0;j<4;j++) pz[i][j] = i*4+j+1;
	}
}

static void shuffle(int sh){

	int tap;
	char sw2='';
	static int sx=3,sy=3;	/*EΊ(=uN^C)π¦·*/
	srand(sys_get_tick_count() & 0x7fff);
	for (;sh>0;sh--){
		tap = rand()%4;
		sw2 = rand()%2 ? 'x' : 'y';
		if (sw2=='x') {
			if (tap < sx){
				for (i=sx;i>=tap && i>0;--i)   pz[sy][i]=pz[sy][i-1];
			}else if (tap > sx) {
				for (i=sx;i <= tap && i<3;++i) pz[sy][i]=pz[sy][i+1];
			}else{
				i=tap;
			}
			pz[sy][i]=16;	sx=i;
		}else{
			if (tap < sy){
				for (i=sy;i>=tap && i>0;--i)   pz[i][sx]=pz[i-1][sx];
			}else if (tap > sy) {
				for (i=sy;i <= tap && i<3;++i) pz[i][sx]=pz[i+1][sx];
			}else{
				i=tap;
			}
			pz[i][sx]=16;	sy=i;
		}
	}
}

static void title(){
	text_put_string(7,3,"");
	text_put_string(7,4,"@15@Game@@");
	text_put_string(7,5,"");
	text_put_string(8,10,"VbtρF");
	text_put_string(2,14,"© ¨ΕVbtρπίu`vΕrs`qs");
	sys_wait(7);

	while (-1) {
		int *pt1 = &SHUF;
		switch (key_press_check()) {
		case KEY_LEFT1:
			if (SHUF>10)  SHUF-=10;
			break;
		case KEY_RIGHT1:
			if (SHUF<100) SHUF+=10;
			break;
		case KEY_START:
			bios_exit();
			break;
		case KEY_A:
			return;
			break;
		}
		text_put_numeric(16, 10, 3, NUM_PADZERO ,*pt1);
		sys_wait(5);
	}
}

static void game(){

	int *pt2[16];
	int x=0,y=0;
	for (y=0;y<4;y++){
		for (x=0;x<4;x++) pt2[y*4+x] = &pz[y][x];
	}
	
	while (hnt() != 1) {
		switch (key_press_check()) {
		case KEY_UP1:
			if (cy>0) cy--;
			break;
		case KEY_RIGHT1:
			if (cx<3) cx++;
			break;
		case KEY_DOWN1:
			if (cy<3) cy++;
			break;
		case KEY_LEFT1:
			if (cx>0) cx--;
			break;
		case KEY_A:
			mv();
			break;
		case KEY_START:
			bios_exit();
			break;
		}
		text_screen_init();
		text_put_string(20,17,"startΕIΉ");
		for (y=0;y<4;y++){
			for (x=0;x<4;x++) {
				if (cx == x && cy == y) text_put_string((cx)*4+3-1,(cy)*2+3, "[");
				if (pz[y][x]!=16){
					text_put_numeric((x)*4+3,(y)*2+3, 2, NUM_PADZERO ,*pt2[y*4+x]);
				}
				if (cy == x && cy == y) text_put_string((cx)*4+3+2,(cy)*2+3, "]");
			}
		}
		sys_wait(7);
	}
	text_screen_init();
	text_put_string(5,5,"CongratulationsI");
	text_put_string(5,6,"vOπIΉ΅ά·");
	sys_wait(220);
	bios_exit();

	sys_wait(75);
}
static void mv(){
	if (cy>0) {	/*ue*/
		if (pz[cy-1][cx] == 16){
			pz[cy-1][cx] = pz[cy][cx];	pz[cy][cx] = 16;
			return;
		}
	}
	if (cx<3) {	/*migi*/
		if (pz[cy][cx+1] == 16){
			pz[cy][cx+1] = pz[cy][cx];	pz[cy][cx] = 16;
			return;
		}
	}
	if (cy<3) {	/*sita*/
		if (pz[cy+1][cx] == 16){
			pz[cy+1][cx] = pz[cy][cx];	pz[cy][cx] = 16;
			return;
		}
	}
	if (cx>0) {	/*hidari*/
		if (pz[cy][cx-1] == 16){
			pz[cy][cx-1] = pz[cy][cx];	pz[cy][cx] = 16;
			return;
		}
	}
	return;
}
static int hnt(){
	int hnt = 1;
	for (i=0;i<4;i++){
		for (j=0;j<4;j++) {
			if (pz[i][j] != i*4+j+1) {
				hnt = 0;
				return hnt;
			}
		}
	}
	return hnt;
}
