%%%%%%%%%%% % place_t % %%%%%%%%%%% place_t = cluster is create, parse, unparse, get_row, get_col, equal %Overview: A place represents a cell on the board. It numbers % the lower-left corner row 1, column 1. Places are % immutable. rep = struct[row:int,col:int] % Abstraction Function: % places are represented by a row r.row and a % column r.col % where 1<= r.row <= 8, 1 <= r.col <= 8 % Rep invariant: 1 <= row <= 8, 1 <= col <= 8 create = proc(row, col: int) returns (cvt) signals (invalid) % effects: Returns a new place according to row and col. % Signals invalid if the coordinates are invalid, % i.e., not 1 <= row, col <= 8. if (row >= 1) & (row <= 8) & (col >= 1) & (col <= 8) then return(rep${row:row,col:col}) else signal invalid end end create parse = proc(s: string) returns (cvt) signals (invalid) % effects: Parses a place string of the form "g3" and % returns a place for it. The column may be % either an upper case or lower case letter. % Signals invalid if the string is not of the % correct form or if the coordinates are not % in the range a,A <= col <= h,H, 1 <= row <= 8 a_int : int := char$c2i('a') a_cap_int : int := char$c2i('A') if (string$size(s) = 2) then col : int := char$c2i(string$fetch(s,1)) row : int := int$parse(string$substr(s,2,1)) except when bad_format, overflow: signal invalid end if (row >= 1) & (row <= 8) then if (col >= a_int) & (col <= a_int + 7) then ret : rep := rep${row:row,col:(col - a_int + 1)} return(ret) else if (col >= a_cap_int) & (col <= a_cap_int + 7) then ret : rep := rep${row:row,col:(col - a_cap_int + 1)} return(ret) end end end end signal invalid end parse unparse = proc(p: cvt) returns (string) % effects: Returns the textual representation of the place % on the board. a_int : int := char$c2i('a') col_str : string := string$c2s(char$i2c(a_int -1 + p.col)) return(col_str || int$unparse(p.row)) end unparse get_row = proc(p: cvt) returns (int) % effects: Returns the row number of the place. return(p.row) end get_row get_col = proc(p: cvt) returns (int) % effects: Returns the column number of the place. return(p.col) end get_col equal = proc(p1,p2:cvt) returns(bool) % effects: Returns true if p1 and p2 represent the same % cell on the board. return((p1.row = p2.row) & (p1.col = p2.col)) end equal end place_t