chdir("~/Source");


function ret=rowswap( a, b, M )
  ret=M;
  c1=ceil(a/3);
  c2=ceil(b/3);
  if( c1 != c2 )
    printf("[error]\n");
    return;
  endif  
  ret(a,:)=M(b,:);
  ret(b,:)=M(a,:);
endfunction

function ret=colswap(a, b, M )
  ret=M;
  c1=ceil(a/3);
  c2=ceil(b/3);
  if( c1 != c2 )
    printf("[error]\n");
    return;
  endif
  ret(:,a)=M(:,b);
  ret(:,b)=M(:,a);
endfunction

function ret=swapblockrow(a,b,M);
  ret=M;
  x = a*b;
  switch( x )
    case 2
      % Swap 1 and 2
      for( a = 1:3 )
	b=a+3;
	ret(a,:)=M(b,:);
	ret(b,:)=M(a,:);
      endfor
    case 3
      for( a = 1:3 )
	b=a+6;
	ret(a,:)=M(b,:);
	ret(b,:)=M(a,:);
      endfor
    case 6
      for( a = 4:6 )
	b=a+3;
	ret(a,:)=M(b,:);
	ret(b,:)=M(a,:);
      endfor
    case 1
    case 4
    case 9
  endswitch
endfunction

function ret=swapblockcol(a,b,M);
  ret=M;
  x = a*b;
  switch( x )
    case 2
      % Swap 1 and 2
      for( a = 1:3 )
	b=a+3;
	ret(:,a)=M(:,b);
	ret(:,b)=M(:,a);
      endfor
    case 3
      for( a = 1:3 )
	b=a+6;
	ret(:,a)=M(:,b);
	ret(:,b)=M(:,a);
      endfor
    case 6
      for( a = 4:6 )
	b=a+3;
	ret(:,a)=M(:,b);
	ret(:,b)=M(:,a);
      endfor
    case 1
    case 4
    case 9
  endswitch
endfunction



M="";a="";b="";sw=0;A=0;B=0;
%S=["         ";"         ";"         ";"         ";"         ";"         ";"         ";"         ";"         "];

% Basic Sudoku
S=["..8..5...";"....31...";"9.4......";"....4..82";".1.287.3.";"78..6....";"......6.7";"...32....";"...7..1.."];

S(1,:)=input("Enter Row 1:","s");
S(2,:)=input("Enter Row 2:","s");
S(3,:)=input("Enter Row 3:","s");
S(4,:)=input("Enter Row 4:","s");
S(5,:)=input("Enter Row 5:","s");
S(6,:)=input("Enter Row 6:","s");
S(7,:)=input("Enter Row 7:","s");
S(8,:)=input("Enter Row 8:","s");
S(9,:)=input("Enter Row 9:","s");

cmd=["r11"];
cmd_temp=["r11"];
while (cmd != "end")
  cmd=["r11"];
  cmd_temp=["r11"];
  D=[ "123456789" ; S ];
  E=[ "  ";"1-";"2-";"3-";"4 ";"5 ";"6 ";"7-";"8-";"9-" ];
  F=[ E D ];
  
  disp(F);

  cmd_temp=input("> ","s");

  if( cmd_temp(1) == "e" )
    break;
  endif


  %% R12, R13, R23
  %% R45, R46, R56
  %% R78, R79, R89
  %% C12, C13, C23
  %% C45, C46, C56
  %% C78, C79, C89
 
  if ((cmd_temp(1) == 'R') || (cmd_temp(1) == 'r'))
    sw=0;
    A=eval( cmd_temp(2) );
    B=eval( cmd_temp(3) );
  elseif ((cmd_temp(1) == 'C') || (cmd_temp(1) == 'c'))
    sw=1;
    A=eval( cmd_temp(2) );
    B=eval( cmd_temp(3) );
  elseif ((cmd_temp(1) == 'E') || (cmd_temp(1) == 'e'))
    sw=999;
    A=B=1;
  elseif ((cmd_temp(1) == 'V') || (cmd_temp(1) == 'v'))
    sw=2;
    A=eval( cmd_temp(2) );
    B=eval( cmd_temp(3) );
  elseif ((cmd_temp(1) == 'H') || (cmd_temp(1) == 'h'))
    sw=3;
    A=eval( cmd_temp(2) );
    B=eval( cmd_temp(3) );
  elseif (cmd_temp(1) == '')
    sw=4;
    A=B=1;
  elseif (cmd_temp(1) == '?')
    sw=5;
    A=B=1;
  elseif ((cmd_temp(1) == 'N') || (cmd_temp(1) == 'n'))
    sw=6;
    A=B=1;
  elseif ((cmd_temp(1) == 'D') || (cmd_temp(1) == 'd'))
    sw=7;
    A=B=1;
  endif

  switch( sw )
    case 999
      S
      return
    case 0
      S=rowswap(A,B,S);
    case 1
      S=colswap(A,B,S);
    case 2
      S=swapblockcol( A, B, S );
    case 3
      S=swapblockrow( A, B, S );
    case 4
      disp(S)
    case 5
      printf( "--------------------------------------------------\n" );
      printf( "[R|C]NN Swaps Rows or Columns N and N.\n");
      printf( "[H|V]NN Swaps Block Rows or Block Columns N and N.\n" );
      printf( "[N]ew - Enter a new board.\n");
      printf( "[D]ump the board to a comma delimited board\n");
      printf( "\n" );
      printf( "??? Help\n");
      printf( "--------------------------------------------------\n" );
    case 6
      printf( "=========\n");
      disp(S);
      printf( "=========\n");
      for i=1:9 
	S(i,:)=input( "# ", "s" );
      endfor
      cmd_temp=["   "];
    case 7
      for i=1:9
	for j=1:9
	  printf( "%c",S(i,j) );
	  if j < 9
	    printf(",");
	  else
	    printf("\n")
	  endif
	endfor
      endfor
    otherwise
      cmd_temp=["   "];
      printf("[???]\n");
  endswitch

  cmd=cmd_temp;
  sw=-1;
endwhile
