// nmC̓	eXgXNvg
// Vǉ\̎gp@Ȃƍ̂ō܂B

prontera.gat,160,180,1	script	nmC̓ē	112,{
	// ֐錾
	function hanoi_init;		// ~Ղ̏
	function hanoi_move;		// ~Ղ̈ړ
	function hanoi_display;		// ~Ղ̕\
	function hanoi_display_sub;
	function hanoi_check;		// ~Ղ̏ԂǂmF
	function hanoi_solve;		// nmC̓
	function hanoi_name;		// _̖Oϊ

	// ~Ղ̖
	// ƑςȎɂȂ̂ŒӁI
	set @level,4;

	switch(select("nmC̓ɂ","Vł݂","𓚂","ς߂")) {
	case 1:
		// ɂȕ⊮肢܂B
		mes "[nmC̓ē]";
		mes "nmC̓Ƃ́A" + @level + "̈Ⴄ~ՂA";
		mes "ʂ̏ꏊɈړQ[łB";
		mes "~Ղ̑傫͐ŕ\Ă܂B";
		next;
		mes "[nmC̓ē]";
		mes "[͎̂QłB";
		mes "Exɓ͈̂ꖇ";
		mes "E~Ղ̏ɑ傫~Ղ͏d˂Ȃ";
		next;
		mes "[nmC̓ē]";
		mes "҂Ă܂B";
		close;
		break;
	case 2:
		// ~Ղ
		hanoi_init;
		do {
			mes "[nmC̓ē]";
			mes "܂ł" + @count + "~Ղړ܂B";
			hanoi_display;
			next;
			set @pos_a , select("~Ղ`","~Ղa","~Ղb","ς߂");
			if(@pos_a == 4) { close; end; }
			switch(@pos_a) {
				case 1: set @move_count , @A_count; break;
				case 2: set @move_count , @B_count; break;
				case 3: set @move_count , @C_count; break;
			}
			if(@move_count == 0) {
				mes "[nmC̓ē]";
				mes "̏ꏊɉ~Ղ͂܂B";
				next;
				continue;
			}
			set @pos_b , select("~Ղ`ֈړ","~Ղaֈړ","~Ղbֈړ","ς߂");
			if(@pos_a == 4) { close; end; }
			if(hanoi_move(@pos_a,@pos_b) == 0) {
				// ړs\
				mes "[nmC̓ē]";
				mes "ړł܂B";
				next;
				continue;
			}
			set @count,@count + 1;
			if(hanoi_check()) {
				// ̂J
				mes "[nmC̓ē]";
				mes "AłB(" + @count + "̉~Ոړ)";
				mes "܋Ƃ100Zenyグ܂B";
				set Zeny,Zeny + 100;
				break;
			}
		} while(1);
		close;
		break;
	case 3:
		hanoi_init();
		mes "[nmC̓ē]";
		mes "ł́An߂܂B";
		hanoi_display();
		next;
		hanoi_solve @level,@A_data,@C_data,@B_data,1,3,2;
		mes "[nmC̓ē]";
		mes @count + "̈ړŒZłˁB";
		close;
	}
	// 
	// hanoi_solve(lv,from_array,to_array,temp_array,from_name,to_name,temp_name)
	function hanoi_solve {
		if(getarg(0) == 1) {
			// hanoi_move ͒lԂ֐Ȃ̂ŁAl̂ĂKv
			set @dummy , hanoi_move(getarg(4),getarg(5));

			// ̎菇\
			set @count , @count + 1;
			mes "[nmC̓ē]";
			mes "" + @count + " : " + hanoi_name(getarg(4)) + "̉~Ղ" + hanoi_name(getarg(5)) + "Ɉړ";
			hanoi_display();
			next;
		} else {
			hanoi_solve getarg(0) - 1,getarg(1),getarg(3),getarg(2),getarg(4),getarg(6),getarg(5);

			// hanoi_move ͒lԂ֐Ȃ̂ŁAl̂ĂKv
			set @dummy , hanoi_move(getarg(4),getarg(5));

			// ̎菇\
			set @count , @count + 1;
			mes "[nmC̓ē]";
			mes "" + @count + " : " + hanoi_name(getarg(4)) + "̉~Ղ" + hanoi_name(getarg(5)) + "Ɉړ";
			hanoi_display();
			next;

			hanoi_solve getarg(0) - 1,getarg(3),getarg(2),getarg(1),getarg(6),getarg(5),getarg(4);
		}
	}
	function hanoi_name {
		switch(getarg(0)) {
		case 1: return "A";
		case 2: return "B";
		case 3: return "C";
		}
		return "??";
	}
	// 
	function hanoi_init {
		cleararray @A_data,0,@level;
		cleararray @B_data,0,@level;
		cleararray @C_data,0,@level;
		set @A_data[0] , @level + 1;
		set @B_data[0] , @level + 1;
		set @C_data[0] , @level + 1;
		set @A_count , 0;
		set @B_count , 0;
		set @C_count , 0;
		for(set @i,@level ; @i > 0 ; set @i , @i - 1) {
			set @A_data[@A_count + 1] , @i;
			set @A_count , @A_count + 1;
		}
		set @count , 0;
	}
	// e\
	function hanoi_display {
		mes "A: " + hanoi_display_sub(@A_data,@A_count);
		mes "B: " + hanoi_display_sub(@B_data,@B_count);
		mes "C: " + hanoi_display_sub(@C_data,@C_count);
	}
	function hanoi_display_sub {
		set @__ret$ , "";
		set @__max  , getarg(1);
		for(set @i , 1;@i <= @__max; set @i , @i + 1) {
			// zRs[鏈d邩b菈
			set @__ret$ , @__ret$ + getelementofarray(getarg(0),@i);
		}
		return @__ret$;
	}
	// ~Ղړ
	function hanoi_move {
		set @__pos_a , getarg(0);
		set @__pos_b , getarg(1);
		// ܂ŏɈړł邩`FbN
		if(@__pos_a == @__pos_b) {
			// ~ՂȂ̂ňړs
			return 0;
		}
		switch(@__pos_a) {
			case 1: set @__move_val_1,@A_data[@A_count]; break;
			case 2: set @__move_val_1,@B_data[@B_count]; break;
			case 3: set @__move_val_1,@C_data[@C_count]; break;
		}
		switch(@__pos_b) {
			case 1: set @__move_val_2,@A_data[@A_count]; break;
			case 2: set @__move_val_2,@B_data[@B_count]; break;
			case 3: set @__move_val_2,@C_data[@C_count]; break;
		}
		if(@__move_val_2 < @__move_val_1) {
			// ړs\i~Ղ̏ɑ傫~Ղd˂悤Ƃj
			return 0;
		}
		// ۂɈړ
		switch(@__pos_a) {
			case 1: set @A_data[@A_count],0; set @A_count,@A_count-1; break;
			case 2: set @B_data[@B_count],0; set @B_count,@B_count-1; break;
			case 3: set @C_data[@C_count],0; set @C_count,@C_count-1; break;
		}
		switch(@__pos_b) {
			case 1: set @A_count,@A_count+1; set @A_data[@A_count],@__move_val_1; break;
			case 2: set @B_count,@B_count+1; set @B_data[@B_count],@__move_val_1; break;
			case 3: set @C_count,@C_count+1; set @C_data[@C_count],@__move_val_1; break;
		}
		// ړ\
		return 1;
	}
	// Ă邩m߂
	function hanoi_check {
		for(set @i , 1; @i <= @level; set @i,@i + 1) {
			if(@B_data[@i] != @level - @i + 1) break;
		}
		if(@i == @level + 1) return 1;
		for(set @i , 1; @i <= @level; set @i,@i + 1) {
			if(@C_data[@i] != @level - @i + 1) break;
		}
		if(@i == @level + 1) return 1;
		return 0;
	}
}
