'program TEA2MOVE.TEA
'People need computer software that actually works.
vari dg_pass1, dg_pass2, sg_pass1, sg_pass2
vari sg_board0, sg_board1, sg_board2, sg_board3, sg_board4, sg_board5
vari sg_tobywhite, sg_tobyblack
vari sg_menwhite, sg_menblack
vari sg_abcdefgh, sg_12345678
vari sg_chkboard, dg_wincheck, dg_bincheck

vari sg_moveman1, sg_moveman2, sg_moveman3, sg_moveman4
vari dg_movefrom1, dg_movefrom2, dg_movefrom3, dg_movefrom4
vari dg_moveto1, dg_moveto2, dg_moveto3, dg_moveto4
vari sg_movefrom1, sg_movefrom2, sg_movefrom3, sg_movefrom4
vari sg_moveto1, sg_moveto2, sg_moveto3, sg_moveto4
vari dg_movefromhold2, dg_movetohold2, sg_movemanhold2

vari dg_value0, dg_value1, dg_value2, dg_value3, dg_value4
vari dg_movecount1, dg_movecount2
vari dg_totalmovecount3, dg_movecount3, sg_solution, sg_score
vari dg_totalmatescount, dg_matescount

vari sg_movemanm, dg_movefromm, dg_movetom, sg_movefromm, sg_movetom
vari dg_new, sg_boardm
vari dg_binmate

sub_process
endp

subr sub_process
'updated 2008/02/25, 2000/05/20
    vari s_any, d_any, s_dot, d_dot
    vari d_seconds, d_loop

    $ch$ s_dot, "-", 60
    $out s_dot
    $out "Program: tea2move.tea, build 4, 2009/10/25"
    $out "This program was begun on 2000/04/03"
    $out "Copyright (c) 2000-2009 by D La Pierre Ballard"
    $out "This program was written in the language Teapro"
    $out "Copyright (c) 1997-2009 by D La Pierre Ballard"
    $out "People need computer software that actually works."
    $out s_dot

    d_loop = 1
    dwhi d_loop = 1
	  sub_speedquick
	  $out "speed=" + dg_pass1

        $inp s_any, "1 = old way, 2 = new way"
        dg_new = 1
        $if1 s_any = "1": dg_new = 2
    
        sub_initialize

        sg_pass1 = sg_board0
        sub_board

        dsec d_seconds

        sg_board1 = sg_board0
        sub_move1

        dsec d_any
        d_seconds = d_any - d_seconds

	  s_any = ", New"
	  dif1 dg_new <> 1: s_any = ", Old"

        $out sg_solution + s_any
        $inp s_any, "return, seconds = " + d_seconds

	  $inp s_any, "1 = do another"
	  $if1 s_any <> "1": dinc d_loop
    endw
ends sub_process


subr sub_initialize
'updated 2000/05/20
    vari s_any, d_any
    vari s_pick

    $out "1 = simplest"
    $out "2 = Morphy's problem"
    $out "3 = Alain C. White, A7"
    $out "4 = A. Hesselgren, A6"
    $out "5 = A. Kempe, 1855, A1"
    $out "6 = Lev Loshinski, 1933"
    $out "7 = Comins Mansfield, 1953"
    $inp s_pick, "Enter a number: 1 to 7"

    'the board starts with 110 spaces
    dch$ sg_board0, 32, 110

    $ift s_pick = "0"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "1"
        sg_board0 = sg_board0 + "----K---  "
        sg_board0 = sg_board0 + "-------k  "
        sg_board0 = sg_board0 + "------R-  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "2"
        sg_board0 = sg_board0 + "R-----pk  "
        sg_board0 = sg_board0 + "-----Ppb  "
        sg_board0 = sg_board0 + "-------K  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "3"
	  $out "by Alain C. White, A7"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "q-B-----  "
        sg_board0 = sg_board0 + "bP------  "
        sg_board0 = sg_board0 + "kB-----R  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "---N-Q--  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "-------K  "
    endi
    $ift s_pick = "4"
	  $out "A. Hesselgren, A6"
        sg_board0 = sg_board0 + "---R---B  "
        sg_board0 = sg_board0 + "---b----  "
        sg_board0 = sg_board0 + "-PPbpn--  "
        sg_board0 = sg_board0 + "----n-N-  "
        sg_board0 = sg_board0 + "Q-pkrr-R  "
        sg_board0 = sg_board0 + "-Pp---N-  "
        sg_board0 = sg_board0 + "--KP----  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "5"
	  $out "A. Kempe, A1, 1855"
        sg_board0 = sg_board0 + "----B---  "
        sg_board0 = sg_board0 + "---K----  "
        sg_board0 = sg_board0 + "-B----N-  "
        sg_board0 = sg_board0 + "---krr--  "
        sg_board0 = sg_board0 + "--R--p--  "
        sg_board0 = sg_board0 + "-P---N-R  "
        sg_board0 = sg_board0 + "--n-----  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "6"
	  $out "Lev Loshinski, 1933"
        sg_board0 = sg_board0 + "--p-----  "
        sg_board0 = sg_board0 + "-N-R-K--  "
        sg_board0 = sg_board0 + "Q-----B-  "
        sg_board0 = sg_board0 + "-r-N----  "
        sg_board0 = sg_board0 + "b-q-PP--  "
        sg_board0 = sg_board0 + "RP-k----  "
        sg_board0 = sg_board0 + "-P-ppB--  "
        sg_board0 = sg_board0 + "--r-----  "
    endi
    $ift s_pick = "7"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "K---Pp--  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "----R-r-  "
        sg_board0 = sg_board0 + "-Q-NNk--  "
        sg_board0 = sg_board0 + "--Bpr-R-  "
        sg_board0 = sg_board0 + "------P-  "
        sg_board0 = sg_board0 + "------P-  "
    endi

    'the board ends with 100 spaces
    dch$ s_any, 32, 100
    sg_board0 = sg_board0 + s_any

    dtoi 81, 1
    dtoi 82, -1
    dtoi 83, 10
    dtoi 84, -10

    dtoi 85, 11
    dtoi 86, -11
    dtoi 87, 9
    dtoi 88, -9

    dtoi 91, 12
    dtoi 92, -12
    dtoi 93, 21
    dtoi 94, -21
    dtoi 95, 19
    dtoi 96, -19
    dtoi 97, 8
    dtoi 98, -8       

    sg_abcdefgh = "abcdefgh"
    sg_12345678 = "12345678"

    sg_menwhite = "PNBRQK"
    sg_menblack = "pnbrqk"

    sg_tobyblack = "-PNBRQK"
    sg_tobywhite = "-pnbrqk"

    dg_movecount1 = 0
    dg_totalmovecount3 = 0
    dg_totalmatescount = 0
    sg_solution = ""
    sg_score = ""
ends sub_initialize


subr sub_board
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot
    vari s_rank, d_rank, d_file, s_left, s_dashes
    vari s_board

    s_board = sg_pass1
    $ch$ s_dashes, "-", 50
    dch$ s_left, 32, 10

    $out s_dashes

    'from White's side
    d_rank = 8
    dwhi d_rank >= 1

        s_rank = s_left
	  d_file = 1
	  dwhi d_file <= 8

		d_dot = d_file * 10 + d_rank + 100
		$cut s_dot, s_board, d_dot, 1
		s_rank = s_rank + s_dot + " "

		dinc d_file
	  endw

        $out s_rank
        d_rank = d_rank - 1
    endw 
    $out s_dashes

    $inp s_any, "return"
ends sub_board


subr sub_move1
'updated 2000/05/20
    'board is sg_board1
    vari d_any, s_any, d_dot, s_dot

    dg_movefrom1 = 111
    dwhi dg_movefrom1 <= 188
	  $cut sg_moveman1, sg_board1, dg_movefrom1, 1
	  $lok d_any, sg_menwhite, 1, sg_moveman1
	  dift d_any > 0
		$if1 sg_moveman1 = "P": sub_pawn1
		$if1 sg_moveman1 = "N": sub_knight1
		$if1 sg_moveman1 = "B": sub_bishop1
		$if1 sg_moveman1 = "R": sub_rook1
		$if1 sg_moveman1 = "Q": sub_queen1
		$if1 sg_moveman1 = "K": sub_king1
	  endi

	  dinc dg_movefrom1
    endw
    s_any = "total 3-ply positions = " + dg_totalmovecount3
    s_any = s_any + ", total mates = " + dg_totalmatescount
    $out s_any
ends sub_move1


subr sub_pawn1
'updated 2000/05/20
    'board is in sg_board1, dg_movefrom1
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_torank

    'one move forward
    dg_moveto1 = dg_movefrom1 + 1
    d_torank = dg_moveto1 % 10
    $cut sg_moveto1, sg_board1, dg_moveto1, 1

    $ift sg_moveto1 = "-"
	  dift d_torank = 8
	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "N"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "B"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "R"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "Q"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1
	  else
	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "P"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1
	  endi

	  'two moves forward
	  d_any = dg_movefrom1 % 10

	  dift d_any = 2
            dg_moveto1 = dg_movefrom1 + 2
            $cut sg_moveto1, sg_board1, dg_moveto1, 1

            $ift sg_moveto1 = "-"
	          sg_board2 = sg_board1
	          $rep sg_board2, dg_moveto1, "P"
	          $rep sg_board2, dg_movefrom1, "-"
	          sub_test_move1
		endi
	  endi
    endi

    'capture 44 to 35
    dg_moveto1 = dg_movefrom1 - 9
    $cut sg_moveto1, sg_board1, dg_moveto1, 1
    $lok d_any, sg_menblack, 1, sg_moveto1

    dift d_any > 0
	  dift d_torank = 8
	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "N"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "B"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "R"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "Q"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1
	  else
	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "P"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1
	  endi
    endi

    'capture 44 to 55
    dg_moveto1 = dg_movefrom1 + 11
    $cut sg_moveto1, sg_board1, dg_moveto1, 1
    $lok d_any, sg_menblack, 1, sg_moveto1

    dift d_any > 0
	  dift d_torank = 8
	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "N"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "B"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "R"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1

	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "Q"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1
	  else
	      sg_board2 = sg_board1
	      $rep sg_board2, dg_moveto1, "P"
	      $rep sg_board2, dg_movefrom1, "-"
	      sub_test_move1
	  endi
    endi
ends sub_pawn1


subr sub_knight1
'updated 2000/05/20
    'board is in sg_board1, dg_movefrom1
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  dg_moveto1 = dg_movefrom1 + d_delta

	  $cut sg_moveto1, sg_board1, dg_moveto1, 1
	  $lok d_any, sg_tobywhite, 1, sg_moveto1

	  dift d_any > 0
		sg_board2 = sg_board1
		$rep sg_board2, dg_moveto1, "N"
		$rep sg_board2, dg_movefrom1, "-"

		sub_test_move1
	  endi

	  dinc d_index
    endw
ends sub_knight1


subr sub_bishop1
'updated 2000/05/20
    'board is in sg_board1, dg_movefrom1
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto1 = dg_movefrom1

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto1 = dg_moveto1 + d_delta

	      $cut sg_moveto1, sg_board1, dg_moveto1, 1
	      $lok d_any, sg_tobywhite, 1, sg_moveto1

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board2 = sg_board1
		    $rep sg_board2, dg_moveto1, "B"
		    $rep sg_board2, dg_movefrom1, "-"

		    sub_test_move1
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bishop1


subr sub_rook1
'updated 2000/05/20
    'board is in sg_board1, dg_movefrom1
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  dg_moveto1 = dg_movefrom1

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto1 = dg_moveto1 + d_delta

	      $cut sg_moveto1, sg_board1, dg_moveto1, 1
	      $lok d_any, sg_tobywhite, 1, sg_moveto1

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board2 = sg_board1
		    $rep sg_board2, dg_moveto1, "R"
		    $rep sg_board2, dg_movefrom1, "-"

		    sub_test_move1
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_rook1


subr sub_queen1
'updated 2000/05/20
    'board is in sg_board1, dg_movefrom1
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto1 = dg_movefrom1

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto1 = dg_moveto1 + d_delta

	      $cut sg_moveto1, sg_board1, dg_moveto1, 1
	      $lok d_any, sg_tobywhite, 1, sg_moveto1

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board2 = sg_board1
		    $rep sg_board2, dg_moveto1, "Q"
		    $rep sg_board2, dg_movefrom1, "-"

		    sub_test_move1
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_queen1


subr sub_king1
'updated 2000/05/20
    'board is in sg_board1, dg_movefrom1
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto1 = dg_movefrom1 + d_delta

	  $cut sg_moveto1, sg_board1, dg_moveto1, 1
	  $lok d_any, sg_tobywhite, 1, sg_moveto1

	  dift d_any > 0
		sg_board2 = sg_board1
		$rep sg_board2, dg_moveto1, "K"
		$rep sg_board2, dg_movefrom1, "-"

		sub_test_move1
	  endi

	  dinc d_index
    endw
ends sub_king1


subr sub_test_move1
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot

    sg_chkboard = sg_board2
    sub_wincheck

    dift dg_wincheck <> 1
	  dinc dg_movecount1

	  dg_movecount2 = 0
	  dg_movecount3 = 0
	  dg_matescount = 0

	  'value = 1 is good value = 2 is bad
	  dg_value2 = 2

	  sub_move2

	  sub_show_score1

	  dift dg_value2 = 2
		$out "wins"
		sg_solution = sg_score
	  endi
	  dg_totalmovecount3 = dg_totalmovecount3 + dg_movecount3
	  dg_totalmatescount = dg_totalmatescount + dg_matescount
    endi
ends sub_test_move1


subr sub_move2
'updated 2000/05/20
    'board is sg_board2
    vari d_any, s_any, d_dot, s_dot

    dg_movefrom2 = 111
    dwhi dg_movefrom2 <= 188
	  $cut sg_moveman2, sg_board2, dg_movefrom2, 1
	  $lok d_any, sg_menblack, 1, sg_moveman2
	  dift d_any > 0
		$if1 sg_moveman2 = "p": sub_pawn2
		$if1 sg_moveman2 = "n": sub_knight2
		$if1 sg_moveman2 = "b": sub_bishop2
		$if1 sg_moveman2 = "r": sub_rook2
		$if1 sg_moveman2 = "q": sub_queen2
		$if1 sg_moveman2 = "k": sub_king2
	  endi

	  dinc dg_movefrom2
    endw
ends sub_move2


subr sub_pawn2
'updated 2000/05/20
    'board is in sg_board2, dg_movefrom2
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_torank

    'one move forward
    dg_moveto2 = dg_movefrom2 - 1
    d_torank = dg_moveto2 % 10
    $cut sg_moveto2, sg_board2, dg_moveto2, 1

    $ift sg_moveto2 = "-"
	  dift d_torank = 1
	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "n"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "b"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "r"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "q"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2
	  else
	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "p"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2
	  endi

	  'two moves forward
	  d_any = dg_movefrom2 % 10

	  dift d_any = 7
            dg_moveto2 = dg_movefrom2 - 2
            $cut sg_moveto2, sg_board2, dg_moveto2, 1

            $ift sg_moveto2 = "-"
	          sg_board3 = sg_board2
	          $rep sg_board3, dg_moveto2, "p"
	          $rep sg_board3, dg_movefrom2, "-"
	          sub_test_move2
		endi
	  endi
    endi

    'capture 45 to 34
    dg_moveto2 = dg_movefrom2 - 11
    $cut sg_moveto2, sg_board2, dg_moveto2, 1
    $lok d_any, sg_menwhite, 1, sg_moveto2

    dift d_any > 0
	  dift d_torank = 1
	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "n"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "b"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "r"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "q"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2
	  else
	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "p"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2
	  endi
    endi

    'capture 45 to 54
    dg_moveto2 = dg_movefrom2 + 9
    $cut sg_moveto2, sg_board2, dg_moveto2, 1
    $lok d_any, sg_menwhite, 1, sg_moveto2

    dift d_any > 0
	  dift d_torank = 1
	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "n"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "b"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "r"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2

	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "q"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2
	  else
	      sg_board3 = sg_board2
	      $rep sg_board3, dg_moveto2, "p"
	      $rep sg_board3, dg_movefrom2, "-"
	      sub_test_move2
	  endi
    endi
ends sub_pawn2


subr sub_knight2
'updated 2000/05/20
    'board is in sg_board2, dg_movefrom2
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  dg_moveto2 = dg_movefrom2 + d_delta

	  $cut sg_moveto2, sg_board2, dg_moveto2, 1
	  $lok d_any, sg_tobyblack, 1, sg_moveto2

	  dift d_any > 0
		sg_board3 = sg_board2
		$rep sg_board3, dg_moveto2, "n"
		$rep sg_board3, dg_movefrom2, "-"

		sub_test_move2
	  endi

	  dinc d_index
    endw
ends sub_knight2


subr sub_bishop2
'updated 2000/05/20
    'board is in sg_board2, dg_movefrom2
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto2 = dg_movefrom2

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto2 = dg_moveto2 + d_delta

	      $cut sg_moveto2, sg_board2, dg_moveto2, 1
	      $lok d_any, sg_tobyblack, 1, sg_moveto2

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board3 = sg_board2
		    $rep sg_board3, dg_moveto2, "b"
		    $rep sg_board3, dg_movefrom2, "-"

		    sub_test_move2
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bishop2


subr sub_rook2
'updated 2000/05/20
    'board is in sg_board2, dg_movefrom2
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  dg_moveto2 = dg_movefrom2

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto2 = dg_moveto2 + d_delta

	      $cut sg_moveto2, sg_board2, dg_moveto2, 1
	      $lok d_any, sg_tobyblack, 1, sg_moveto2

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board3 = sg_board2
		    $rep sg_board3, dg_moveto2, "r"
		    $rep sg_board3, dg_movefrom2, "-"

		    sub_test_move2
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_rook2


subr sub_queen2
'updated 2000/05/20
    'board is in sg_board2, dg_movefrom2
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto2 = dg_movefrom2

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto2 = dg_moveto2 + d_delta

	      $cut sg_moveto2, sg_board2, dg_moveto2, 1
	      $lok d_any, sg_tobyblack, 1, sg_moveto2

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board3 = sg_board2
		    $rep sg_board3, dg_moveto2, "q"
		    $rep sg_board3, dg_movefrom2, "-"

		    sub_test_move2
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_queen2


subr sub_king2
'updated 2000/05/20
    'board is in sg_board2, dg_movefrom2
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto2 = dg_movefrom2 + d_delta

	  $cut sg_moveto2, sg_board2, dg_moveto2, 1
	  $lok d_any, sg_tobyblack, 1, sg_moveto2

	  dift d_any > 0
		sg_board3 = sg_board2
		$rep sg_board3, dg_moveto2, "k"
		$rep sg_board3, dg_movefrom2, "-"

		sub_test_move2
	  endi

	  dinc d_index
    endw
ends sub_king2


subr sub_test_move2
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot

    sg_chkboard = sg_board3
    sub_bincheck

    dift dg_bincheck <> 1
	  dinc dg_movecount2
	  dg_value3 = 2

	  sub_move3

	  dift dg_value3 = 2
		'if no good move3
		dg_value2 = 1
		dg_movefromhold2 = dg_movefrom2
		dg_movetohold2 = dg_moveto2
		sg_movemanhold2 = sg_moveman2
	  else
		'if good move3
		dinc dg_matescount
	  endi
    endi
ends sub_test_move2


subr sub_move3
'updated 2000/05/20
    'board is sg_board3
    vari d_any, s_any, d_dot, s_dot

    dg_movefrom3 = 111
    dwhi dg_movefrom3 <= 188
	  $cut sg_moveman3, sg_board3, dg_movefrom3, 1
	  $lok d_any, sg_menwhite, 1, sg_moveman3
	  dift d_any > 0
		$if1 sg_moveman3 = "P": sub_pawn3
		$if1 sg_moveman3 = "N": sub_knight3
		$if1 sg_moveman3 = "B": sub_bishop3
		$if1 sg_moveman3 = "R": sub_rook3
		$if1 sg_moveman3 = "Q": sub_queen3
		$if1 sg_moveman3 = "K": sub_king3
	  endi

	  dinc dg_movefrom3
    endw
ends sub_move3


subr sub_pawn3
'updated 2000/05/20
    'board is in sg_board3, dg_movefrom3
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_torank

    'one move forward
    dg_moveto3 = dg_movefrom3 + 1
    d_torank = dg_moveto3 % 10
    $cut sg_moveto3, sg_board3, dg_moveto3, 1

    $ift sg_moveto3 = "-"
	  dift d_torank = 8
	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "N"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "B"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "R"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "Q"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3
	  else
	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "P"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3
	  endi

	  'two moves forward
	  d_any = dg_movefrom3 % 10

	  dift d_any = 2
            dg_moveto3 = dg_movefrom3 + 2
            $cut sg_moveto3, sg_board3, dg_moveto3, 1

            $ift sg_moveto3 = "-"
	          sg_board4 = sg_board3
	          $rep sg_board4, dg_moveto3, "P"
	          $rep sg_board4, dg_movefrom3, "-"
	          sub_test_move3
		endi
	  endi
    endi

    'capture 44 to 35
    dg_moveto3 = dg_movefrom3 - 9
    $cut sg_moveto3, sg_board3, dg_moveto3, 1
    $lok d_any, sg_menblack, 1, sg_moveto3

    dift d_any > 0
	  dift d_torank = 8
	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "N"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "B"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "R"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "Q"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3
	  else
	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "P"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3
	  endi
    endi

    'capture 44 to 55
    dg_moveto3 = dg_movefrom3 + 11
    $cut sg_moveto3, sg_board3, dg_moveto3, 1
    $lok d_any, sg_menblack, 1, sg_moveto3

    dift d_any > 0
	  dift d_torank = 8
	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "N"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "B"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "R"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3

	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "Q"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3
	  else
	      sg_board4 = sg_board3
	      $rep sg_board4, dg_moveto3, "P"
	      $rep sg_board4, dg_movefrom3, "-"
	      sub_test_move3
	  endi
    endi
ends sub_pawn3


subr sub_knight3
'updated 2000/05/20
    'board is in sg_board3, dg_movefrom3
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  dg_moveto3 = dg_movefrom3 + d_delta

	  $cut sg_moveto3, sg_board3, dg_moveto3, 1
	  $lok d_any, sg_tobywhite, 1, sg_moveto3

	  dift d_any > 0
		sg_board4 = sg_board3
		$rep sg_board4, dg_moveto3, "N"
		$rep sg_board4, dg_movefrom3, "-"

		sub_test_move3
	  endi

	  dinc d_index
    endw
ends sub_knight3


subr sub_bishop3
'updated 2000/05/20
    'board is in sg_board3, dg_movefrom3
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto3 = dg_movefrom3

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto3 = dg_moveto3 + d_delta

	      $cut sg_moveto3, sg_board3, dg_moveto3, 1
	      $lok d_any, sg_tobywhite, 1, sg_moveto3

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board4 = sg_board3
		    $rep sg_board4, dg_moveto3, "B"
		    $rep sg_board4, dg_movefrom3, "-"

		    sub_test_move3
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bishop3


subr sub_rook3
'updated 2000/05/20
    'board is in sg_board3, dg_movefrom3
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  dg_moveto3 = dg_movefrom3

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto3 = dg_moveto3 + d_delta

	      $cut sg_moveto3, sg_board3, dg_moveto3, 1
	      $lok d_any, sg_tobywhite, 1, sg_moveto3

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board4 = sg_board3
		    $rep sg_board4, dg_moveto3, "R"
		    $rep sg_board4, dg_movefrom3, "-"

		    sub_test_move3
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_rook3


subr sub_queen3
'updated 2000/05/20
    'board is in sg_board3, dg_movefrom3
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto3 = dg_movefrom3

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto3 = dg_moveto3 + d_delta

	      $cut sg_moveto3, sg_board3, dg_moveto3, 1
	      $lok d_any, sg_tobywhite, 1, sg_moveto3

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board4 = sg_board3
		    $rep sg_board4, dg_moveto3, "Q"
		    $rep sg_board4, dg_movefrom3, "-"

		    sub_test_move3
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_queen3


subr sub_king3
'updated 2000/05/20
    'board is in sg_board3, dg_movefrom3
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto3 = dg_movefrom3 + d_delta

	  $cut sg_moveto3, sg_board3, dg_moveto3, 1
	  $lok d_any, sg_tobywhite, 1, sg_moveto3

	  dift d_any > 0
		sg_board4 = sg_board3
		$rep sg_board4, dg_moveto3, "K"
		$rep sg_board4, dg_movefrom3, "-"

		sub_test_move3
	  endi

	  dinc d_index
    endw
ends sub_king3


subr sub_test_move3
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot

    sg_chkboard = sg_board4
    sub_wincheck

    dift dg_wincheck <> 1

	  dinc dg_movecount3

	  dift dg_new = 1
		sg_boardm = sg_board4
		sub_binmate
		dift dg_binmate = 1
		    dg_value3 = 1
	      endi
	  else
	      sg_chkboard = sg_board4
	      sub_bincheck

	      dift dg_bincheck = 1
		    dg_value4 = 2

		    sub_move4

		    dift dg_value4 = 2
			  dg_value3 = 1
		    endi
	      endi
	  endi
    endi
ends sub_test_move3


subr sub_move4
'updated 2000/05/20
    'board is sg_board4
    vari d_any, s_any, d_dot, s_dot

    dg_movefrom4 = 111
    dwhi dg_movefrom4 <= 188
	  $cut sg_moveman4, sg_board4, dg_movefrom4, 1
	  $lok d_any, sg_menblack, 1, sg_moveman4
	  dift d_any > 0
		$if1 sg_moveman4 = "p": sub_pawn4
		$if1 sg_moveman4 = "n": sub_knight4
		$if1 sg_moveman4 = "b": sub_bishop4
		$if1 sg_moveman4 = "r": sub_rook4
		$if1 sg_moveman4 = "q": sub_queen4
		$if1 sg_moveman4 = "k": sub_king4
	  endi

	  dinc dg_movefrom4
    endw
ends sub_move4


subr sub_pawn4
'updated 2000/05/20
    'board is in sg_board4, dg_movefrom4
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    'one move forward
    dg_moveto4 = dg_movefrom4 - 1
    $cut sg_moveto4, sg_board4, dg_moveto4, 1

    $ift sg_moveto4 = "-"
	  sg_board5 = sg_board4
	  $rep sg_board5, dg_moveto4, "p"
	  $rep sg_board5, dg_movefrom4, "-"
	  sub_test_move4

	  'two moves forward
	  d_any = dg_movefrom4 % 10

	  dift d_any = 7
            dg_moveto4 = dg_movefrom4 - 2
            $cut sg_moveto4, sg_board4, dg_moveto4, 1

            $ift sg_moveto4 = "-"
	          sg_board5 = sg_board4
	          $rep sg_board5, dg_moveto4, "p"
	          $rep sg_board5, dg_movefrom4, "-"
	          sub_test_move4
		endi
	  endi
    endi

    'capture 45 to 34
    dg_moveto4 = dg_movefrom4 - 11
    $cut sg_moveto4, sg_board4, dg_moveto4, 1
    $lok d_any, sg_menwhite, 1, sg_moveto4

    dift d_any > 0
	  sg_board5 = sg_board4
	  $rep sg_board5, dg_moveto4, "p"
	  $rep sg_board5, dg_movefrom4, "-"
	  sub_test_move4
    endi

    'capture 45 to 54
    dg_moveto4 = dg_movefrom4 + 9
    $cut sg_moveto4, sg_board4, dg_moveto4, 1
    $lok d_any, sg_menwhite, 1, sg_moveto4

    dift d_any > 0
	  sg_board5 = sg_board4
	  $rep sg_board5, dg_moveto4, "p"
	  $rep sg_board5, dg_movefrom4, "-"
	  sub_test_move4
    endi
ends sub_pawn4


subr sub_knight4
'updated 2000/05/20
    'board is in sg_board4, dg_movefrom4
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  dg_moveto4 = dg_movefrom4 + d_delta

	  $cut sg_moveto4, sg_board4, dg_moveto4, 1
	  $lok d_any, sg_tobyblack, 1, sg_moveto4

	  dift d_any > 0
		sg_board5 = sg_board4
		$rep sg_board5, dg_moveto4, "n"
		$rep sg_board5, dg_movefrom4, "-"

		sub_test_move4
	  endi

	  dinc d_index
    endw
ends sub_knight4


subr sub_bishop4
'updated 2000/05/20
    'board is in sg_board4, dg_movefrom4
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto4 = dg_movefrom4

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto4 = dg_moveto4 + d_delta

	      $cut sg_moveto4, sg_board4, dg_moveto4, 1
	      $lok d_any, sg_tobyblack, 1, sg_moveto4

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board5 = sg_board4
		    $rep sg_board5, dg_moveto4, "b"
		    $rep sg_board5, dg_movefrom4, "-"

		    sub_test_move4
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bishop4


subr sub_rook4
'updated 2000/05/20
    'board is in sg_board4, dg_movefrom4
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  dg_moveto4 = dg_movefrom4

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto4 = dg_moveto4 + d_delta

	      $cut sg_moveto4, sg_board4, dg_moveto4, 1
	      $lok d_any, sg_tobyblack, 1, sg_moveto4

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board5 = sg_board4
		    $rep sg_board5, dg_moveto4, "r"
		    $rep sg_board5, dg_movefrom4, "-"

		    sub_test_move4
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_rook4


subr sub_queen4
'updated 2000/05/20
    'board is in sg_board4, dg_movefrom4
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto4 = dg_movefrom4

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_moveto4 = dg_moveto4 + d_delta

	      $cut sg_moveto4, sg_board4, dg_moveto4, 1
	      $lok d_any, sg_tobyblack, 1, sg_moveto4

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_board5 = sg_board4
		    $rep sg_board5, dg_moveto4, "q"
		    $rep sg_board5, dg_movefrom4, "-"

		    sub_test_move4
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_queen4


subr sub_king4
'updated 2000/05/20
    'board is in sg_board4, dg_movefrom4
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_moveto4 = dg_movefrom4 + d_delta

	  $cut sg_moveto4, sg_board4, dg_moveto4, 1
	  $lok d_any, sg_tobyblack, 1, sg_moveto4

	  dift d_any > 0
		sg_board5 = sg_board4
		$rep sg_board5, dg_moveto4, "k"
		$rep sg_board5, dg_movefrom4, "-"

		sub_test_move4
	  endi

	  dinc d_index
    endw
ends sub_king4


subr sub_test_move4
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot

    sg_chkboard = sg_board5
    sub_bincheck

    dift dg_bincheck <> 1
	  dg_value4 = 1
    endi
ends sub_test_move4


subr sub_wincheck
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot
    vari d_loop, d_count
    vari d_index, d_delta, d_wking
    vari d_square, s_square

    dg_wincheck = 2
    $lok d_wking, sg_chkboard, 111, "K"

    'check by knight
    d_index = 91

    dwhi d_index <= 98
	  itod d_delta, d_index

	  d_square = d_wking + d_delta
	  $cut s_square, sg_chkboard, d_square, 1
	  $if1 s_square = "n": dg_wincheck = 1

	  dinc d_index
    endw

    'check by Pawn, bishop, rook, queen or king
    d_index = 81

    dwhi d_index <= 88
	  itod d_delta, d_index

	  'is white in check
	  d_square = d_wking
	  d_count = 0
	  d_loop = 1

	  dwhi d_loop = 1
		dinc d_count
		d_square = d_square + d_delta
		$cut s_square, sg_chkboard, d_square, 1
		$if1 s_square <> "-": dinc d_loop
	  endw
	  dift d_count = 1
		'Black Pawn or Black King
		$if1 s_square = "k": dg_wincheck = 1

		'how about a Pawn
		$ift s_square = "p"
		    '54 by 45 or 65
		    dift d_delta = -9: dg_wincheck = 1
		    dift d_delta = 11: dg_wincheck = 1
		endi 
	  endi
	  $if1 s_square = "q": dg_wincheck = 1
	  $ift s_square = "r"
		dif1 d_index <= 84: dg_wincheck = 1
	  endi
	  $ift s_square = "b"
		dif1 d_index >= 85: dg_wincheck = 1
	  endi

	  dinc d_index
    endw
ends sub_wincheck


subr sub_bincheck
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot
    vari d_loop, d_count
    vari d_index, d_delta, d_bking
    vari d_square, s_square

    dg_bincheck = 2
    $lok d_bking, sg_chkboard, 111, "k"

    'check by knight
    d_index = 91

    dwhi d_index <= 98
	  itod d_delta, d_index

	  d_square = d_bking + d_delta
	  $cut s_square, sg_chkboard, d_square, 1
	  $if1 s_square = "N": dg_bincheck = 1

	  dinc d_index
    endw

    'check by Pawn, bishop, rook, queen or king
    d_index = 81

    dwhi d_index <= 88
	  itod d_delta, d_index

	  'is Black in check
	  d_square = d_bking
	  d_count = 0
	  d_loop = 1

	  dwhi d_loop = 1
		dinc d_count
		d_square = d_square + d_delta
		$cut s_square, sg_chkboard, d_square, 1
		$if1 s_square <> "-": dinc d_loop
	  endw
	  dift d_count = 1
		'White Pawn or White King
		$if1 s_square = "K": dg_bincheck = 1

		'how about a Pawn, 45 by 34, 45 by 54
		$ift s_square = "P"
		    '55 by 44 or 64
		    dift d_delta = -11: dg_bincheck = 1
		    dift d_delta = 9: dg_bincheck = 1
		endi 
	  endi
	  $if1 s_square = "Q": dg_bincheck = 1
	  $ift s_square = "R"
		dif1 d_index <= 84: dg_bincheck = 1
	  endi
	  $ift s_square = "B"
		dif1 d_index >= 85: dg_bincheck = 1
	  endi

	  dinc d_index
    endw
ends sub_bincheck


subr sub_binmate
'updated 2000/05/20
    'board is sg_boardm
    vari d_any, s_any, d_dot, s_dot

    sg_chkboard = sg_boardm
    sub_bincheck
    dift dg_bincheck <> 1
	  dg_binmate = 2
    else
	  dg_binmate = 1
        dg_movefromm = 111
        dwhi dg_movefromm <= 188
	      $cut sg_movemanm, sg_boardm, dg_movefromm, 1
	      $lok d_any, sg_menblack, 1, sg_movemanm
	      dift d_any > 0
		    $if1 sg_movemanm = "p": sub_pawnm
		    $if1 sg_movemanm = "n": sub_knightm
		    $if1 sg_movemanm = "b": sub_bishopm
		    $if1 sg_movemanm = "r": sub_rookm
		    $if1 sg_movemanm = "q": sub_queenm
		    $if1 sg_movemanm = "k": sub_kingm
	      endi
	      dif1 dg_binmate = 2: dg_movefromm = 200
	      dinc dg_movefromm
        endw
    endi
ends sub_binmate


subr sub_pawnm
'updated 2000/05/20
    'board is in sg_boardm, dg_movefromm
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    'one move forward
    dg_movetom = dg_movefromm - 1
    $cut sg_movetom, sg_boardm, dg_movetom, 1

    $ift sg_movetom = "-"
	  sg_chkboard = sg_boardm
	  $rep sg_chkboard, dg_movetom, "p"
	  $rep sg_chkboard, dg_movefromm, "-"

        sub_bincheck
        dif1 dg_bincheck <> 1: dinc dg_binmate

	  'two moves forward
	  d_any = dg_movefromm % 10

	  dift d_any = 7
            dg_movetom = dg_movefromm - 2
            $cut sg_movetom, sg_boardm, dg_movetom, 1

            $ift sg_movetom = "-"
	          sg_chkboard = sg_boardm
	          $rep sg_chkboard, dg_movetom, "p"
	          $rep sg_chkboard, dg_movefromm, "-"

	          sub_bincheck
      	    dif1 dg_bincheck <> 1: dinc dg_binmate
		endi
	  endi
    endi

    'capture 45 to 34
    dg_movetom = dg_movefromm - 11
    $cut sg_movetom, sg_boardm, dg_movetom, 1
    $lok d_any, sg_menwhite, 1, sg_movetom

    dift d_any > 0
	  sg_chkboard = sg_boardm
	  $rep sg_chkboard, dg_movetom, "p"
	  $rep sg_chkboard, dg_movefromm, "-"

        sub_bincheck
        dif1 dg_bincheck <> 1: dinc dg_binmate
    endi

    'capture 45 to 54
    dg_movetom = dg_movefromm + 9
    $cut sg_movetom, sg_boardm, dg_movetom, 1
    $lok d_any, sg_menwhite, 1, sg_movetom

    dift d_any > 0
	  sg_chkboard = sg_boardm
	  $rep sg_chkboard, dg_movetom, "p"
	  $rep sg_chkboard, dg_movefromm, "-"

        sub_bincheck
        dif1 dg_bincheck <> 1: dinc dg_binmate
    endi
ends sub_pawnm


subr sub_knightm
'updated 2000/05/20
    'board is in sg_boardm, dg_movefromm
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  dg_movetom = dg_movefromm + d_delta

	  $cut sg_movetom, sg_boardm, dg_movetom, 1
	  $lok d_any, sg_tobyblack, 1, sg_movetom

	  dift d_any > 0
		sg_chkboard = sg_boardm
		$rep sg_chkboard, dg_movetom, "n"
		$rep sg_chkboard, dg_movefromm, "-"

            sub_bincheck
            dif1 dg_bincheck <> 1: dinc dg_binmate
	  endi

	  dinc d_index
    endw
ends sub_knightm


subr sub_bishopm
'updated 2000/05/20
    'board is in sg_boardm, dg_movefromm
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_movetom = dg_movefromm

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_movetom = dg_movetom + d_delta

	      $cut sg_movetom, sg_boardm, dg_movetom, 1
	      $lok d_any, sg_tobyblack, 1, sg_movetom

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_chkboard = sg_boardm
		    $rep sg_chkboard, dg_movetom, "b"
		    $rep sg_chkboard, dg_movefromm, "-"

	          sub_bincheck
	          dif1 dg_bincheck <> 1: dinc dg_binmate
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bishopm


subr sub_rookm
'updated 2000/05/20
    'board is in sg_boardm, dg_movefromm
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  dg_movetom = dg_movefromm

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_movetom = dg_movetom + d_delta

	      $cut sg_movetom, sg_boardm, dg_movetom, 1
	      $lok d_any, sg_tobyblack, 1, sg_movetom

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_chkboard = sg_boardm
		    $rep sg_chkboard, dg_movetom, "r"
		    $rep sg_chkboard, dg_movefromm, "-"

	          sub_bincheck
	          dif1 dg_bincheck <> 1: dinc dg_binmate
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_rookm


subr sub_queenm
'updated 2000/05/20
    'board is in sg_boardm, dg_movefromm
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_movetom = dg_movefromm

	  d_loop = 1
	  dwhi d_loop = 1
	      dg_movetom = dg_movetom + d_delta

	      $cut sg_movetom, sg_boardm, dg_movetom, 1
	      $lok d_any, sg_tobyblack, 1, sg_movetom

		dif1 d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_chkboard = sg_boardm
		    $rep sg_chkboard, dg_movetom, "q"
		    $rep sg_chkboard, dg_movefromm, "-"

	          sub_bincheck
	          dif1 dg_bincheck <> 1: dinc dg_binmate
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_queenm


subr sub_kingm
'updated 2000/05/20
    'board is in sg_boardm, dg_movefromm
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  dg_movetom = dg_movefromm + d_delta

	  $cut sg_movetom, sg_boardm, dg_movetom, 1
	  $lok d_any, sg_tobyblack, 1, sg_movetom

	  dift d_any > 0
		sg_chkboard = sg_boardm
		$rep sg_chkboard, dg_movetom, "k"
		$rep sg_chkboard, dg_movefromm, "-"

	      sub_bincheck
	      dif1 dg_bincheck <> 1: dinc dg_binmate
	  endi

	  dinc d_index
    endw
ends sub_kingm


subr sub_show_score1
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot
    vari s_out, s_algebra1, s_algebra2

    dg_pass1 = dg_movefrom1
    sub_to_algebra
    s_algebra1 = sg_pass1

    dg_pass1 = dg_moveto1
    sub_to_algebra
    s_algebra2 = sg_pass1

    s_out = dg_movecount1 + ". " + sg_moveman1
    s_out = s_out + s_algebra1 + "-" + s_algebra2

    dg_pass1 = dg_movefromhold2
    sub_to_algebra
    s_algebra1 = sg_pass1

    dg_pass1 = dg_movetohold2
    sub_to_algebra
    s_algebra2 = sg_pass1

    $cup s_any, sg_movemanhold2
    s_out = s_out + " " + s_any + s_algebra1 + "-" + s_algebra2

    s_out = s_out + ", moves2=" + dg_movecount2
    s_out = s_out + ", moves3=" + dg_movecount3
    sg_score = s_out + ", mates3=" + dg_matescount 
    $out sg_score
ends sub_show_score1


subr sub_show_score3
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot
    vari s_algebra1, s_algebra2

    dg_pass1 = dg_movefrom1
    sub_to_algebra
    s_algebra1 = sg_pass1

    dg_pass1 = dg_moveto1
    sub_to_algebra
    s_algebra2 = sg_pass1

    $out "1." + sg_moveman1 + s_algebra1 + "-" + s_algebra2

    dg_pass1 = dg_movefrom2
    sub_to_algebra
    s_algebra1 = sg_pass1

    dg_pass1 = dg_moveto2
    sub_to_algebra
    s_algebra2 = sg_pass1

    $cup s_any, sg_moveman2
    $out "1...." + s_any + s_algebra1 + "-" + s_algebra2

    dg_pass1 = dg_movefrom3
    sub_to_algebra
    s_algebra1 = sg_pass1

    dg_pass1 = dg_moveto3
    sub_to_algebra
    s_algebra2 = sg_pass1

    $out "2." + sg_moveman3 + s_algebra1 + "-" + s_algebra2
ends sub_show_score3


subr sub_to_algebra
'updated 2000/05/20
    vari d_any, s_any, d_dot, s_dot
    vari d_square, s_file, d_rank

    d_square = dg_pass1 % 100
    d_rank = d_square % 10
    d_any = d_square \ 10
    $cut s_file, sg_abcdefgh, d_any, 1
    sg_pass1 = s_file + d_rank
ends sub_to_algebra

subr sub_speedquick
'updated 2008/02/23
    vari d_any, d_dot, d_time

    dsec d_time
    d_dot = 10 ^ 6 * 2
    d_any = 0
    dwhi d_any < d_dot
	  dinc d_any
    endw
    dsec d_any
    dg_pass1 = d_any - d_time
ends sub_speedquick