'tea3move, solve 3-move chess problems
'Written in Teapro which uses the OpenTea technology
'People need computer software that actually works.
vari sg_pass1, sg_pass2, sg_pass3
vari dg_pass1, dg_pass2, dg_pass3

vari dg_ply
vari sg_menwhite, sg_menblack
vari sg_abcdefgh, sg_12345678
vari sg_tobywhite, sg_tobyblack
vari sg_board0, sg_in1, sg_in2
vari dg_movestoshow, sg_outputfile
vari dg_value1, dg_value2, dg_value3
vari dg_value4, dg_value5, dg_value6
vari sg_move1, sg_move2, sg_move3, sg_move4, sg_move5

vari dg_movefrom1, dg_moveto1, sg_moveman1, sg_promote1
vari dg_movefrom2, dg_moveto2, sg_moveman2, sg_promote2
vari dg_movefrom3, dg_moveto3, sg_moveman3, sg_promote3
vari dg_movefrom4, dg_moveto4, sg_moveman4, sg_promote4

vari dg_movefromhold1, dg_movetohold1, sg_movemanhold1
vari dg_movefromhold2, dg_movetohold2, sg_movemanhold2

vari dg_movecount1, dg_movecount2, dg_movecount3
vari dg_movecount4, dg_movecount5, dg_movecount6

vari dg_onecount1, dg_onecount2, dg_onecount3
vari dg_onecount4, dg_onecount5, dg_onecount6

vari dg_totalmoves1, dg_totalmoves2, dg_totalmoves3
vari dg_totalmoves4, dg_totalmoves5, dg_totalmoves6

vari dg_solutionindex, sg_score
vari dg_totalmatescount

sub_main
endp

subr sub_main
'updated 2008/02/25, 2002/11/10
    vari s_any, d_any, s_dot, d_dot
    vari d_loop, s_pick

    $out "Program: tea3move.tea, build 14, 2009/10/25"
    $out "Copyright (c) 2002-2009 D La Pierre Ballard"
    $out "Written in Teapro which uses the OpenTea technology"
    $out "Copyright (c) 1997-2009 D La Pierre Ballard"
    $out "This program was begun 2000/04/26"
    $out "People need computer software that actually works."
    dsec d_any
    ded$ s_any, d_any, 0, 15
    $out "Seconds = " + s_any

    dg_movestoshow = 0
    d_loop = 1
    dwhi d_loop = 1
	  'get sg_outputfile
        dran d_any
        d_any = d_any * 99999 * 99999 \ 1
        sg_outputfile = "TEA3" + d_any + d_any
        $cut sg_outputfile, sg_outputfile, 1, 8
	  sg_outputfile = sg_outputfile + ".TXT"
	  $out "outputfile=" + sg_outputfile

	  sub_speedquick
	  $out "speed=" + dg_pass1

	  $out "1 = Solve a Composition"
	  $inp s_pick, "Choose a number, * to end"

	  $ift s_pick = "1": sub_process
	  $ift s_pick = "*": dinc d_loop
    endw    
ends sub_main


subr sub_initialize
'updated 2002/03/05
    vari s_any, d_any
    vari s_pick

    sg_in1 = " "
    sg_in2 = " "

    $out "1 = Sam Loyd, 1857"
    sg_in1 =  "2. Miroslav Havel, Sjakk Nytt 11, 1947 "
    sg_in1 = sg_in1 + "1st Prize"
    $out sg_in1
    $out "3 = Viktor Volcheck, Wola Gulowska 2001"
    $out "4 = Valery M. Shavyrin, Wola Gulowska 2001"
    $out "5 = Milan Vukcevich, Phenix 1999, 1st"
    $out "6 = Milan Velimirovic, Phenix 1999, 1st mention"
    $out "7. Samuel Loyd, Chess Monthly 1859"
    $out "8. Samuel Loyd, New York Albion 1859"
    $out "9. Samuel Loyd, Baltimore Dispatch 1859"
    $out "10. Lev Loshinski, 64 1974 1st Prize"
    $out "11. Lev Loshinski, Themes 64 1974 1st Prize"
    $out "12. Lev Loshinski, 64 1973 1st Prize"
    $out "13. Lev Loshinski, Gruzija-50 1972 1st Prize"
    $out "14. Lev Loshinski, Die Schwalbe 1971 1st Prize"
    $out "15. Miroslav Havel Zlata Praha 30/4 1910 2nd Prize"
    $inp s_pick, "Enter a number or return for more"

    $trb s_pick, s_pick
    $ift s_pick = ""
        $out "16. Miroslav Havel Zlata Praha 30/4 1910 4th Prize"
        $out "17. Miroslav Havel Natal Mercury 31/12 1911 1st Prize"
	  $out "18. Miroslav Havel, Denj 28/6 1913 1st Prize"
	  $out "19. Miroslav Havel, Tidskritt fur Schack 5-6 1914 1st Prize"
	  $out "20. Miroslav Havel, Cesky spolek sachovni 30/3 1915 1st Prize"
	  $out "21. Miroslav Havel, Cesky spolek sachovni 1/5 1916 2nd Prize"
	  $out "22. Miroslav Havel, Norsk Schakblad 3-4 1920 1st Prize"
	  $out "23. Lev Loshinski, Smena 1931 1st Prize"
	  $out "24. Lev Loshinski, Rostov 1940 1st Prize"
	  $out "25. Lev Loshinski, Konk. Pamjati Kubbelja 1946 1st Prize"
	  s_any = "26. Lev Loshinski, Aleksandr Feoktistov, "
	  $app s_any, "Wirtanen Jt 1973 3rd Prize"
	  $out s_any

	  $out "27. Lev Loshinski, 64 1973 1st Prize"
	  $out "28. Lev Loshinski, Gruzija-50 1972, 1st Prize"

	  sg_in1 = "29. Lev Loshinski, Yakov Georgevich Vladimirov "
	  sg_in1 = sg_in1 + "64 1968, 1st Prize"
	  $out sg_in1

	  sg_in1 = "30. Lev Loshinski, Schweizerische "
	  sg_in1 = sg_in1 + "Schachzeitung 1970, 1st Prize"
	  $out sg_in1

        $inp s_pick, "Enter a number or return for more"
    endi

    $trb s_pick, s_pick
    $ift s_pick = ""
	  sg_in1 = "31. Lev Loshinski, Jubilejnij Konk. S. F. "
	  sg_in1 = sg_in1 + "1969, 1st Prize"
	  $out sg_in1

	  sg_in1 = "32. Lev Loshinski, Evgeny Ivanovich "
	  sg_in1 = sg_in1 + "Umnov, Tijdschrift 1930, "
	  sg_in1 = sg_in1 + "1st Prize"
	  $out sg_in1

	  sg_in1 = "33. Lev Ilitch Loshinski, Rafael Kofman, "
	  sg_in1 = sg_in1 + "Comite des Sports du Belarus, "
	  sg_in1 = sg_in1 + "1st Prize"
	  $out sg_in1

	  sg_in1 = "34. Lev Loshinski, Uzbecki Sportkomite "
	  sg_in1 = sg_in1 + "1955, 1st Prize"
	  $out sg_in1

        $inp s_pick, "Enter a number or return for more"
    endi

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

    dg_movestoshow = 0
    $inp s_any, "ply to show 1..5"
    $isd d_any, s_any
    dift d_any = 1: $tod dg_movestoshow, s_any   

    $ift s_pick = "1"
	  sg_in1 = "1. Sam Loyd, First Prize, "
	  sg_in1 = sg_in1 + "Chess monthly, 1857"
	  sg_in2 = "1.Nf2-g4, 108,932"
        sg_board0 = sg_board0 + "------R-  "
        sg_board0 = sg_board0 + "--K-----  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "-----p--  "
        sg_board0 = sg_board0 + "------p-  "
        sg_board0 = sg_board0 + "-Q---Nk-  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "2"
	  sg_in1 =  "2. Miroslav Havel, Sjakk Nytt 11, 1947, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "1.Nf7-g5, 170,869"
        sg_board0 = sg_board0 + "-q-Bk---  "
        sg_board0 = sg_board0 + "-----N--  "
        sg_board0 = sg_board0 + "-rpp----  "
        sg_board0 = sg_board0 + "-b------  "
        sg_board0 = sg_board0 + "--p--R--  "
        sg_board0 = sg_board0 + "-------Q  "
        sg_board0 = sg_board0 + "------K-  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "3"
	  sg_in1 = "3. Viktor Volchek, Wola Gulowska 2001"
	  sg_in2 = "1.Qf1-b5, 695,536, times=2"
        sg_board0 = sg_board0 + "--BB-KR-  "
        sg_board0 = sg_board0 + "-----n--  "
        sg_board0 = sg_board0 + "r---p---  "
        sg_board0 = sg_board0 + "p---R-p-  "
        sg_board0 = sg_board0 + "r---pknP  "
        sg_board0 = sg_board0 + "----pNpP  "
        sg_board0 = sg_board0 + "--NP--P-  "
        sg_board0 = sg_board0 + "---b-Q--  "
    endi
    $ift s_pick = "4"
	  sg_in1 = "4. Valery M. Shavyrin, Wola Gulowska 2001"
	  sg_in2 = "1.Nf4-h3, 359,425, times=2"
        sg_board0 = sg_board0 + "-------B  "
        sg_board0 = sg_board0 + "-Bp-----  "
        sg_board0 = sg_board0 + "--R----p  "
        sg_board0 = sg_board0 + "---NP--K  "
        sg_board0 = sg_board0 + "--ppkNP-  "
        sg_board0 = sg_board0 + "r-----p-  "
        sg_board0 = sg_board0 + "p-PPrPpP  "
        sg_board0 = sg_board0 + "--n-----  "
    endi
    $ift s_pick = "5"
	  sg_in1 = "5. Milan Vukcevich, Phenix 1999, 1st"
	  sg_in2 = "1.Rd7-e7, 161,498, times=2"
        sg_board0 = sg_board0 + "--r-----  "
        sg_board0 = sg_board0 + "---R-p--  "
        sg_board0 = sg_board0 + "-P--pKp-  "
        sg_board0 = sg_board0 + "nQ-bB---  "
        sg_board0 = sg_board0 + "Np--kP-p  "
        sg_board0 = sg_board0 + "---R--P-  "
        sg_board0 = sg_board0 + "n---P---  "
        sg_board0 = sg_board0 + "--r-----  "
    endi
    $ift s_pick = "6"
	  sg_in1 = "6. Milan Velimirovic, Phenix 1999, "
	  sg_in1 = sg_in1 + "1st mention"
	  sg_in2 = "1.Rd8-e8, 144,646, times=2"
        sg_board0 = sg_board0 + "---R----  "
        sg_board0 = sg_board0 + "B-K--Rp-  "
        sg_board0 = sg_board0 + "--P---P-  "
        sg_board0 = sg_board0 + "----N---  "
        sg_board0 = sg_board0 + "p---kPP-  "
        sg_board0 = sg_board0 + "r-----p-  "
        sg_board0 = sg_board0 + "-nPp--P-  "
        sg_board0 = sg_board0 + "-N-r----  "
    endi
    $ift s_pick = "7"
	  sg_in1 = "7. Samuel Loyd, Chess Monthly 1859"
	  sg_in2 = "1.Rg7-g3, 12,106"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "------R-  "
        sg_board0 = sg_board0 + "-------p  "
        sg_board0 = sg_board0 + "-----K-k  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "------p-  "
        sg_board0 = sg_board0 + "-----bPP  "
        sg_board0 = sg_board0 + "----N---  "
    endi
    $ift s_pick = "8"
	  sg_in1 = "8. Samuel Loyd, New York Albion 1859"
	  sg_in2 = "19,631"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--pB-p--  "
        sg_board0 = sg_board0 + "---k-K--  "
        sg_board0 = sg_board0 + "-----P--  "
        sg_board0 = sg_board0 + "QP------  "
        sg_board0 = sg_board0 + "-----b--  "
    endi
    $ift s_pick = "9"
	  sg_in1 = "9. Samuel Loyd, Baltimore Dispatch 1859"
	  sg_in2 = "67,664"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "-----N--  "
        sg_board0 = sg_board0 + "--k-----  "
        sg_board0 = sg_board0 + "----p---  "
        sg_board0 = sg_board0 + "-B------  "
        sg_board0 = sg_board0 + "K--Q----  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "10"
	  sg_in1 = "10. Lev Loshinski, 64 1974 1st Prize"
	  sg_in2 = "1.Re6-g6, 185,313"
        sg_board0 = sg_board0 + "-Q----K-  "
        sg_board0 = sg_board0 + "----pp--  "
        sg_board0 = sg_board0 + "-p--R---  "
        sg_board0 = sg_board0 + "rr--P-N-  "
        sg_board0 = sg_board0 + "--bPBkpB  "
        sg_board0 = sg_board0 + "--N--p--  "
        sg_board0 = sg_board0 + "---P----  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "11"
	  sg_in1 = "11. Lev Loshinski, Themes 64 1974, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "1.Bc4-e6, 40,891"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "B-p--N-b  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "-pr-N-K-  "
        sg_board0 = sg_board0 + "--Bkp-p-  "
        sg_board0 = sg_board0 + "--p-p---  "
        sg_board0 = sg_board0 + "--PpPRrp  "
        sg_board0 = sg_board0 + "-----nbn  "
    endi
    $ift s_pick = "12"
	  sg_in1 = "12. Lev Loshinski, 64 1973 1st Prize"
	  sg_in2 = "1.Nf7-g5, 551,523"
        sg_board0 = sg_board0 + "-----bN-  "
        sg_board0 = sg_board0 + "r---pN--  "
        sg_board0 = sg_board0 + "-p-B-RB-  "
        sg_board0 = sg_board0 + "pKPk----  "
        sg_board0 = sg_board0 + "---p----  "
        sg_board0 = sg_board0 + "-p-pp-Q-  "
        sg_board0 = sg_board0 + "-----r-q  "
        sg_board0 = sg_board0 + "-------b  "
    endi
    $ift s_pick = "13"
	  sg_in1 = "13. Lev Loshinski, Gruzija-50 1972, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "1.Pd6-d7, 237,330"
        sg_board0 = sg_board0 + "--n-n---  "
        sg_board0 = sg_board0 + "r----P--  "
        sg_board0 = sg_board0 + "-PpP-Nb-  "
        sg_board0 = sg_board0 + "--B-kpN-  "
        sg_board0 = sg_board0 + "-----p-Q  "
        sg_board0 = sg_board0 + "pKP-----  "
        sg_board0 = sg_board0 + "---P----  "
        sg_board0 = sg_board0 + "----br--  "
    endi
    $ift s_pick = "14"
	  sg_in1 = "14. Lev Loshinski, Die Schwalbe 1971, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "1.Ka4-b5, 144,227"
        sg_board0 = sg_board0 + "-------r  "
        sg_board0 = sg_board0 + "--P-R-p-  "
        sg_board0 = sg_board0 + "NP----P-  "
        sg_board0 = sg_board0 + "---kB---  "
        sg_board0 = sg_board0 + "Kp-Np-R-  "
        sg_board0 = sg_board0 + "---Pr-P-  "
        sg_board0 = sg_board0 + "----B---  "
        sg_board0 = sg_board0 + "----b---  "
    endi
    $ift s_pick = "15"
	  sg_in1 = "15. Miroslav Havel, Zlata Praha 30/4 "
	  sg_in1 = sg_in1 + "1910, 2nd Prize"
	  sg_in2 = "1.Bf5-g6, 176,791"
        sg_board0 = sg_board0 + "-K------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "-----p--  "
        sg_board0 = sg_board0 + "R----B--  "
        sg_board0 = sg_board0 + "-p------  "
        sg_board0 = sg_board0 + "---Npk--  "
        sg_board0 = sg_board0 + "---p---Q  "
        sg_board0 = sg_board0 + "-b------  "
    endi
    $ift s_pick = "16"
	  sg_in1 = "16. Miroslav Havel, Zlata Praha 30/4 "
	  sg_in1 = sg_in1 + "1910 4th Prize"
	  sg_in2 = "1.Kf3-g4, 116,348"
        sg_board0 = sg_board0 + "-b---B--  "
        sg_board0 = sg_board0 + "----R---  "
        sg_board0 = sg_board0 + "-p-n----  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "---k----  "
        sg_board0 = sg_board0 + "-Q---Kn-  "
        sg_board0 = sg_board0 + "---N----  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "17"
	  sg_in1 = "17. Miroslav Havel, Natal Mercury 31/12, "
	  sg_in1 = sg_in1 + "1911 1st Prize"
	  sg_in2 = "1.Rd2-g2, 115,960"
        sg_board0 = sg_board0 + "-Q------  "
        sg_board0 = sg_board0 + "-p------  "
        sg_board0 = sg_board0 + "----kp--  "
        sg_board0 = sg_board0 + "--p-----  "
        sg_board0 = sg_board0 + "--N-----  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--KR----  "
        sg_board0 = sg_board0 + "--n----B  "
    endi
    $ift s_pick = "18"
	  sg_in1 = "18. Miroslav Havel, Denj 28/6 1913, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "1.Nf2-e4, 179,659"
        sg_board0 = sg_board0 + "--n----Q  "
        sg_board0 = sg_board0 + "-------b  "
        sg_board0 = sg_board0 + "----p-p-  "
        sg_board0 = sg_board0 + "-----k--  "
        sg_board0 = sg_board0 + "--p-----  "
        sg_board0 = sg_board0 + "---nB---  "
        sg_board0 = sg_board0 + "--B--NK-  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "19"
	  sg_in1 = "19. Miroslav Havel, Tidskritt fur "
	  sg_in1 = sg_in1 + "Schack, 5-6 1914 1st Prize"
	  sg_in2 = "1.Bd2-h6, 153,061"
        sg_board0 = sg_board0 + "n-------  "
        sg_board0 = sg_board0 + "-----Rp-  "
        sg_board0 = sg_board0 + "-p-kp---  "
        sg_board0 = sg_board0 + "-p--p---  "
        sg_board0 = sg_board0 + "-P-NP-p-  "
        sg_board0 = sg_board0 + "-p------  "
        sg_board0 = sg_board0 + "---BK---  "
        sg_board0 = sg_board0 + "-------Q  "
    endi
    $ift s_pick = "20"
	  sg_in1 = "20. Miroslav Havel, Cesky spolek "
	  sg_in1 = sg_in1 + "sachovni 30/3 1915 1st Prize"
	  sg_in2 = "1.Qc8-b8, 312,576"
        sg_board0 = sg_board0 + "--Q-----  "
        sg_board0 = sg_board0 + "---B--R-  "
        sg_board0 = sg_board0 + "-------p  "
        sg_board0 = sg_board0 + "Kp---nPk  "
        sg_board0 = sg_board0 + "-------p  "
        sg_board0 = sg_board0 + "---N----  "
        sg_board0 = sg_board0 + "n--Npp--  "
        sg_board0 = sg_board0 + "-----q--  "
    endi
    $ift s_pick = "21"
	  sg_in1 = "21. Miroslav Havel, Cesky spolek "
	  sg_in1 = sg_in1 + "sachovni 1/5 1916 2nd Prize"
	  sg_in2 = "1.Rg7-e7, 272,523"
        sg_board0 = sg_board0 + "--K-----  "
        sg_board0 = sg_board0 + "p-----Rn  "
        sg_board0 = sg_board0 + "---pN---  "
        sg_board0 = sg_board0 + "-N--n--p  "
        sg_board0 = sg_board0 + "----k---  "
        sg_board0 = sg_board0 + "-Q------  "
        sg_board0 = sg_board0 + "--p--R--  "
        sg_board0 = sg_board0 + "--br----  "
    endi
    $ift s_pick = "22"
	  sg_in1 = "22. Miroslav Havel, Norsk Schakblad "
	  sg_in1 = sg_in1 + "3-4 1920 1st Prize"
	  sg_in2 = "1.Ra3-e3, 208,819"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "----p--r  "
        sg_board0 = sg_board0 + "-QNkP---  "
        sg_board0 = sg_board0 + "-p--N--n  "
        sg_board0 = sg_board0 + "----B---  "
        sg_board0 = sg_board0 + "R-----p-  "
        sg_board0 = sg_board0 + "-------p  "
        sg_board0 = sg_board0 + "K-------  "
    endi
    $ift s_pick = "23"
	  sg_in1 = "23. Lev Loshinski, Smena 1931 1st Prize"
	  sg_in2 = "Three solutions"
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "b-pQ-p--  "
        sg_board0 = sg_board0 + "r--p-p-r  "
        sg_board0 = sg_board0 + "p--k-P-b  "
        sg_board0 = sg_board0 + "K-R---N-  "
        sg_board0 = sg_board0 + "-P-P-p--  "
        sg_board0 = sg_board0 + "N-BRnq-B  "
    endi
    $ift s_pick = "24"
	  sg_in1 = "24. Lev Loshinski, Rostov 1940 1st Prize"
	  sg_in2 = "1.Ra1-c1, 436,033"
        sg_board0 = sg_board0 + "r-------  "
        sg_board0 = sg_board0 + "-p--B---  "
        sg_board0 = sg_board0 + "---p----  "
        sg_board0 = sg_board0 + "--P-pNp-  "
        sg_board0 = sg_board0 + "----N-P-  "
        sg_board0 = sg_board0 + "--QRK--p  "
        sg_board0 = sg_board0 + "b-pP---k  "
        sg_board0 = sg_board0 + "R------B  "
    endi
    $ift s_pick = "25"
	  sg_in1 = "25. Lev Loshinski, Konk. Pamjati "
	  sg_in1 = sg_in1 + "Kubelja, 1946 1st Prize"
	  sg_in2 = "1.Qa6-a3, 127,133"
        sg_board0 = sg_board0 + "--rbn---  "
        sg_board0 = sg_board0 + "--P--pp-  "
        sg_board0 = sg_board0 + "Q-P-pN--  "
        sg_board0 = sg_board0 + "-RB-k--P  "
        sg_board0 = sg_board0 + "----P---  "
        sg_board0 = sg_board0 + "------Pb  "
        sg_board0 = sg_board0 + "p-------  "
        sg_board0 = sg_board0 + "K--n----  "
    endi
    $ift s_pick = "26"
	  sg_in1 = "26. Lev Loshinski, Aleksandr Feoktistov, "
	  sg_in1 = sg_in1 + "Wirtanen Jt 1973 3rd Prize"
	  sg_in2 = "1.Qh6-e3, 329,738"
        sg_board0 = sg_board0 + "-------b  "
        sg_board0 = sg_board0 + "---Prp-b  "
        sg_board0 = sg_board0 + "KRpR---Q  "
        sg_board0 = sg_board0 + "B-p-----  "
        sg_board0 = sg_board0 + "p-k-----  "
        sg_board0 = sg_board0 + "r---pN-n  "
        sg_board0 = sg_board0 + "-p-P----  "
        sg_board0 = sg_board0 + "---N----  "
    endi
    $ift s_pick = "27"
	  sg_in1 = "27. Lev Loshinski, 64 1973 1st Prize"
	  sg_in2 = "1.Nf7-g5, 551,523, times=2"
        sg_board0 = sg_board0 + "-----bN-  "
        sg_board0 = sg_board0 + "r---pN--  "
        sg_board0 = sg_board0 + "-p-B-RB-  "
        sg_board0 = sg_board0 + "pKPk----  "
        sg_board0 = sg_board0 + "---p----  "
        sg_board0 = sg_board0 + "-p-pp-Q-  "
        sg_board0 = sg_board0 + "-----r-q  "
        sg_board0 = sg_board0 + "-------b  "
    endi
    $ift s_pick = "28"
	  sg_in1 = "28. Lev Loshinski, Gruzija-50 1972"
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "1.Pd6-d7, 237,330"
        sg_board0 = sg_board0 + "--n-n---  "
        sg_board0 = sg_board0 + "r----P--  "
        sg_board0 = sg_board0 + "-PpP-Nb-  "
        sg_board0 = sg_board0 + "--B-kpN-  "
        sg_board0 = sg_board0 + "-----p-Q  "
        sg_board0 = sg_board0 + "pKP-----  "
        sg_board0 = sg_board0 + "---P----  "
        sg_board0 = sg_board0 + "----br--  "
    endi
    $ift s_pick = "29"
	  sg_in1 = "29. Lev Loshinski, Yakov Georgevich " 	  
	  sg_in1 = sg_in1 + "Vladimirov, 64 1968, 1st Prize"
	  sg_in2 = "1.Rb6-g6, 1,571,228, times=2"
        sg_board0 = sg_board0 + "rQb-----  "
        sg_board0 = sg_board0 + "--Pp-R--  "
        sg_board0 = sg_board0 + "-R------  "
        sg_board0 = sg_board0 + "---p-N-N  "
        sg_board0 = sg_board0 + "---Bk---  "
        sg_board0 = sg_board0 + "---rn---  "
        sg_board0 = sg_board0 + "--BP--P-  "
        sg_board0 = sg_board0 + "--b--nK-  "
    endi
    $ift s_pick = "30"
	  sg_in1 = "30. Lev Loshinski, Schweizerische "
	  sg_in1 = sg_in1 + "Schachzeitung 1970, 1st Prize"
	  sg_in2 = "1.Nb5-d6, 415,903, times=2"
        sg_board0 = sg_board0 + "-Bqr--B-  "
        sg_board0 = sg_board0 + "--PPP-Rb  "
        sg_board0 = sg_board0 + "pnkpN---  "
        sg_board0 = sg_board0 + "QNb-----  "
        sg_board0 = sg_board0 + "p---pP-K  "
        sg_board0 = sg_board0 + "--R----n  "
        sg_board0 = sg_board0 + "--------  "
        sg_board0 = sg_board0 + "---r----  "
    endi
    $ift s_pick = "31"
	  sg_in1 = "31. Lev Loshinski, Jubilejnij Konk. "
	  sg_in1 = sg_in1 + "S. F. 1969, 1st Prize"
	  sg_in2 = "1.Re1-e3, 1,064,337, times=2"
        sg_board0 = sg_board0 + "-B------  "
        sg_board0 = sg_board0 + "----NK-R  "
        sg_board0 = sg_board0 + "----Q-N-  "
        sg_board0 = sg_board0 + "--r--pkB  "
        sg_board0 = sg_board0 + "p----p--  "
        sg_board0 = sg_board0 + "-p-----n  "
        sg_board0 = sg_board0 + "q--P-Pb-  "
        sg_board0 = sg_board0 + "b-r-R---  "
    endi
    $ift s_pick = "32"
	  sg_in1 = "32. Lev Loshinski, Evgeny Ivanovich "
	  sg_in1 = sg_in1 + "Umnov, Tijdschrift 1930, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "no solution, correct diagram, #71"
        sg_board0 = sg_board0 + "---nr--r  "
        sg_board0 = sg_board0 + "-bpp-P--  "
        sg_board0 = sg_board0 + "-p-k-Bp-  "
        sg_board0 = sg_board0 + "p--NN--R  "
        sg_board0 = sg_board0 + "-PPRK-B-  "
        sg_board0 = sg_board0 + "------Q-  "
        sg_board0 = sg_board0 + "-----p--  "
        sg_board0 = sg_board0 + "--------  "
    endi
    $ift s_pick = "33"
	  sg_in1 = "33. Lev Ilitch Loshinski, Rafael Kofman, "
	  sg_in1 = sg_in1 + "Comite des Sports du Belarus, "
	  sg_in1 = sg_in1 + "1st Prize"
	  sg_in2 = "#90"
        sg_board0 = sg_board0 + "--N-QB-b  "
        sg_board0 = sg_board0 + "---p-p-B  "
        sg_board0 = sg_board0 + "R-----r-  "
        sg_board0 = sg_board0 + "-n-kP-p-  "
        sg_board0 = sg_board0 + "R-------  "
        sg_board0 = sg_board0 + "-P--P---  "
        sg_board0 = sg_board0 + "-NP--n--  "
        sg_board0 = sg_board0 + "K-------  "
    endi
    $ift s_pick = "34"
	  sg_in1 = "34. Lev Loshinski, Uzbecki Sportkomite "
	  sg_in1 = sg_in1 + "1955, 1st Prize"
	  sg_in2 = "1.Qh3-g3, 158,730"
        sg_board0 = sg_board0 + "----K---  "
        sg_board0 = sg_board0 + "--N-b---  "
        sg_board0 = sg_board0 + "ppr-p-p-  "
        sg_board0 = sg_board0 + "B-----R-  "
        sg_board0 = sg_board0 + "---k----  "
        sg_board0 = sg_board0 + "R--P---Q  "
        sg_board0 = sg_board0 + "----q--n  "
        sg_board0 = sg_board0 + "-n--N--B  "
    endi


    'template below
    $ift s_pick = "9999"
	  sg_in1 = "3"
	  sg_in1 = sg_in1 + " "
	  sg_in2 = "3"
        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

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

    'rotate if needed
    sg_pass1 = sg_board0
    sub_rotate_right
    sg_board0 = sg_pass1

    'initialize the string array
    dg_solutionindex = 1
    dwhi dg_solutionindex <= 1000
	  $toi dg_solutionindex, ""
	  dinc dg_solutionindex
    endw
    dg_solutionindex = 0

    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_movecount2 = 0
    dg_movecount3 = 0
    dg_movecount4 = 0
    dg_movecount5 = 0
    dg_movecount6 = 0

    dg_onecount1 = 0
    dg_onecount2 = 0
    dg_onecount3 = 0
    dg_onecount4 = 0
    dg_onecount5 = 0
    dg_onecount6 = 0

    dg_totalmoves1 = 0
    dg_totalmoves2 = 0
    dg_totalmoves3 = 0
    dg_totalmoves4 = 0
    dg_totalmoves5 = 0
    dg_totalmoves6 = 0

    dg_totalmatescount = 0
    sg_score = ""
ends sub_initialize


subr sub_board
'updated 2000/09/25
    vari d_any, s_any, d_dot, s_dot
    vari s_rank, d_rank, d_file, s_left
    vari s_board

    s_board = sg_pass1
    dch$ s_left, 32, 10

    $out sg_in1
    $out sg_in2

    '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
		$app s_rank, s_dot + " "

		dinc d_file
	  endw

        $out s_rank
        ddec d_rank
    endw 
ends sub_board


subr sub_process
'updated 2002/03/06
    vari s_any, d_any, s_dot, d_dot
    vari d_seconds, d_minutes, s_board, d_loop

    sub_initialize

    'show the board
    sg_pass1 = sg_board0
    sub_board

    dsec d_seconds

    dg_ply = 0
    s_board = sg_board0
    sg_pass1 = s_board
    sub_wmove

    dsec d_any
    d_seconds = d_any - d_seconds
    d_minutes = d_seconds / 60

    d_loop = 1
    dwhi d_loop = 1
	  sg_pass1 = sg_board0
	  sub_board

	  $out sg_in1
	  $out sg_in2
 
        'output results
        ded$ s_any, dg_totalmoves1, 9, 0
        $out "total 1-ply positions = " + s_any

        ded$ s_any, dg_totalmoves2, 9, 0
        $out "total 2-ply positions = " + s_any

        ded$ s_any, dg_totalmoves3, 9, 0
        $out "total 3-ply positions = " + s_any

        ded$ s_any, dg_totalmoves4, 9, 0
        $out "total 4-ply positions = " + s_any

        ded$ s_any, dg_totalmoves5, 9, 0
        $out "total 5-ply positions = " + s_any + " *** value"

        ded$ s_any, dg_totalmoves6, 9, 0
        $out "total 6-ply positions = " + s_any

        ded$ s_any, dg_totalmatescount, 9, 0
        $out "total 5-ply mates     = " + s_any

        dsec d_any
        d_seconds = d_any - d_seconds

        $out "The solution follows:"

        'output all of the solutions
        d_dot = 1
        dwhi d_dot <= dg_solutionindex
            ito$ s_dot, d_dot
	      $out "Solution=" + s_dot

	      dinc d_dot
        endw
        ded$ s_any, d_minutes, 9, 3
	  $out "minutes = " + s_any

        $inp s_any, "return, * to end"
	  $ift s_any = "*": dinc d_loop
    endw
ends sub_process


subr sub_wmove
'updated 2002/03/02
    'White move
    vari d_any, s_any, d_dot, s_dot
    vari s_board, d_movefrom, s_moveman, d_value

    s_board = sg_pass1

    dinc dg_ply

    d_value = 2
    d_movefrom = 111
    dwhi d_movefrom <= 188

	  $cut s_moveman, s_board, d_movefrom, 1
	  $lok d_any, sg_menwhite, 1, s_moveman

	  dift d_any > 0
	      dg_pass1 = d_movefrom
	      sg_pass1 = s_board

		$ift s_moveman = "P": sub_wpawn
		$ift s_moveman = "N": sub_wknight
		$ift s_moveman = "B": sub_wbishop
		$ift s_moveman = "R": sub_wrook
		$ift s_moveman = "Q": sub_wqueen
		$ift s_moveman = "K": sub_wking
	  endi

	  dinc d_movefrom
    endw
    ddec dg_ply
ends sub_wmove


subr sub_wpawn
'updated 2002/03/02
    'White Pawn move
    vari d_any, s_any, d_dot, s_dot
    vari s_board, d_movefrom, d_moveto, s_moveto
    vari s_promote
    vari d_index, d_delta, d_torank

    d_movefrom = dg_pass1
    s_board = sg_pass1

    'one move forward
    d_moveto = d_movefrom + 1
    d_torank = d_moveto % 10
    $cut s_moveto, s_board, d_moveto, 1

    $ift s_moveto = "-"
	  dift d_torank = 8
		s_promote = "N"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "N"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "N"
	      sub_test_wmove

		s_promote = "B"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "B"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "B"
	      sub_test_wmove

		s_promote = "R"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "R"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "R"
	      sub_test_wmove

		s_promote = "Q"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "Q"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "Q"
	      sub_test_wmove
	  else
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "P"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto 
	      sub_test_wmove
	  endi

	  'two moves forward
	  d_any = d_movefrom % 10

	  dift d_any = 2
            d_moveto = d_movefrom + 2
            $cut s_moveto, s_board, d_moveto, 1

            $ift s_moveto = "-"
	          sg_pass1 = s_board
	          $rep sg_pass1, d_moveto, "P"
	          $rep sg_pass1, d_movefrom, "-"
		    sg_pass2 = "P" + d_movefrom + d_moveto 
	          sub_test_wmove
		endi
	  endi
    endi

    'capture 44 to 35
    d_moveto = d_movefrom - 9
    $cut s_moveto, s_board, d_moveto, 1
    $lok d_any, sg_menblack, 1, s_moveto

    dift d_any > 0
	  dift d_torank = 8
		s_promote = "N"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "N"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "N"
	      sub_test_wmove

		s_promote = "B"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "B"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "B"
	      sub_test_wmove

		s_promote = "R"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "R"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "R"
	      sub_test_wmove

		s_promote = "Q"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "Q"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "Q"
	      sub_test_wmove
	  else
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "P"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto
	      sub_test_wmove
	  endi
    endi

    'capture 44 to 55
    d_moveto = d_movefrom + 11
    $cut s_moveto, s_board, d_moveto, 1
    $lok d_any, sg_menblack, 1, s_moveto

    dift d_any > 0
	  dift d_torank = 8
		s_promote = "N"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "N"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "N"
	      sub_test_wmove

		s_promote = "B"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "B"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "B"
	      sub_test_wmove

		s_promote = "R"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "R"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "R"
	      sub_test_wmove

		s_promote = "Q"
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "Q"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto + "Q"
	      sub_test_wmove
	  else
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "P"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "P" + d_movefrom + d_moveto
	      sub_test_wmove
	  endi
    endi
ends sub_wpawn


subr sub_wknight
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  d_moveto = d_movefrom + d_delta

	  $cut s_moveto, s_board, d_moveto, 1
	  $lok d_any, sg_tobywhite, 1, s_moveto

	  dift d_any > 0
		sg_pass1 = s_board
		$rep sg_pass1, d_moveto, "N"
		$rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "N" + d_movefrom + d_moveto
		sub_test_wmove
	  endi

	  dinc d_index
    endw
ends sub_wknight


subr sub_wbishop
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  d_moveto = d_movefrom

	  d_loop = 1
	  dwhi d_loop = 1
	      d_moveto = d_moveto + d_delta

	      $cut s_moveto, s_board, d_moveto, 1
	      $lok d_any, sg_tobywhite, 1, s_moveto

		dift d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_pass1 = s_board
		    $rep sg_pass1, d_moveto, "B"
		    $rep sg_pass1, d_movefrom, "-"
		    sg_pass2 = "B" + d_movefrom + d_moveto
		    sub_test_wmove
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_wbishop


subr sub_wrook
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  d_moveto = d_movefrom

	  d_loop = 1
	  dwhi d_loop = 1
	      d_moveto = d_moveto + d_delta

	      $cut s_moveto, s_board, d_moveto, 1
	      $lok d_any, sg_tobywhite, 1, s_moveto

		dift d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_pass1 = s_board
		    $rep sg_pass1, d_moveto, "R"
		    $rep sg_pass1, d_movefrom, "-"
		    sg_pass2 = "R" + d_movefrom + d_moveto
		    sub_test_wmove
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_wrook


subr sub_wqueen
'updated 2000/09/25
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  d_moveto = d_movefrom

	  d_loop = 1
	  dwhi d_loop = 1
	      d_moveto = d_moveto + d_delta

	      $cut s_moveto, s_board, d_moveto, 1
	      $lok d_any, sg_tobywhite, 1, s_moveto

		dift d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_pass1 = s_board
		    $rep sg_pass1, d_moveto, "Q"
		    $rep sg_pass1, d_movefrom, "-"
		    sg_pass2 = "Q" + d_movefrom + d_moveto
		    sub_test_wmove
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_wqueen


subr sub_wking
'updated 2000/09/25
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  d_moveto = d_movefrom + d_delta

	  $cut s_moveto, s_board, d_moveto, 1
	  $lok d_any, sg_tobywhite, 1, s_moveto

	  dift d_any > 0
		sg_pass1 = s_board
		$rep sg_pass1, d_moveto, "K"
		$rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "K" + d_movefrom + d_moveto
		sub_test_wmove
	  endi

	  dinc d_index
    endw
ends sub_wking


subr sub_test_wmove
'updated 2002/03/03
    vari d_any, s_any, d_dot, s_dot
    vari s_board, s_lastmove, d_bincheck, d_testmore

    d_testmore = 1

    s_lastmove = sg_pass2
    s_board = sg_pass1
    sub_wincheck
    dift dg_pass1 = 1: dinc d_testmore

    dift dg_ply = 3
	  dift dg_value3 = 1: dinc d_testmore
    endi
    dift dg_ply = 5
	  dift dg_value5 = 1: dinc d_testmore
    endi

    dift d_testmore = 1
	  'is black in check
	  sg_pass1 = s_board
	  sub_bincheck
	  d_bincheck = dg_pass1

	  'K151161
	  sg_pass1 = s_lastmove
	  sub_to_algebra
	  s_lastmove = sg_pass1

	  dift dg_ply = 1
		sg_move1 = s_lastmove
		dift dg_movestoshow > 0: $out sg_move1

		dinc dg_movecount1
		dinc dg_totalmoves1

		'get black move2
		dg_value2 = 2
		dg_movecount2 = 0
		sg_pass1 = s_board
		sub_bmove

		dift dg_movecount2 = 0
		    dift d_bincheck <> 1: dg_value2 = 1
		endi

		dift dg_movecount1 = 1
		    fapp d_any, sg_outputfile, sg_in1
		endi

	      s_any = sg_move1 + " " + sg_move2 
	      $app s_any, ", ply2=" + dg_onecount2 
	      $app s_any, ", ply3=" + dg_onecount3 
	      $app s_any, ", ply4=" + dg_onecount4  
	      $app s_any, ", ply5=" + dg_onecount5
	      $app s_any, ", ply6=" + dg_onecount6
	      $out s_any

		fapp d_any, sg_outputfile, s_any

		dg_onecount2 = 0
		dg_onecount3 = 0
		dg_onecount4 = 0
		dg_onecount5 = 0
		dg_onecount6 = 0

		dg_value1 = 2
		dift dg_value2 <> 1: dg_value1 = 1

		dift dg_value1 = 1
		    s_any = sg_move1 + " wins"
		    $out s_any
		    fapp d_any, sg_outputfile, s_any

		    dinc dg_solutionindex
		    $toi dg_solutionindex, sg_move1
		endi
	  endi
	  dift dg_ply = 3
		sg_move3 = s_lastmove
		dift dg_movestoshow > 2
		    s_any = sg_move1 + " " + sg_move2 + " " + sg_move3
		    $out s_any
		endi

		dinc dg_movecount3
		dinc dg_onecount3
		dinc dg_totalmoves3

		'get black move4
		dg_value4 = 2
		dg_movecount4 = 0
		sg_pass1 = s_board
		sub_bmove

		dift dg_movecount4 = 0
		    dift d_bincheck <> 1: dg_value4 = 1
		endi

		dift dg_value4 <> 1: dg_value3 = 1
	  endi
	  dift dg_ply = 5
		sg_move5 = s_lastmove
		dift dg_movestoshow > 4
		    s_any = sg_move1 + " " + sg_move2 + " " + sg_move3
		    $app s_any, " " + sg_move4 + " " + sg_move5
		    $out s_any
		endi

		dinc dg_movecount5
		dinc dg_onecount5
		dinc dg_totalmoves5

		dift d_bincheck = 1
		    'get black move6
		    dg_value6 = 2
		    dg_movecount6 = 0
		    sg_pass1 = s_board
		    sub_bmove

		    dift dg_value6 <> 1
			  dg_value5 = 1
			  dinc dg_totalmatescount
		    endi
		endi
	  endi
    endi
ends sub_test_wmove


subr sub_bmove
'updated 2002/03/02
    'board is s_board
    vari d_any, s_any, d_dot, s_dot
    vari s_board, d_movefrom, s_moveman

    s_board = sg_pass1

    dinc dg_ply

    d_movefrom = 111
    dwhi d_movefrom <= 188
	  $cut s_moveman, s_board, d_movefrom, 1
	  $lok d_any, sg_menblack, 1, s_moveman
	  dift d_any > 0
		dg_pass1 = d_movefrom
		sg_pass1 = s_board

		$ift s_moveman = "p": sub_bpawn
		$ift s_moveman = "n": sub_bknight
		$ift s_moveman = "b": sub_bbishop
		$ift s_moveman = "r": sub_brook
		$ift s_moveman = "q": sub_bqueen
		$ift s_moveman = "k": sub_bking
	  endi

	  dinc d_movefrom
    endw
    ddec dg_ply
ends sub_bmove


subr sub_bpawn
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_torank
    vari s_board, d_movefrom, d_moveto, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    'one move forward
    d_moveto = d_movefrom - 1
    d_torank = d_moveto % 10
    $cut s_moveto, s_board, d_moveto, 1

    $ift s_moveto = "-"
	  dift d_torank = 1
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "n"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "n"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "b"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "b"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "r"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "r"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "q"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "q"
	      sub_test_bmove
	  else
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "p"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto
	      sub_test_bmove
	  endi

	  'two moves forward
	  d_any = d_movefrom % 10

	  dift d_any = 7
            d_moveto = d_movefrom - 2
            $cut s_moveto, s_board, d_moveto, 1

            $ift s_moveto = "-"
	          sg_pass1 = s_board
	          $rep sg_pass1, d_moveto, "p"
	          $rep sg_pass1, d_movefrom, "-"
		    sg_pass2 = "p" + d_movefrom + d_moveto
	          sub_test_bmove
		endi
	  endi
    endi

    'capture 45 to 34
    d_moveto = d_movefrom - 11
    $cut s_moveto, s_board, d_moveto, 1
    $lok d_any, sg_menwhite, 1, s_moveto

    dift d_any > 0
	  dift d_torank = 1
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "n"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "n"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "b"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "b"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "r"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "r"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "q"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "q"
	      sub_test_bmove
	  else
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "p"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto
	      sub_test_bmove
	  endi
    endi

    'capture 45 to 54
    d_moveto = d_movefrom + 9
    $cut s_moveto, s_board, d_moveto, 1
    $lok d_any, sg_menwhite, 1, s_moveto

    dift d_any > 0
	  dift d_torank = 1
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "n"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "n"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "b"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "b"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "r"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "r"
	      sub_test_bmove

	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "q"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto + "q"
	      sub_test_bmove
	  else
	      sg_pass1 = s_board
	      $rep sg_pass1, d_moveto, "p"
	      $rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "p" + d_movefrom + d_moveto
	      sub_test_bmove
	  endi
    endi
ends sub_bpawn


subr sub_bknight
'updated 2002/03/02
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 91
    dwhi d_index <= 98
	  itod d_delta, d_index
	  d_moveto = d_movefrom + d_delta

	  $cut s_moveto, s_board, d_moveto, 1
	  $lok d_any, sg_tobyblack, 1, s_moveto

	  dift d_any > 0
		sg_pass1 = s_board
		$rep sg_pass1, d_moveto, "n"
		$rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "n" + d_movefrom + d_moveto 
		sub_test_bmove
	  endi

	  dinc d_index
    endw
ends sub_bknight


subr sub_bbishop
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 85
    dwhi d_index <= 88
	  itod d_delta, d_index
	  d_moveto = d_movefrom

	  d_loop = 1
	  dwhi d_loop = 1
	      d_moveto = d_moveto + d_delta

	      $cut s_moveto, s_board, d_moveto, 1
	      $lok d_any, sg_tobyblack, 1, s_moveto

		dift d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_pass1 = s_board
		    $rep sg_pass1, d_moveto, "b"
		    $rep sg_pass1, d_movefrom, "-"
 		    sg_pass2 = "b" + d_movefrom + d_moveto 
		    sub_test_bmove
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bbishop


subr sub_brook
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 81
    dwhi d_index <= 84
	  itod d_delta, d_index
	  d_moveto = d_movefrom

	  d_loop = 1
	  dwhi d_loop = 1
	      d_moveto = d_moveto + d_delta

	      $cut s_moveto, s_board, d_moveto, 1
	      $lok d_any, sg_tobyblack, 1, s_moveto

		dift d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_pass1 = s_board
		    $rep sg_pass1, d_moveto, "r"
		    $rep sg_pass1, d_movefrom, "-"
  		    sg_pass2 = "r" + d_movefrom + d_moveto 
		    sub_test_bmove
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_brook


subr sub_bqueen
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta, d_loop
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  d_moveto = d_movefrom

	  d_loop = 1
	  dwhi d_loop = 1
	      d_moveto = d_moveto + d_delta

	      $cut s_moveto, s_board, d_moveto, 1
	      $lok d_any, sg_tobyblack, 1, s_moveto

		dift d_any > 1: dinc d_loop
	      dift d_any > 0
		    sg_pass1 = s_board
		    $rep sg_pass1, d_moveto, "q"
		    $rep sg_pass1, d_movefrom, "-"
		    sg_pass2 = "q" + d_movefrom + d_moveto 
		    sub_test_bmove
		else
		    dinc d_loop
		endi
	  endw

	  dinc d_index
    endw
ends sub_bqueen


subr sub_bking
'updated 2002/03/02
    'board is in s_board, d_movefrom
    vari d_any, s_any, d_dot, s_dot
    vari d_index, d_delta
    vari s_board, d_moveto, d_movefrom, s_moveto

    s_board = sg_pass1
    d_movefrom = dg_pass1

    d_index = 81
    dwhi d_index <= 88
	  itod d_delta, d_index
	  d_moveto = d_movefrom + d_delta

	  $cut s_moveto, s_board, d_moveto, 1
	  $lok d_any, sg_tobyblack, 1, s_moveto

	  dift d_any > 0
		sg_pass1 = s_board
		$rep sg_pass1, d_moveto, "k"
		$rep sg_pass1, d_movefrom, "-"
		sg_pass2 = "k" + d_movefrom + d_moveto 
		sub_test_bmove
	  endi

	  dinc d_index
    endw
ends sub_bking


subr sub_test_bmove
'updated 2002/03/03
    vari d_any, s_any, d_dot, s_dot
    vari s_board, s_lastmove, d_wincheck, d_testmore

    d_testmore = 1

    s_lastmove = sg_pass2
    s_board = sg_pass1
    sub_bincheck
    dift dg_pass1 = 1: dinc d_testmore

    dift d_testmore = 1
	  dift dg_ply = 2
		dift dg_value2 = 1: dinc d_testmore
	  endi
	  dift dg_ply = 4
		dift dg_value4 = 1: dinc d_testmore
	  endi
    endi
    dift d_testmore = 1
	  'k158157
	  sg_pass1 = s_lastmove
	  sub_to_algebra
	  s_lastmove = sg_pass1

	  dift dg_ply = 2
		sg_move2 = s_lastmove
		dift dg_movestoshow > 1: $out sg_move1 + " " + sg_move2

		dinc dg_movecount2
		dinc dg_onecount2
		dinc dg_totalmoves2

		'get white's move3
		dg_movecount3 = 0
		dg_value3 = 2
		sg_pass1 = s_board
	      sub_wmove

		dift dg_value3 <> 1: dg_value2 = 1
	  endi
	  dift dg_ply = 4
		sg_move4 = s_lastmove
		dift dg_movestoshow > 3
		    s_any = sg_move1 + " " + sg_move2 + " " 
		    $app s_any, sg_move3 + " " + sg_move4
		    $out s_any
		endi

		dinc dg_movecount4
		dinc dg_onecount4
		dinc dg_totalmoves4

		'get white's move5
		dg_movecount5 = 0
		dg_value5 = 2
		sg_pass1 = s_board
	      sub_wmove

		dift dg_value5 <> 1: dg_value4 = 1
	  endi
	  dift dg_ply = 6
		dg_value6 = 1
		dinc dg_movecount6
		dinc dg_onecount6
		dinc dg_totalmoves6
	  endi
    endi
ends sub_test_bmove


subr sub_wincheck
'updated 2000/09/26
    'is White in check
    vari d_any, s_any, d_dot, s_dot
    vari d_loop, d_count
    vari d_index, d_delta, d_wking, d_wincheck
    vari s_board, d_square, s_square

    s_board = sg_pass1

    d_wincheck = 2
    $lok d_wking, s_board, 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, s_board, d_square, 1
	  $ift s_square = "n": d_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, s_board, d_square, 1
		$ift s_square <> "-": dinc d_loop
	  endw
	  dift d_count = 1
		'Black Pawn or Black King
		$ift s_square = "k": d_wincheck = 1

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

	  dinc d_index
    endw
    dg_pass1 = d_wincheck
ends sub_wincheck


subr sub_bincheck
'updated 2000/09/26
    'is Black in check
    vari d_any, s_any, d_dot, s_dot
    vari d_loop, d_count
    vari d_index, d_delta, d_bking, d_bincheck
    vari s_board, d_square, s_square

    s_board = sg_pass1

    d_bincheck = 2
    $lok d_bking, s_board, 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, s_board, d_square, 1
	  $ift s_square = "N": d_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, s_board, d_square, 1
		$ift s_square <> "-": dinc d_loop
	  endw
	  dift d_count = 1
		'White Pawn or White King
		$ift s_square = "K": d_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: d_bincheck = 1
		    dift d_delta = 9: d_bincheck = 1
		endi 
	  endi
	  $ift s_square = "Q": d_bincheck = 1
	  $ift s_square = "R"
		dift d_index <= 84: d_bincheck = 1
	  endi
	  $ift s_square = "B"
		dift d_index >= 85: d_bincheck = 1
	  endi

	  dinc d_index
    endw
    dg_pass1 = d_bincheck
ends sub_bincheck


subr sub_to_algebra
'updated 2002/03/02
    vari d_any, s_any, d_dot, s_dot
    vari s_lastmove

    s_lastmove = sg_pass1

    'K151162
    $del s_lastmove, 2, 1

    'K51162
    $rep s_lastmove, 4, "-"

    'K51-62
    $cut s_any, s_lastmove, 2, 1
    $tod d_any, s_any
    $cut s_any, sg_abcdefgh, d_any, 1
    $rep s_lastmove, 2, s_any

    'Ke1-62
    $cut s_any, s_lastmove, 5, 1
    $tod d_any, s_any
    $cut s_any, sg_abcdefgh, d_any, 1
    $rep s_lastmove, 5, s_any
    'Ke1-f2

    sg_pass1 = dg_ply + "." + s_lastmove
ends sub_to_algebra


subr sub_rotate_right
'updated 2000/04/20
'rotate the board to the right
    vari d_any, s_any, d_dot, s_dot
    vari d_loop
    vari s_board1, s_board2, d_file1, d_rank1, d_file2, d_rank2

    s_board1 = sg_pass1
    dch$ s_board2, 32, 222
    d_file1 = 1
    dwhi d_file1 <= 8
	  d_rank1 = 1
	  dwhi d_rank1 <= 8
		'file 1 becomes rank 8
		'file 2 becomes rank 7
		'file 8 becomes rank 1
		d_rank2 = 9 - d_file1
		d_file2 = d_rank1

		d_dot = 10 * d_file1 + d_rank1 + 100
		$cut s_dot, s_board1, d_dot, 1

		d_dot = 10 * d_file2 + d_rank2 + 100
		$rep s_board2, d_dot, s_dot

		dinc d_rank1
	  endw
	  dinc d_file1
    endw
    sg_pass1 = s_board2
ends sub_rotate_right


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