#include <pspkernel.h>
#include <pspdebug.h>
#include <pspaudiolib.h>
#include <pspaudio.h>
#include <pspdisplay.h>
#include <pspctrl.h>

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

/* 
   This part of the code is more or less identical to the sdktest sample 
*/

/* Define the module info section */
PSP_MODULE_INFO("PSPABCHR", 0, 1, 1);
/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
/* Define printf, just to make typing easier */
#define printf	pspDebugScreenPrintf

/* Exit callback */
int exitCallback(int arg1, int arg2, void *common) {
	sceKernelExitGame();
	return 0;
}

/* Callback thread */
int callbackThread(SceSize args, void *argp) {
	int cbid;

	cbid = sceKernelCreateCallback("Exit Callback", (void*) exitCallback, NULL);
	sceKernelRegisterExitCallback(cbid);
	sceKernelSleepThreadCB();

	return 0;
}

/* Sets up the callback thread and returns its thread id */
int setupCallbacks(void) {
	int thid = 0;

	thid = sceKernelCreateThread("update_thread", callbackThread, 0x11, 0xFA0, 0, 0);
	if (thid >= 0) {
		sceKernelStartThread(thid, 0, 0);
	}
	return thid;
}

/*
  Below this point is the interesting code in this sample
*/

const int SND_SILENCE=-1;
const int SND_OK=-2;
const int SND_NG=-3;
const int SND_PERARI=-4;
const int SND_PI=-5;
const int SND_KATAKATA=-6;
const int SND_COLLISION=-7;

#define ENC_NUM_MAX (9+1)
#define SOUNDS_MAX (30)
#define ABX_CRITERIA_FULL_MAX (30)
#define STRINGS_LMAX (240)

char testname[STRINGS_LMAX];
char encoders_name[ENC_NUM_MAX][STRINGS_LMAX];
char encoders_exte[ENC_NUM_MAX][STRINGS_LMAX];
char sounds[SOUNDS_MAX][STRINGS_LMAX];

int sounds_num = 1;
int enc_num = 1+1;

int play_raw_index_x = 0;
int play_raw_index_y = 1;

int now_testing = -1;
int is_stopping = 0;

int room_select = -1;

int test_candi = 0;
int test_sample = 0;

int test_correct_count = 0;
int test_all_count = 0;
int test_correct_count_total = 0;
int test_all_count_total = 0;

int tcc[ENC_NUM_MAX];
int tac[ENC_NUM_MAX];
int tsp[ENC_NUM_MAX];
int test_align[ENC_NUM_MAX];
int I_think_the_nonref_is[ENC_NUM_MAX];
int the_nonref_is[ENC_NUM_MAX];
time_t test_abx_time[ENC_NUM_MAX][ABX_CRITERIA_FULL_MAX];
int test_abx_errata[ENC_NUM_MAX][ABX_CRITERIA_FULL_MAX];

int abx_criteria_full = 10;
int abx_criteria = 9;

typedef struct {
        short l, r;
} sample_t;

struct raw_common{
 int length;
 char path[STRINGS_LMAX];
};

struct raw_common RBANK[10+ENC_NUM_MAX*SOUNDS_MAX];
struct raw_common *rbank[10+ENC_NUM_MAX*SOUNDS_MAX];
FILE* fp[2];
int fpi = 0;


/* This function gets called by pspaudiolib every time the
   audio buffer needs to be filled. The sample format is
   16-bit, stereo. */
void audioCallback(void* buf, unsigned int length, void *userdata) {
	sample_t* ubuf = (sample_t*) buf;
	int i,fpt;
	int exist_length = 0;
	fpt = fpi;
	if(fp[fpt]!=NULL){
		exist_length = fread(buf, 4, length, fp[fpt]);
		if(exist_length<length){
			fclose(fp[fpt]);
			fp[fpt] = NULL;
		}
	}
	
	for (i = 0;i<(length-exist_length);i++) {
		ubuf[i].l = 0;
		ubuf[i].r = 0;
	}
}

SceKernelUtilsMt19937Context rand_mt_ctx;

u32 rand_mt(u32 num){
	static int rand_mt_init_remain = 32000;
	//return 0, 1, 2,  ...  (num-2), (num-1) using PSP builtin Mersenne twister
	if(rand_mt_init_remain){
		sceKernelUtilsMt19937Init(&rand_mt_ctx, time(NULL));	//initialize
		while(rand_mt_init_remain){
			if(sceKernelUtilsMt19937UInt(&rand_mt_ctx))rand_mt_init_remain--;
		}
	}
	if(num<=2){
		if(num<=1)return 0;
		return sceKernelUtilsMt19937UInt(&rand_mt_ctx)&1;
	}
	u32 range = num;
	u32 ans = 0;
	u32 num_mask = num-1;
	num_mask = (num_mask|num_mask>>1);
	num_mask = (num_mask|num_mask>>2);
	num_mask = (num_mask|num_mask>>4);
	num_mask = (num_mask|num_mask>>8);
	num_mask = (num_mask|num_mask>>16);
	do{
		ans = sceKernelUtilsMt19937UInt(&rand_mt_ctx)&num_mask;
	}while(ans >= range);
	return ans;
}

int init_raw_common(struct raw_common *rawc, char *path){
 strcpy(rawc->path,path);
 fp[fpi] = fopen(rawc->path,"rb");
 if(fp[fpi]==NULL){
  printf("File %s not found.\n",path);
  return 1;
 }
 else{
  fseek(fp[fpi], 0, SEEK_END);
  rawc->length = ftell(fp[fpi])>>2;
  fclose(fp[fpi]);
  return 0;
 }
}

void renew_fp() {
	int new_fpi = 1-fpi;
	int pri_temp = 10+test_sample*ENC_NUM_MAX;
	switch(now_testing){
		case -1:pri_temp = -1;break;
		case 0:pri_temp += 0;break;
		case 1:pri_temp += test_align[test_candi];break;
		case 2:pri_temp += (play_raw_index_x?test_align[test_candi]:0);break;
		case 3:pri_temp += (play_raw_index_y?test_align[test_candi]:0);break;
		default:pri_temp = -now_testing-2;break;
	}
	if(is_stopping)pri_temp = -1;
	
	if(pri_temp>=0){
		if(fp[new_fpi]!=NULL)fclose(fp[new_fpi]);
		fp[new_fpi] = fopen(rbank[pri_temp]->path,"rb");
		if(fp[new_fpi]==NULL)printf("%s Not Found.\n",rbank[pri_temp]->path);
	}
	else{
		if(fp[new_fpi]!=NULL)fclose(fp[new_fpi]);
		fp[new_fpi] = NULL;
	}
	fpi = new_fpi;
}

void init_abchr() {
	int i,j;
	test_align[0] = 0;
	for(i=1;i<enc_num;i++){
		tcc[i] = tac[i] = 0;
		tsp[i] = 50;
		I_think_the_nonref_is[i] = 0;
		the_nonref_is[i] = rand_mt(2);
		test_align[i] = i;
	}
	for(i=enc_num-1;i>1;i--){
		j = rand_mt(i)+1;
		int tmp = test_align[j];
		test_align[j] = test_align[i];
		test_align[i] = tmp;
	}
}

void abchr_save_and_setup() {
	int i,j;
	time_t timer;
	time(&timer);
	FILE* fp2;
	char filename[STRINGS_LMAX];
	int log_number = 0;
	struct stat st;
	do{
		log_number++;
		filename[0] = '\0';
		sprintf(filename,"%s_%s_log%d.txt",testname,sounds[test_sample],log_number);
	}
	while(stat(filename,&st)==0);
	
	fp2 = fopen(filename,"w");
	fprintf(fp2,"PSP abc/hr test v2.4\r\n");
	for(i=1;i<enc_num;i++)fprintf(fp2,"%02d",(I_think_the_nonref_is[i]==the_nonref_is[i]?tsp[i]:50));
	fprintf(fp2,"\r\nFinished time: %s\r\n", ctime(&timer));
	fprintf(fp2,"testname: %s\r\n", testname);
	fprintf(fp2,"tested sound: %s\r\n", sounds[test_sample]);
	fprintf(fp2,"tested encoders: ");
	for(i=1;i<enc_num;i++){
		pspDebugScreenSetXY(i*7+1,29);
		//printf("%s        ",encoders_name[test_align[i]]);
		fprintf(fp2,"%s,",encoders_name[test_align[i]]);
	}
	fprintf(fp2,"\r\n\r\n");
	fprintf(fp2,"5_ 00/00 %s\r\n",encoders_name[i]);

	for(i=1;i<enc_num;i++){
		pspDebugScreenSetXY(i*7+2,28);
		printf("%d",tsp[test_align[i]]);
		fprintf(fp2,"%02d %02d/%02d ",(I_think_the_nonref_is[i]==the_nonref_is[i]?tsp[i]:50),tcc[i],tac[i]);//score 10-50
		fprintf(fp2,"%s  %s\r\n",encoders_name[i],rbank[10+ENC_NUM_MAX*test_sample+i]->path);
	}
	fprintf(fp2,"\r\n\r\n");
	
	for(i=1;i<enc_num;i++){
		fprintf(fp2,"original vs %s\r\n",encoders_name[i]);
		fprintf(fp2,"%d/%d\r\n",tcc[i],tac[i]);
		for(j=0;j<tac[i];j++){
			fprintf(fp2,"%02d ",test_abx_errata[i][j]);
			fprintf(fp2,"%s \r\n",ctime(&test_abx_time[i][j]));
			test_abx_errata[i][j] = -1;
		}
		fprintf(fp2,"\r\n");
	}
	fclose(fp2);
	init_abchr();
}



void abx_save_and_setup() {
	test_all_count_total += test_all_count;
	test_correct_count_total += test_correct_count;
	int past_room_select = room_select;
	if(past_room_select==-1){//sample select room
		now_testing = SND_PERARI;
		room_select = 0;
	}
	if(past_room_select==0){//home
		if(test_candi==0){
			now_testing = SND_PERARI;//quit
			room_select = -1;//sample select room
		}
		else{
			now_testing = SND_PERARI;
			room_select = 1;//training
		}
	}
	
	if(past_room_select==1){//training -> testing
		if(test_candi && tac[test_align[test_candi]]<abx_criteria_full){
			room_select = 2; // testing room
			now_testing = SND_PERARI;
		}
		else{
			if(test_candi){
				room_select = 0; // home
				now_testing = SND_PERARI;
			}
		}
	}
	if(past_room_select==2){//testing room
		now_testing = SND_PERARI;
		room_select = 0; // home
	}
	
	if(room_select==0){
		play_raw_index_y = the_nonref_is[test_align[test_candi]];
		play_raw_index_x = 1-play_raw_index_y;
	}
	else{
		play_raw_index_x = rand_mt(2);
		play_raw_index_y = 1-play_raw_index_x;
	}
	

	test_correct_count = 0;
	test_all_count = 0;

}



void check_up_key() {
    static int oldButtons = 0;
	SceCtrlData pad;
	int changedButtons;
	sceCtrlReadBufferPositive(&pad, 1);
	changedButtons = pad.Buttons & (~oldButtons);
	if (changedButtons & PSP_CTRL_SQUARE) {
		is_stopping = 0;
		now_testing = 0;//play A
		renew_fp();
		oldButtons |= PSP_CTRL_SQUARE;
	}
	else if (changedButtons & PSP_CTRL_TRIANGLE) {
		if(test_candi){
			is_stopping = 0;
			now_testing = 1;//play B
			renew_fp();
		}
		oldButtons |= PSP_CTRL_TRIANGLE;
	}
	else if (changedButtons & PSP_CTRL_CROSS) {
		if(test_candi){
			is_stopping = 0;
			now_testing = 2;//play X
			renew_fp();
		}
		oldButtons |= PSP_CTRL_CROSS;
	}
	else if (changedButtons & PSP_CTRL_CIRCLE) {
		if(test_candi){
			is_stopping = 0;
			now_testing = 3;//play Y
			renew_fp();
		}
		oldButtons |= PSP_CTRL_CIRCLE;
	}
	else if (changedButtons & PSP_CTRL_START) {
		if(test_candi || room_select>=0){
			if(is_stopping){
				is_stopping = 0;
				renew_fp();
			}
			else{
				if(now_testing>=0){
					is_stopping = 1;
					renew_fp();
				}
			}
		}
		else{
			abchr_save_and_setup();
			init_abchr();
			now_testing = SND_KATAKATA;
			renew_fp();
		}
		oldButtons |= PSP_CTRL_START;
	}
	else if (changedButtons & PSP_CTRL_LTRIGGER) {//room change
		if(is_stopping==0){
			abx_save_and_setup();
			renew_fp();
		}
		oldButtons |= PSP_CTRL_LTRIGGER;
	}
	else if ((changedButtons & PSP_CTRL_RTRIGGER)) {
		if(now_testing>=2 && test_candi && is_stopping==0){
			if(room_select>0){// training or testing room
				int test_codec = test_align[test_candi];
				if(room_select==2){ // testing room
					test_abx_errata[test_codec][tac[test_codec]] = (now_testing-2)*2+play_raw_index_y;
					time(&test_abx_time[test_codec][tac[test_codec]]);
				}
				if(now_testing-2==play_raw_index_y){
					if(room_select==1){
						test_correct_count++;
					}
					else{
						tcc[test_codec]++;
					}
					now_testing = SND_OK;
				}
				else{
					now_testing = SND_NG;
				}
				play_raw_index_x = rand_mt(2);
				play_raw_index_y = 1-play_raw_index_x;
				
				if(room_select==1){
					test_all_count++;
				}
				if(room_select==2){
					tac[test_codec]++;
					if(tac[test_codec]>=abx_criteria_full){
						I_think_the_nonref_is[test_codec] = the_nonref_is[test_codec];
						abx_save_and_setup();
						now_testing = SND_PI;
					}
				}
				renew_fp();
			}
		}
		oldButtons |= PSP_CTRL_RTRIGGER;
	}
	else if (changedButtons & PSP_CTRL_UP) {
		if(test_candi==0){
			if(room_select==-1){//sample select
				init_abchr();
				test_sample--;
				if(test_sample<0){
					test_sample = 0;
					now_testing = SND_COLLISION;
				}
				else{
					now_testing = SND_PERARI;
				}
				test_all_count_total = 0;
				test_correct_count_total = 0;
				renew_fp();
			}
			else{
				now_testing = SND_COLLISION;
			}
		}
		else{
			if(room_select==0){ // base
				if(now_testing>=2){
					if(tsp[test_align[test_candi]]<50)tsp[test_align[test_candi]]++;
					I_think_the_nonref_is[test_align[test_candi]] = now_testing-2;
				}
			}
			else if(room_select>0){
				if(tsp[test_align[test_candi]]<50)tsp[test_align[test_candi]]++;
			}
		}
		oldButtons |= PSP_CTRL_UP;
	}
	else if (changedButtons & PSP_CTRL_DOWN) {
		if(test_candi==0){
			if(room_select==-1){
				init_abchr();
				test_sample++;
				if(test_sample>=sounds_num){
					test_sample = sounds_num-1;
					now_testing = SND_COLLISION;
				}
				else{
					now_testing = SND_PERARI;
				}
				test_all_count_total = 0;
				test_correct_count_total = 0;
				renew_fp();
			}
			else{
				now_testing = SND_COLLISION;
			}
		}
		else{
			if(room_select==0){ // base
				if(now_testing>=2){
					if(tsp[test_align[test_candi]]>10)tsp[test_align[test_candi]]--;
					I_think_the_nonref_is[test_align[test_candi]] = now_testing-2;
				}
			}
			else if(room_select>0){
				if(tsp[test_align[test_candi]]>10)tsp[test_align[test_candi]]--;
			}
		}
		oldButtons |= PSP_CTRL_DOWN;
	}
	else if (changedButtons & PSP_CTRL_LEFT) {
		is_stopping = 0;
		if(room_select<=1){//select room or home or training room
			test_candi--;
			if(test_candi<0)test_candi = 0;
			if(room_select==0){
				play_raw_index_y = the_nonref_is[test_align[test_candi]];
				play_raw_index_x = 1-play_raw_index_y;
			}
			else{
				play_raw_index_x = rand_mt(2);
				play_raw_index_y = 1-play_raw_index_x;
			}
			test_all_count = 0;
			test_correct_count = 0;
			now_testing = SND_SILENCE;
		}
		else{//testing room
			now_testing = SND_COLLISION;
		}
		renew_fp();
		oldButtons |= PSP_CTRL_LEFT;
	}
	else if (changedButtons & PSP_CTRL_RIGHT) {
		is_stopping = 0;
		if(room_select<=1){//select room or home or training room
			test_candi++;
			if(test_candi>=enc_num)test_candi = enc_num-1;
			if(room_select==0){
				play_raw_index_y = the_nonref_is[test_align[test_candi]];
				play_raw_index_x = 1-play_raw_index_y;
			}
			else{
				play_raw_index_x = rand_mt(2);
				play_raw_index_y = 1-play_raw_index_x;
			}
			test_all_count = 0;
			test_correct_count = 0;
			now_testing = SND_SILENCE;
		}
		else{//testing room
			now_testing = SND_COLLISION;
		}
		renew_fp();
		oldButtons |= PSP_CTRL_RIGHT;
	}
	else{
		oldButtons = pad.Buttons;
	}

}

void readline(char *s, int n, FILE *fp){
	char *s1;
	if(n<=1){
		s[0] = '\0';
	}
	else{
		s1 = fgets(s,n-1,fp);
		if(s1 == NULL){
			s[0] = '\0';
		}
		else{
			char *p;
			p = strchr(s,'\r');
			if(p!=NULL)p[0] = '\0';
			p = strchr(s,'\n');
			if(p!=NULL)p[0] = '\0';
		}
	}
}
	

int main(void) {
	pspDebugScreenInit();
	setupCallbacks();
	pspDebugScreenSetXY(0,3);
	printf(" PSP ABC/HR TEST v2.4 http://d.hatena.ne.jp/kamedo2/\n\n");
	int i,j;
	for(i=0;i<10+ENC_NUM_MAX*SOUNDS_MAX;i++){
		rbank[i] = &RBANK[i];
	}
	init_raw_common(rbank[0],"syssound/ok.raw");
	init_raw_common(rbank[1],"syssound/ng.raw");
	init_raw_common(rbank[2],"syssound/perari.raw");
	init_raw_common(rbank[3],"syssound/pi.raw");
	init_raw_common(rbank[4],"syssound/katakata.raw");
	init_raw_common(rbank[5],"syssound/collision.raw");
	
	FILE* fp3;
	fp3 = fopen("config.txt","r");
	char buf[STRINGS_LMAX];
	if(fp3==NULL)printf("config.txt not found.\n");
	readline( buf, STRINGS_LMAX, fp3 );
	if(strncmp(buf,"[testname]",9))printf(" config.txt testname error. %s \n",buf);
	readline( testname, STRINGS_LMAX, fp3 );
	printf("  testname = %s \n",testname);
	readline( buf, STRINGS_LMAX, fp3 );
	if(strncmp(buf,"[criteria]",9))printf(" config.txt criteria error. %s \n",buf);
	readline( buf, STRINGS_LMAX, fp3 );	
	char *p;
	p = strchr(buf,'/');
	if(p!=NULL && (p-buf)>0 && (p-buf)<9){
		char nn[9];
		strncpy(nn,buf,p-buf);
		abx_criteria = atoi(nn);
		strncpy(nn,p+1,9);
		abx_criteria_full = atoi(nn);
		if(abx_criteria_full>ABX_CRITERIA_FULL_MAX)abx_criteria_full = ABX_CRITERIA_FULL_MAX;
		printf("  criteria %d / %d \n",abx_criteria,abx_criteria_full);
	}
	else{
		printf(" xx/yy was not found. %s \n",buf);
	}
	readline( buf, STRINGS_LMAX, fp3 );
	if(strncmp(buf,"[encoders]",9))printf(" config.txt encoders error. %s \n",buf);
	i = 0;
	while(1){
		readline( buf, STRINGS_LMAX, fp3 );
		if(buf[0]=='[')break;
		p = strchr(buf,'=');
		if(i>=ENC_NUM_MAX){
			printf(" config.txt too many encoders.\n");
			break;
		}
		if(p!=NULL && (p-buf)>0 && (p-buf)<STRINGS_LMAX){
			strncpy(encoders_name[i],buf,p-buf);
			strncpy(encoders_exte[i],p+1,STRINGS_LMAX);
			//printf(" name=%s exte=%s \n",encoders_name[i],encoders_exte[i]);
			i++;
		}
		else{
			printf(" =equal was not found. %s \n",buf);
		}

	}
	if(strncmp(encoders_name[0],"or",2))printf("The first sample must be original.\n");
	enc_num = i;

	if(strncmp(buf,"[sounds]",6))printf(" config.txt error. %s \n",buf);
	i = 0;
	while(1){
		readline( buf, STRINGS_LMAX, fp3);
		if(buf[0]=='[')break;
		if(buf[0]=='\0')break;
		if(i>=SOUNDS_MAX){
			printf(" config.txt too many sounds.\n");
			break;
		}
		strncpy(sounds[i],buf,STRINGS_LMAX);
		//printf(" sounds=%s \n",sounds[i]);
		i++;
	}
	fclose(fp3);
	sounds_num = i;
	printf("  %d encoders %d sounds \n",enc_num-1,sounds_num);
	init_abchr();
	
	for(i=0;i<sounds_num;i++){
		for(j=0;j<enc_num;j++){
			char thepath[STRINGS_LMAX] = "";
			sprintf(thepath, "%s%s.raw", sounds[i], encoders_exte[j]);
			init_raw_common(rbank[10+i*ENC_NUM_MAX+j],thepath);
			//printf(" thepath %s \n",thepath);
		}
	}
	
	pspAudioInit();
	pspAudioSetChannelCallback(0, audioCallback, NULL);
	sceCtrlSetSamplingCycle(0);
	sceCtrlSetSamplingMode(PSP_CTRL_MODE_DIGITAL);

	while(1) {
		sceDisplayWaitVblankStart();
		check_up_key();
		pspDebugScreenSetXY(0,0);
		if(room_select==-1)printf("sample select room");
		if(room_select==0)printf("home              ");
		if(room_select==1)printf("training room     ");
		if(room_select==2)printf("testing room      ");
		pspDebugScreenSetXY(44,9);
		if(now_testing==0)printf("Playing A(ref) []");
		if(now_testing==1)printf("Playing B      /\\");
		if(now_testing==2)printf("Playing X      X ");
		if(now_testing==3)printf("Playing Y      O ");
		if(now_testing<0)printf("                 ");
		pspDebugScreenSetXY(2,31);
		printf("Ltrig:");
		if(is_stopping==0){
			if(room_select==-1){
					printf("go home     ");
			}
			else if(room_select==0){
				if(test_candi){
					printf("go training ");
				}
				else{
					printf("samp select ");
				}
			}
			else if(room_select==1){
				if(test_candi && tac[test_align[test_candi]]<abx_criteria_full){
					printf("go testing  ");
				}
				else{
					if(test_candi){
						printf("back home   ");
					}
					else{
						printf("            ");
					}
				}
			}
			else if(room_select==2){
				printf("back home   ");
			}
			else{
				printf("UNEXPECTED  ");
			}
		}
		else{
			printf("            ");
		}
		printf("Up:");
		if(test_candi==0){
			if(test_sample>=1 && room_select==-1){
				printf("back sample ");
			}
			else{
				printf("            ");
			}
		}
		else{
			if(room_select>=0 && (room_select>0 || now_testing>=2) && tsp[test_align[test_candi]]<50){
				printf("more score  ");
			}
			else{
				printf("            ");
			}
		}
		printf("Rtrig:");
		if(now_testing>=2 && test_candi && room_select>0 && is_stopping==0){
			if(now_testing==2)printf(" X is B   ");
			if(now_testing==3)printf(" Y is B   ");
		}
		else{
			printf("           ");
		}

		printf("Start:");
		if(test_candi){
			if(is_stopping){
				printf("restart  ");
			}
			else{
				if(now_testing>=0){
					printf("pause    ");
				}
				else{
					printf("         ");
				}
			}
		}
		else{
			if(room_select==-1){
				printf("save log ");
			}
			else{
				printf("         ");
			}
		}
		int now_seek = 0;
		if(fp[fpi]!=NULL)now_seek = ftell(fp[fpi])/(4*4410);
		pspDebugScreenSetXY(0,12);
		//printf("               fpi =   %d\n", fpi);
		//printf("    play_raw_index = %3d\n", play_raw_index[fpi]);
		printf("              play =%4d.%1d\n\n",now_seek/10,now_seek%10);
		//printf("       now_testing =  %2d\n", now_testing);
		printf("       test_sample =  %s\n\n", sounds[test_sample]);

		printf("  training_counter =  %2d/%2d\n\n\n", test_correct_count,test_all_count);
		for(j=0;j<enc_num;j++){
			if(j==test_candi){
				printf(" *");
			}
			else{
				printf("  ");
			}
			if(j==0){
				printf(" ref ");
			}
			else{
				printf("%2d/%2d", tcc[test_align[j]], tac[test_align[j]]);
			}
		}
		printf("\n\n");
		if(room_select==0){//base room
			for(j=0;j<enc_num;j++){
				if(j==test_candi && now_testing==3){
					printf("    *");
				}
				else{
					printf("     ");
				}
				if(j){
					printf("%2d", (I_think_the_nonref_is[test_align[j]]?tsp[test_align[j]]:50));
				}
				else{
					printf("50");
				}
			}
			printf("\n\n");
			for(j=0;j<enc_num;j++){
				if(j==test_candi && now_testing==2){
					printf(" *");
				}
				else{
					printf("  ");
				}
				if(j){
					printf("%2d   ", (I_think_the_nonref_is[test_align[j]]?50:tsp[test_align[j]]));
				}
				else{
					printf("     ");
				}
			}
			
		}
		else{//training room, test room
			for(j=0;j<enc_num;j++){
				if(j==test_candi){
					printf(" *");
				}
				else{
					printf("  ");
				}
				if(j){
					printf("%2d   ", tsp[test_align[j]]);
				}
				else{
					printf("50.  ");
				}
			}
			printf("\n\n");
			for(j=0;j<enc_num;j++){
				printf("       ");
			}
		}

	}
	
	return 0;
}
