chess_board
= cluster is create, pieces, board_location, spot_empty, make_move, move_legal, undo_move, parse, unparse, all_possible_moves, can_move,
count_possible_moves, set_teleport, get_teleport, equal, lt
- Overview
- chess_board is a mutable data type which represents a chess board for anti-chess. It keeps track of the location of all the pieces, checks for the legality of the moves, and provides the capability to undo moves. It also has some utility procedures which are of use to computer strategy algorithms.
- written by stevenj, twm
- create = proc() returns(cvt)
- effects: returns a new board with all the pieces in their initial positions for a chess/antichess game
- set_teleport = proc(b: cvt, newtel:teleportState) signals(illegal_location)
- effects: Sets the teleport state to newtel. If the state is not off, and if the locations are not distinct or are not legal board locations, signals illegal_location.
- get_teleport = proc(b: cvt) returns(teleportState)
- effects: returns the current teleport state of the board
- pieces = iter(b: cvt) yields(pieceColor,pieceKind,int,int)
- effects: this iterator yields all the pieces on the chess board one by one. The pieceColor is whitePiece or blackPiece, depending upon whether a piece is white or black. The pieceKind tells what kind of piece it is (rook, pawn, etc.). The two integers are the row and column number of the piece's location, in that order. Row and column numbers run from 1 to 8, and start at the lower-left corner of the board.
- board_location = proc(b: cvt, row,col: int) returns(pieceColor,pieceKind) signals(no_piece, illegal_location)
- effects: Tells what kind of piece is located at the specified row and column of the board (rows/columns numbered as specified in the pieces iterator). The pieceColor is whitePiece or blackPiece depending upon whether the piece is white or black, and the pieceKind tells what sort of piece it is. If there is no piece at that location, signals no_piece. If the row or col is not in the legal range, signals illegal_location.
- spot_empty = proc(b:cvt, r,c:int) returns(bool) signals(no_piece, illegal_location)
- effects: returns true iff there is no piece at the location on the board given by r and c. Signals illegal_location if the location (r,c) is not on the board.
- can_move = proc(b: cvt, color: pieceColor) returns(bool)
- effects: returns true iff there is some legal move that can be made by the pieces of color "color".
- move_legal = proc(b: cvt, cur_color: pieceColor, m:movedata) returns(bool,string)
signals(no_piece,illegal_location,wrongColor)
- effects: returns true iff moving the piece at the location (m.fromrow,m.fromcol) to (m.torow,m.tocol) is legal. If the move is not legal, returns a descriptive string. Signals illegal_location if the the specified locations are not on the board. Signals no_piece if there is no piece at location (m.fromrow,m.fromcol) on the board. Signals wrongColor if the piece being moved isn't of color cur_color.
- make_move = proc(b:cvt, cur_color: pieceColor, m:moveData) returns(undoMoveInfo)
signals(no_piece,illegal_move(string),illegal_location,wrongColor)
- modifies: b
- effects: Moves a piece according to the move data in m. If there is no such piece, signals no_piece. If the move is illegal, signals illegal_move and includes a string telling what went wrong. If either of the two locations is not on the board, then signals illegal_location. Signals wrongColor if the color of the piece being moved is not equal to the color cur_color. Note that any enemy piece at (m.torow,m.tocol) is taken. The return value is the information needed to undo the move. Note: will queen pawns, if possible.
- undo_move = proc(b: cvt, uinfo: undoMoveInfo)
- requires: uinfo must be the undoMoveInfo returned by the most recent call to make_move
- modifies: b
- effects: will undo the effects of the most recent call to make_move
- parse = proc(b_string: string) returns(cvt) signals(bad_format(string))
- effects: parse takes a string representing a game board, as described by the format on page 9 of problem set #5, and returns the associated chess board object. Signals bad_format if the format is wrong, returns a descriptive error string.
- unparse = proc(b: cvt) returns(string)
- effects: returns a string representing a game board, as described by the format on page 9 of problem set #5, given a chess board object b.
- all_possible_moves = iter(b: cvt, pcolor:pieceColor) yields(int,int,int,int,undoMoveInfo)
- requires: When control reaches the end of the body of the for loop calling this iterator, the board must be in the state it was in at the beginning of the body of the for loop. i.e. Any moves which were made in the body must be undone!
- modifies: b
- effects: Finds all possible moves that may be made by the player whose pieces are the color "pcolor". One by one it makes them, yielding the row and column of the original piece location, the row and column of the new location, and the information necessary to undo the move. The move is undone automatically when control reaches the end of the body of the for loop, however! The undo information is only provided in case the caller breaks out of the for loop and wants to undo the move. As can be seen, this iterator must be called with care; its interface is the way it is because it is designed to be called in a min-max search for the best move, and has to be efficient.
- count_possible_moves = proc(b:cvt, pcolor:pieceColor) returns(int)
- effects: returns the total number of moves that the player of color pcolor can currently make on the chess board b
- equal = proc(b1, b2: cvt) returns(bool)
- effects: Returns whether the current board configuration of b1 matches the current board configuration of b2. Note - this procedure does not distinguish between natural queens and queened pawns, and therefore two boards with different pieces could be considered equal, but they still must have the same "look" when unparsed. Different teleport states will cause false to be returned.
- lt = proc(b1, b2: cvt) returns(bool)
- effects: This procedure can be used to order two chess_boards. The result has not physical significance per se, but for two chess_boards, b1 and b2, that satisfy b1 ~= b2, (b1 < b2) = ~(b2 < b1) always holds true and (b1 < b2) will always return the same value. (For b1 = b2, b1 < b2 returns false and b2 < b1 returns false). Note - this procedure does not distinguish between natural queens and queened pawns, and therefore two boards with different pieces could be considered equal, but they still must have the same "look" when unparsed. Different teleport states are distinguished.