def addtomovesifcanenpassant(pos, thelist):
            """Determines if the piece at pos can en passant."""
            if colour == 'white': capturerank = 5
            else: capturerank = 2

            square = self.board[pos]
            if square == None:
                return thelist
            elif square.type == pieces.PawnPiece:
                if square.colour == colour:
                    startindex = core.convert(pos, toindex=True)
                    endindex = core.convert((capturerank, file_), toindex=True)
                    thelist.append((startindex, endindex))
            return thelist
Example #2
0
def pawnpromotion(movetuple, colour):
    """Determines if the move was a promotion move."""
    finalcoord = core.convert(movetuple[1], tocoordinate=True)
    finalrank = finalcoord[0]

    if colour == 'white' and finalrank == 7: return True
    elif colour == 'black' and finalrank == 0: return True
    else: return False
    def generateasciiboard(self, board, side):
        """Draws the ascii board."""
        def insertsymbolinrankstring(symbol, position, rankstr):
            """Inserts the character into position."""
            ranklist = list(rankstr)
            ranklist[position] = symbol
            rankstr = reduce(lambda x, y: x + y, ranklist)
            return rankstr

        # Sanity checks.
        try:
            assert side in ('white', 'black')
        except AssertionError:
            raise TypeError(
                "The parameter 'side' must be either 'white' or 'black'")

        # Start by drawing an empty board.
        rankstrings = ['........'] * 8

        # Assign piece symbols to the undecorated board.
        for ii, square in enumerate(board):
            if square == None:
                continue
            else:
                piece = square
                symbol = piece.notationsymbol
                (rank_, file_) = core.convert(ii, tocoordinate=True)

                rankstrings[rank_] = \
                    insertsymbolinrankstring(symbol, file_, rankstrings[rank_])

        # Determine which way to print the board.
        if side == 'white':
            rankstrings = rankstrings[::-1]
            rankcoordinates = self.ranks[::-1]
            filecoordinates = self.files
        elif side == 'black':
            for ii in range(len(rankstrings)):  # HACK: Reverse ranks too.
                rankstrings[ii] = rankstrings[ii][::-1]
            rankcoordinates = self.ranks
            filecoordinates = self.files[::-1]

        # Now decorate board.
        board = reduce(lambda x, y: x+y, map(
            lambda rankstr, ranknotation: \
                (' ' + ranknotation + self.leftedgeborder +
                rankstr +
                self.rightedgeborder),
            rankstrings, rankcoordinates))
        board = (self.topborder + board + self.bottomborder + '    ' +
                 filecoordinates + '\n')
        return board
Example #4
0
    def __getitem__(self, pos):
        """Controls calling the piece at a position on the board like a list."""

        errormsg = "The board is read either as a index from 0 to 63 or a " \
        "tuple/list that specifies the row and column index."
        try:
            position = core.convert(pos, toindex=True)
            return self._board[position]
        except IndexError:
            raise IndexError(errormsg)
        except TypeError:
            raise TypeError(
                errormsg)  # If the pos is wrong type, raise TypeError.
Example #5
0
 def __setitem__(self, pos, piece):
     """Add a piece on the board at pos."""
     errormsg = ("Please pass a position on the board and a piece that "
                 "derives from BasePiece")
     try:
         if piece != None:
             assert isinstance(piece, pieces.BasePiece), errormsg
         position = core.convert(pos, toindex=True)
         self._board[position] = piece
     except AssertionError:
         raise TypeError(errormsg)
     except IndexError:
         raise IndexError(errormsg)
     except TypeError:
         raise TypeError(errormsg)
     else:
         return None
 def getpositionstring(pos):
     (rank_, file_) = core.convert(pos, tocoordinate=True)
     return self._filesymbols[file_] + self._ranksymbols[rank_]
Example #7
0
 def test_convertfunction_vectortovector(self):
     finalpos = core.convert(self.vectorpos, tovector=True)
     self.assertEqual(finalpos, self.vectorpos,
                      errormessage(finalpos, self.vectorpos))
     return None
Example #8
0
 def test_convertfunction_coordtocoord(self):
     finalpos = core.convert(self.tuplepos, tocoordinate=True)
     self.assertEqual(finalpos, self.tuplepos,
                      errormessage(finalpos, self.tuplepos))
     return None
Example #9
0
 def test_convertfunction_indextoindex(self):
     finalpos = core.convert(self.indexpos, toindex=True)
     self.assertEqual(finalpos, self.indexpos,
                      errormessage(finalpos, self.indexpos))
     return None