Version 1 of Play Chess With a WebService

Updated 2003-04-03 19:36:05

MPJ ~ 2003/04/03

After reading a Microsoft case study on .NET on the PocketPc with a chess game (that could also be run on the PC with no code changes)[L1 ]. I remembered Chess in Tcl on the Wiki and figured out how to resize and color it so it looked better on the PocketPC. Then I saw in the article, that the author uses a web service to implete online play against a computer. So I found the service on http://www.xmethods.net [L2 ] and wrote my first TclSOAP interface code ever (took a hour ~ most of the time to read the docs). Then with another couple of lines of code I added online play to Chess in Tcl.

There are a couple of problems because Chess in Tcl does not support all the valid moves but this is my first crack at the code. Please feel free to make changes...

 ### Mike's Add for Smart Network Opponent to Chess in Tcl
 proc InitChessServer {} {
    package require SOAP
    set uri "urn:BorlandChess-IBorlandChess#XML_GetNextMove"
    set proxy "http://www.danmarinescu.com/WebServices/ChessCGIServer.exe/soap/IBorlandChess"
    SOAP::configure \
        -transport http -proxy spider.northgrum.com:80 \
        -headers { "Proxy-Authorization" "Basic amFrZTRjOnBhc3N3b3Jk" } 
    SOAP::create XML_GetNextMove \
        -uri $uri -proxy $proxy \
        -params {"Position" "string" "WhiteMovesNext" "boolean" "SearchDepth" "integer"}
    # example from the danmainescu site
    #set a "rnbqkbnrpppppppp[string repeat { } 32]PPPPPPPPRNBQKBNR"
    #XML_GetNextMove $a true 3  ;# returns "e2e4 OK"
 }

 proc GetNextMove {} {
    global game
    set board {}
    foreach row {8 7 6 5 4 3 2 1} {
        foreach col {A B C D E F G H} {
            if {$game($col$row) == "."} {
                append board " "
            } else {
                append board $game($col$row)
            }
        }
    }
    set result [string toupper [XML_GetNextMove $board false 3]]
    return "[string range [lindex $result 0] 0 1]-[string range [lindex $result 0] 2 3]"
 }
 InitChessServer
 after 1000 CheckBoard
 set game(autoMove) 1
 proc CheckBoard {} {
    global game
    if {$game(toMove) == "black" && $game(autoMove)} {
        set ::game(results) [GetNextMove]
        if {$::game(results) != "A1-A1"} {
            puts [uplevel {chess::move game $::game(results)}]
            game drawBoard .c                
        } else {
            #some prompt that game is over so you want to play somemore
        }
        set game(autoMove) 0
    }
    return [after 1000 CheckBoard]
 }
 proc MoveInfo {- - -} {
    set ::info "$::game(toMove) to move - [chess::values ::game]"
    set ::game(autoMove) 1
 }