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

#define X 7
#define Y 7

int turn;  /* 1:○, 2: ● */
int mode;  /* 0:未指定, 1:移動元指定, 2:移動先指定 */
int map[X][Y] =  {
	1, 0, 0, 0, 0, 0, 2, 
	0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 
	0, 0, 0, 0, 0, 0, 0, 
	2, 0, 0, 0, 0, 0, 1 };
char* chKoma[4] = { "　", "○", "●", "＃" };
int point[4];

unsigned int moto_x, moto_y;  /* 移動元座標 */
unsigned int saki_x, saki_y;  /* 移動先座標 */

int koma_chk(int x, int y) {
	int result;  /* 0:空白, 1:○, 2:●, 3:壁&範囲外  */
	if(x<0 | y<0 | x>=X | y>=Y) {  /* 範囲外 */
		result=3;
	} else {
		result=map[x][y];
	}
	return result;
}

void hanten(int x, int y) {
	int i, j;
	int koma;
	int tx, ty;
	for(j=-1; j<=1; j++) {
		for(i=-1; i<=1; i++) {
			tx=saki_x+i; ty=saki_y+j;
			if(tx>=0&&tx<X&&ty>=0&&ty<Y) {  /* 範囲内のみ */
				koma=koma_chk(tx, ty);
				if(koma==1 || koma==2) {  /* 駒があったら */
					map[tx][ty] = turn;  /* あまりよろしくないけど… */
				}
			}
		}
	}
}



void map_hyouji() {
	int i, j;
	int koma;
	point[0]=point[1]=point[2]=0;
	for(j=0; j<Y; j++) {
		for(i=0; i<X; i++) {
			koma=map[i][j];
			text_put_string(1+i*2, 2+j*2, chKoma[koma]);
			point[koma]++;  /* 駒の数を数えるの */
		}
	}
	text_put_numeric(23, 5, 2, NUM_PADSPACE|NUM_ALIGN_RIGHT, point[1]);
	text_put_numeric(23, 7, 2, NUM_PADSPACE|NUM_ALIGN_RIGHT, point[2]);
}




void print_screen() {
	/*************************１２３４５６７８９０１２３４５６７８９０１２３４５６７８***/
	text_put_string(  0,  0, "　♪♪♪　ＷｏｎｄｅｒＷｉｔｃｈ版　あたっくしぃ♪♪♪　");
	text_put_string(  0,  1, "┌─┬─┬─┬─┬─┬─┬─┐");
	text_put_string(  0,  2, "│　│　│　│　│　│　│　│　★☆★　盤の状態　★☆★");
	text_put_string(  0,  3, "├─┼─┼─┼─┼─┼─┼─┤");
	text_put_string(  0,  4, "│　│　│　│　│　│　│　│　　○：プレイヤー１");
	text_put_string(  0,  5, "├─┼─┼─┼─┼─┼─┼─┤　　　　　　　　ｘｘ点");
	text_put_string(  0,  6, "│　│　│　│　│　│　│　│　　●：プレイヤー２");
	text_put_string(  0,  7, "├─┼─┼─┼─┼─┼─┼─┤　　　　　　　　ｘｘ点");
	text_put_string(  0,  8, "│　│　│　│　│　│　│　│");
	text_put_string(  0,  9, "├─┼─┼─┼─┼─┼─┼─┤");
	text_put_string(  0, 10, "│　│　│　│　│　│　│　│");
	text_put_string(  0, 11, "├─┼─┼─┼─┼─┼─┼─┤");
	text_put_string(  0, 12, "│　│　│　│　│　│　│　│");
	text_put_string(  0, 13, "├─┼─┼─┼─┼─┼─┼─┤");
	text_put_string(  0, 14, "│　│　│　│　│　│　│　│");
	text_put_string(  0, 15, "└─┴─┴─┴─┴─┴─┴─┘");
	text_put_string(  0, 16, "プレイヤー１のターンです。パスは［Ｂ］ボタン　");
	/*************************１２３４５６７８９０１２３４５６７８９０１２３４５６７８***/
}

void cur_print(int x, int y) {
	static int old_x, old_y;
	cursor_set_location(x*2+1, y*2+2, 1, 1);
	text_put_string(old_x*2, 2+old_y*2, "│");
	text_put_string(old_x*2+2, 2+old_y*2, "│");
	text_put_string(x*2, 2+y*2, "《");
	text_put_string(x*2+2, 2+y*2, "》");
	old_x=x; old_y=y;
	return;
};
void select_cur_print(BOOL hyouji) {
	if(hyouji) {
		text_put_string(moto_x*2+1, moto_y*2+3, "↑");
	} else {
		text_put_string(moto_x*2+1, moto_y*2+3, "─");
	}
	return;
};


int move_dist(int mx, int my, int sx, int sy) {
	int result;
	result=((mx-sx)*(mx-sx)+(my-sy)*(my-sy));
	return result;
}

void main() {
	BOOL exit_flag;
	int cur_x, cur_y;  /* カーソルの x,y */
	int c;
	int dist;	
	
	text_screen_init();


	/* 初期化 */
	cur_x=cur_y=3;
	turn=1; mode=0;

	print_screen();
	map_hyouji();


	/* メインループ */
	exit_flag=FALSE;
	while(!exit_flag) {
		text_put_numeric(5, 16, 1, 0, turn);

		c = key_hit_check_with_repeat();
		if (c & KEY_START)		{ exit_flag=TRUE; }
		if (c & KEY_B)			{
			if(mode==0) { /* パス? */
				text_put_string(13, 16, "パス？　Ａ：はい　");
				if(key_wait() & KEY_A) {  /* パスします */
					map_hyouji();
					turn=turn%2+1;  /* 1→2, 2→1 */
					mode=0;
				}
				text_put_string(13, 16, "パスは［Ｂ］ボタン");
			} else { /* キャンセル */
				select_cur_print(FALSE);  /* 選択カーソルを消す */
				mode=0;
			}
		}
		if (c & KEY_A)			{
			switch(mode) {
				case 0:  /* 移動元指定? */
					if(koma_chk(cur_x, cur_y) != turn) break;  /* 自駒じゃなきゃダメ */
					moto_x=cur_x; moto_y=cur_y;
					select_cur_print(TRUE);  /* 選択カーソルを表示 */
					mode=1;
					break;
				case 1:  /* 移動先指定? */
					if(koma_chk(cur_x, cur_y) != 0) break;  /* 空白じゃなきゃダメ */
					saki_x=cur_x; saki_y=cur_y;
					dist=move_dist(moto_x, moto_y, saki_x, saki_y);  /* 移動距離は? */
					if(dist<9) { /* 8を越えていたら遠すぎて移動できないの */
						if(dist>3) {  /* 3を越えていたらジャンプなのだ */
							map[moto_x][moto_y]=0;  /* 移動元は空白にするのだ */
						}
						map[saki_x][saki_y]=turn;  /* 移動先に駒を置く */
						select_cur_print(FALSE);  /* 選択カーソルを表示 */
						hanten(saki_x, saki_y);
						map_hyouji();
						turn=turn%2+1;  /* 1→2, 2→1 */
						mode=0;
					}
					break;
				default:
					break;
			}
		}
		if (c & KEY_LEFT1)		cur_x -= (cur_x>0) ? 1 : 0;
		if (c & KEY_RIGHT1)		cur_x += (cur_x<6) ? 1 : 0;
		if (c & KEY_UP1)		cur_y -= (cur_y>0) ? 1 : 0;
		if (c & KEY_DOWN1)		cur_y += (cur_y<6) ? 1 : 0;
		cur_print(cur_x, cur_y);


		if(point[1]+point[2]+point[3]==49) {  /* 空白以外で全部埋まったら */
			exit_flag=TRUE;
		}
	}
	
	text_put_string(0, 16, "　　ゲーム終了！　　");
	if(point[1]==point[2]) {
		text_put_string(10, 16, "引き分けでした。");
	} else {
		if(point[1]>point[2]) {
			text_put_string(10, 16, "プレイヤー１　○の勝利！");
		} else {
			text_put_string(10, 16, "プレイヤー２　●の勝利！");
		}
	}
	text_put_string(0, 17, "HIT ANY KEY");
	key_wait();
}
