%%%%%%%%%% % move_t % %%%%%%%%%% move_t = cluster is create, parse, unparse, get_from, get_to, equal % Overview: A move provides an immutable representation for a % single piece movement. rep = struct[from,to:place_t] % Abstraction function: % A(r) represents a move on a board supported % by the place_t type, from the place r.from to the % place r.to. % Rep invariant: true create = proc(from, to: place_t) returns (cvt) % effects: Returns the move from "from" to "to". return(rep${from:from,to:to}) end create parse = proc(s: string) returns (cvt) signals (invalid) % effects: Returns the move represented in the form "a1-b2" % by s. the column letter can be either an upper % case or lower case letter Signals invalid if the % format is not valid for a move. if (string$size(s) = 5) & (string$fetch(s,3) = '-') then begin from : place_t := place_t$parse(string$substr(s,1,2)) to : place_t := place_t$parse(string$substr(s,4,2)) return(rep${from:from,to:to}) end resignal invalid end signal invalid end parse unparse = proc(m: cvt) returns (string) % effects: Returns the textual representation of the move, % i.e., "a1-b2", the column character will be a % lowercase letter. from_str : string := place_t$unparse(m.from) to_str : string := place_t$unparse(m.to) return((from_str || "-") || to_str) end unparse get_from = proc(m: cvt) returns (place_t) % effects: Returns the place to be moved from. return(m.from) end get_from get_to = proc(m: cvt) returns (place_t) % effects: Returns the place to be moved to. return(m.to) end get_to equal = proc(m1,m2:cvt) returns(bool) % effects: Returns true if m1 and m2 represent the same % chess move. return((m1.from = m2.from) & (m1.to = m2.to)) end equal end move_t