/*               */
/*     otto      */
/*               */
#include <sys/bios.h>
#include <stdlib.h>
#include "title.h"  

#define BLACK 1
#define WHITE -1
#define SPACE 0
#define TATE 3
#define YOKO -3
#define BOXSIZE 13

typedef struct {
	int boxsize[BOXSIZE][BOXSIZE];
} table;

unsigned short scr1[(224/8)*(144/8)];

int x, y;  /* カーソル座標 */
int countb, countw;  /* コマのカウント */

/* 画面表示 */
void display (table *w){
	int a, b;
	char *c;
	
	for(b=0;b<13;b++){
		for(a=0;a<13;a++){
			switch(w->boxsize[b][a]){
				case WHITE: c = "○"; break;
				case BLACK: c = "●"; break;
				case TATE: c = "│"; break;
				case YOKO: c = "─"; break;
				default: c = "・"; break;
			}
			text_put_string(a+2, b+2, c);
		}
	}
}

/* キー入力 */
int input (int c, table *w) {
	int	k;

	cursor_display(1);
	
	while (1) {
		cursor_set_location(x+2, y+2, 1, 1);
	
		k = key_hit_check_with_repeat();
		if (k & KEY_START)		{cursor_display(0); bios_exit();}
	
		if (k & KEY_LEFT1)		x -= (x > 2) ? 2 : 0;
		if (k & KEY_RIGHT1)		x += (x < 10) ? 2 : 0;
		if (k & KEY_UP1)		y -= (y > 2) ? 2 : 0;
		if (k & KEY_DOWN1)		y += (y < 10) ? 2 : 0;
		if (k & KEY_A){
			if(w->boxsize[y][x] == SPACE){
				if(c == 1){
					w->boxsize[y][x] = BLACK;
					countb++;
				}	
				else if(c == -1){
					w->boxsize[y][x] = WHITE;
					countw++;
				}	
				return 0;
			}
		}

	}
}
/* 判定 */
int jugd(table *w)
{
	
	if(w->boxsize[y-2][x] == BLACK){
		w->boxsize[y-2][x] = WHITE;
		countb--;
		countw++;
	}	
	else if(w->boxsize[y-2][x] == WHITE){
		w->boxsize[y-2][x] = BLACK;
		countb++;
		countw--;
	}	
			
	if(w->boxsize[y][x+2] == BLACK){
		w->boxsize[y][x+2] = WHITE;
		countb--;
		countw++;
	}	
	else if(w->boxsize[y][x+2] == WHITE){
		w->boxsize[y][x+2] = BLACK;
		countb++;
		countw--;
	}	
			
	if(w->boxsize[y+2][x] == BLACK){
		w->boxsize[y+2][x] = WHITE;
		countb--;
		countw++;
	}	
	else if(w->boxsize[y+2][x] == WHITE){
		w->boxsize[y+2][x] = BLACK;	
		countb++;
		countw--;
	}	
		
	if(w->boxsize[y][x-2] == BLACK){
		w->boxsize[y][x-2] = WHITE;
		countb--;
		countw++;
	}	
	else if(w->boxsize[y][x-2] == WHITE){
		w->boxsize[y][x-2] = BLACK;	
		countb++;
		countw--;
	}						
}
/* 乱数回数 */
int random(int max){
	return rand() % (max + 1);
}
/* コンピュータの思考 */
int idea_enemy(int c, table *w)
{
	int foundb, foundw;
	int max_count=-1;
	int count;
	int i,j;
	int d=0,e;
	int scorex[20];
	int scorey[20];
	
	for(j=1;j<12;j+=2){
		for(i=1;i<12;i+=2){
			foundb=0;
			foundw=0;
			count =0;
			
			if(w->boxsize[j][i] != SPACE) continue;
			
			if(w->boxsize[j-2][i] == BLACK)
				foundb++;	
			else if(w->boxsize[j-2][i] == WHITE)
				foundw++;
						
			if(w->boxsize[j][i+2] == BLACK)
				foundb++;
			else if(w->boxsize[j][i+2] == WHITE)
				foundw++;
					
			if(w->boxsize[j+2][i] == BLACK)
				foundb++;
			else if(w->boxsize[j+2][i] == WHITE)
				foundw++;
					
			if(w->boxsize[j][i-2] == BLACK)
				foundb++;
			else if(w->boxsize[j][i-2] == WHITE)
				foundw++;
				
			if(c == BLACK){
				count = foundw;
				if(max_count<count){
					d = 0;
					max_count = count;
					scorex[d] = i;
					scorey[d] = j;
				}
				else if(max_count == count){
					d++;
					scorex[d] = i;
					scorey[d] = j;
				}	
			}
			else if(c == WHITE){
				count = foundb;
				if(max_count<count){
					d=0;
					max_count = count;
					scorex[d] = i;
					scorey[d] = j;
				}
				else if(max_count == count){
					d++;
					scorex[d] = i;
					scorey[d] = j;
				}	
			}
		}
	}
	
	e=random(d);

	x=scorex[e];
	y=scorey[e];
	
	if(c == BLACK){
		w->boxsize[y][x] = BLACK;
		countb++;
	}	
	else if(c == WHITE){
		w->boxsize[y][x] = WHITE;	
		countw++;
	}
}
/* タイトル表示 */
void title(void)
{
	int i;
	int k;
	
	for(i=0;i<504;i++)
		scr1[i]=i;

	
	lcd_set_sleep(0);
	
	font_set_colordata(0, 504, bmp_title);
	
	screen_set_char(SCREEN1, 0, 0, 224/8, 144/8, scr1);
	display_control(DCM_SCR1);
	
	
	lcd_set_sleep(1);
	

	k=key_wait();
	display_control(DCM_SCR2);
	if (k & KEY_START)	
		bios_exit();
				  
}
/* ゲームループ */
void gameplay(void)
{
	int a, b;
	int c;
	int j = 36;
	int k;
	table board;
	
	countb = 0, countw = 0;
	
	text_screen_init();
	
	text_put_string(0, 0, "otto for WonderSwan");
	text_put_string(17, 3, "残りの置き場所 36");
	text_put_string(17, 5, "●の番です!");
	text_put_string(17, 7, "● ＝ 0");
	text_put_string(17, 8, "○ ＝ 0");
	text_put_string(17,15, "START/EXIT");


	for(b=0;b<=12;b++)
		for(a=0;a<=12;a++)
			board.boxsize[b][a] = SPACE;
	for(b=0;b<=12;b+=2)
		for(a=1;a<=11;a+=2)
			board.boxsize[b][a] = YOKO;
	for(b=1;b<=11;b+=2)
		for(a=0;a<=12;a+=2)
			board.boxsize[b][a] = TATE;
	x = 1;
	y = 1;
	display (&board);
	while(1){
		c = 1;
		text_put_string(17, 5, "●の番です!");
		input (c, &board);
		jugd(&board);
		text_put_numeric(21, 7, 2, 0, countb);
		text_put_numeric(21, 8, 2, 0, countw);
		j--;
		text_put_string(17, 3, "残りの置き場所");
		text_put_numeric(25, 3, 2, 0, j);
		display (&board);
		if(j == 0)
			break;

		c = -1;
		text_put_string(17, 5, "○の番です!");
		sys_wait(75);
		idea_enemy(c, &board);
		jugd(&board);
		text_put_numeric(21, 7, 2, 0, countb);
		text_put_numeric(21, 8, 2, 0, countw);
		j--;
		text_put_string(17, 3, "残りの置き場所 ");
		text_put_numeric(25, 3, 2, 0, j);
		display (&board);
		if(j == 0)
			break;
	}	
	text_put_string(17, 13, "おつかれさま");
	if(countb > countw)
		text_put_string(17, 11, "黒の勝ち!!");
	else if(countb < countw)
		text_put_string(17, 11, "白の勝ち!!");
	else
		text_put_string(17, 11, "引き分けだよ〜");
	
	text_put_string(17,16, "  A  /RETRY");
	cursor_display(0);

	k=key_wait();
	if (k & KEY_A) 
		gameplay();
	
}				
main()
{

	srand((unsigned int)sys_get_tick_count());
	
	palette_set_color(1, 0x7654);
	lcd_set_color(0x6420, 0xfca8);
	
	cursor_set_type(1,30);

	title();   
	gameplay();


}

