/*
 *
 * smain.c -- main program
 *
 */

#include "scrab.h"
#include "globals.h"


set_up_window() {
	int xsize, ysize;
	WINDOW *screen;

	screen = initscr();
	refresh();
	if( screen->_maxx < 80 || screen->_maxy < 24 ) {
		endwin();
		fprintf( stderr, "Bezonas ekranon je almenaux 24x80\n" );
		exit( 1 );
	}
	cbreak();
	noecho();
}

exit_window() {
	char c;

	move( 23, 0 );
	printw( "Pusxu klavon por fini... " );
	refresh();
	c = getch();
	endwin();
}

init_board() {
	int i, j;

	for( i = 1; i < 16; i++ )
		for( j = 1; j < 16; j++ )
			board[i][j] = CH_EM;
	board[1][1] = CH_TW;
	board[8][1] = CH_TW;
	board[8][8] = CH_DW;
	for( i = 2; i < 6; i++ ) {
		board[i][i] = CH_DW;
	}
	board[2][6] = CH_TL;
	board[6][2] = CH_TL;
	board[6][6] = CH_TL;
	board[1][4] = CH_DL;
	board[4][1] = CH_DL;
	board[3][7] = CH_DL;
	board[7][3] = CH_DL;
	board[8][4] = CH_DL;
	board[7][7] = CH_DL;
	for( i = 1; i < 9; i++ )
		for( j = 1; j < 8; j++ ) {
			board[16-j][i] = board[i][j];
			board[16-i][16-j] = board[i][j];
			board[j][16-i] = board[i][j];
		}
}

print_info() {
	move( INFO_Y    , INFO_X );
	printw( "Specialaj simboloj:" );
	move( INFO_Y + 1, INFO_X + 2 );
	printw( "%c: Duobla litervaloro", CH_DL );
	move( INFO_Y + 2, INFO_X + 2 );
	printw( "%c: Triobla litervaloro", CH_TL );
	move( INFO_Y + 3, INFO_X + 2 );
	printw( "%c: Duobla vortvaloro", 'o' );
	move( INFO_Y + 4, INFO_X + 2 );
	printw( "%c: Triobla vortvaloro", CH_TW );
	move( INFO_Y + 5, INFO_X + 2 );
	printw( "%c: Senlitero", CH_BL );
	move( INFO_Y + 7, INFO_X );
	printw( "Literaj valoroj kaj nombroj:" );
	move( INFO_Y + 8, INFO_X + 2 );
	printw( "A-1, 8  G-3, 2  K-2, 4  S-1, 6" );
	move( INFO_Y + 9, INFO_X + 2 );
	printw( "B-4, 2  [-3, 2  L-1, 4  X-4, 1" );
	move( INFO_Y +10, INFO_X + 2 );
	printw( "C-4, 1  H-8, 1  M-2, 4  T-1, 4" );
	move( INFO_Y +11, INFO_X + 2 );
	printw( "Q-4, 2  \\10, 1  N-1, 6  U-1, 4" );
	move( INFO_Y +12, INFO_X + 2 );
	printw( "D-2, 3  I-1, 8  O-1, 8  W-8, 1" );
	move( INFO_Y +13, INFO_X + 2 );
	printw( "E-1, 8  J-2, 3  P-2, 3  V-3, 2" );
	move( INFO_Y +14, INFO_X + 2 );
	printw( "F-3, 2  Y10, 1  R-1, 6  Z-5, 1" );
	move( INFO_Y +15, INFO_X + 26);
	printw( "%c-0, 2", CH_BL );
	refresh();
}

init_tiles() {
	int i, j;

	for( i = 0; i < 29; i++ )
		tiles_left[i] = letters[i].tiles;
	for( i = 0; i < 4; i++ )
		for( j = 0; j < 7; j++ )
			plr_tiles[i][j] = 0;
	b1x = 0;
	b2x = 0;
	b1y = 0;
	b2y = 0;
	normal_sum = 0;
	for( i = 0; i < 40; i++	) normal_sum += normal_dist[i];
}

get_num_players() {
	char c;
	int i, j;
	char ft[4], cs[7];

	clear_prompt();
	printw( "  Nombro de komputilaj ludantoj (1-3)? " );
	refresh();
	for( ;; ) {
		c = getch();
		if( c >= '1' && c <= '3' ) break;
	}
	players = c - '0' + 1;
	for( ;; ) {
		init_tiles();
		for( i = 0; i < players; i++ ) ft[i] = draw_tile();
		for( i = 1; i < players; i++ )
			if( ft[0] == ft[i] ) break;
		if( i != players ) continue;
		move( 20, 0 );
		printw( "  Cxiu ludanto prenas ludilon.  Vi: %c", ft[0] );
		for( i = 1; i < players; i++ ) {
			printw( "  Aliulo %d: %c", i + 1, ft[i] );
			if( ft[i] == CH_BL ) ft[i] = 'A' - 1;
		}
		j = players;
		for( i = 1; i < players; i++ )
			if( ft[0] < ft[i] ) j--;
		if( j == 1 ) strcpy( cs, "unue" );
		if( j == 2 ) strcpy( cs, "due" );
		if( j == 3 ) strcpy( cs, "trie" );
		if( j == 4 ) strcpy( cs, "kvare" );
		printw( "\n  Vi ludos %s.", cs );
		human_player = j - 1;
		print_tiles_left( 100 );
		press_return();
		clear_prompt();
		refresh();
		break;
	}

	init_tiles();
	j = '1';
	for( i = 0; i < 4; i++ ) {
		if( i == human_player ) {
			strcpy( &(your[i][0]), "Via" );
			strcpy( &(you[i][0]), "Vi" );
		} else {
			strcpy( &(your[i][0]), "CPU a" );
			strcpy( &(you[i][0]), "CPU " );
			your[i][3] = j;
			you[i][3] = j++;
		}
		plr_skills[i] = 0;
	}

	for( i = 0; i < players; i++ ) {
		print_tiles();
		if( i == human_player ) continue;
		clear_prompt();
		printw( "  Elektu ludkapablon 1-%d por %s, 1 estas komencanto, %d estas sperta, kaj\n", SKILL_EXPERT, you[i], SKILL_LEVELS );
		printw( "  %d estas spertega. -> ", SKILL_EXPERT );
		refresh();
		for( ;; ) {
			c = getch();
			if( c >= '1' && c <= '6' ) break;
		}
		plr_skills[i] = c - '0';
	}

	for( i = 0; i < players; i++ ) {
		plr_scores[i] = 0;
		for( j = 0; j < 7; j++ )
			plr_tiles[i][j] = draw_tile();
	}
	print_tiles();
}

end_game() {
	int i, j;
	int adj[4], wbs, wb[4], was, wa[4];
	long p1;
	FILE *fp;
	char c;

	clear_turn();
	printw( "*** LUDO FINITA\n" );
	print_tiles();
	clear_prompt();
	printw( "  Jen la poentaraj sxangxoj: " );
	wbs = -1;
	for( i = 0; i < players; i++ )
		if( plr_scores[i] > wbs ) wbs = plr_scores[i];
	for( i = 0; i < players; i++ ) {
		if( plr_scores[i] == wbs ) wb[i] = 1;
		else wb[i] = 0;
	}
	for( i = 0; i < players; i++ ) {
		adj[i] = 1;
		for( j = 0; j < 7; j++ ) {
			c = plr_tiles[i][j];
			if( c != 0 ) {
				if( adj[i] == 1 ) adj[i] = 0;
				if( c != CH_BL ) adj[i] -= letters[c - 'A'].points;
			}
		}
	}
	for( j = 0; j < players; j++ )
		if( adj[j] == 1 ) break;
	if( j < players ) {
		adj[j] = 0;
		for( i = 0; i < players; i++ )
			if( i != j ) adj[j] -= adj[i];
	}
	for( i = 0; i < players; i++ ) {
		move( 19 + i, 36 );
		if( i == human_player ) addch( ' ' );
		printw( "%s: %d", you[i], adj[i] );
		plr_scores[i] += adj[i];
	}
	was = -1;
	for( i = 0; i < players; i++ )
		if( plr_scores[i] > was ) was = plr_scores[i];
	for( i = 0; i < players; i++ ) {
		if( plr_scores[i] == was ) wa[i] = 1;
		else wa[i] = 0;
	}
	press_return();
	print_tiles();
	clear_prompt();
	move( 20, 0 );
	was = 0;
	for( i = 0; i < players; i++ ) was += wa[i];
	wbs = 0;
	for( i = 0; i < players; i++ ) wbs += wb[i];
	if( was == 1 || wbs == 1 ) {
		if( was == 1 ) {
			for( i = 0; i < players; i++ )
				if( wa[i] == 1 ) break;
		} else {
			for( i = 0; i < players; i++ )
				if( wb[i] == 1 ) break;
		}
		printw( "  Kaj la gajninto estas:  %s!", you[i] );
	} else {
		printw( "  Estas kungajno inter:  " );
		wbs = 1;
		for( i = 0; i < players; i++ )
			if( wa[i] == 1 ) {
				if( wbs < was - 1 ) printw( ", " );
				else {
					if( was > 2 ) printw( ", kaj " );
					else printw( " kaj " );
				}
				printw( "%s", you[i] );
				wbs++;
			}
		printw( "!" );
	}
	refresh();
	if( dict_changed == 1 ) {
		press_return();
		clear_prompt();
		printw( "  La komputila vortaro sxangxigxis dum la ludo.\n" );
		printw( "  Cxu vi volas memori la novajn vortojn (j/n)? " );
		refresh();
		do {
			c = get_key();
		} while( c != 'J' && c != 'N' );
		if( c == 'J' ) {
			printw( "\n\n  Aldiskigante novan vortaron..." );
			refresh();
			fp = fopen( DICT_FILE, "w+" );
			if( fp == NULL ) {
				printw( "eraro aldiskigante vortaron!\n" );
				exit_window();
				exit( 1 );
			}
			for( p1 = 0; p1 < wlen[16]; p1++ )
				fprintf( fp, "%s\n", &(words[wptr[p1]]) );
			fclose( fp );
		}
	}
}

int main() {
	int plr;
	int i;

	set_up_window();
	clear();
	printw( "*** SKRABLO 1.21-Es (Esperanto) ***\n\n" );

	read_words();
	dict_changed = 0;

	clear();
	printw( "*** SKRABLO 1.21-Es (Esperanto) ***\n\n" );

	init_board();
	seed_random();
	print_board();
	print_info();
	get_num_players();

	game_done = 0;
	abort = 0;
	do {
		for( plr = 0; plr < players; plr++ ) {
			clear_turn();
			printw( "*** %s vico por ludi  ", your[plr] );
			if( plr == human_player ) {
				player_move();
			} else {
				computer_move( plr );
			}
			if( abort != 0 ) break;
			if( game_done == 0 ) clear_rect( 1, 50, 1, 79 );
			else {
				move( 1, 64 );
				printw( "Pasoj:  %d", game_done );
			}
			refresh();
			if( game_done == players ) break;
			for( i = 0; i < 7; i++ )
				if( plr_tiles[plr][i] != 0 ) break;
			if( i == 7 ) {
				game_done = players;
				break;
			}
		}
	} while( game_done < players && abort == 0 );

	if( abort == 0 ) end_game();
	exit_window();
}
