ROW = 0 COLUMN = 1 COORDINATE = 0 REPEAT = 1 KING = ' K ' QUEEN = ' Q ' BISHOP = ' B ' KNIGHT = ' N ' ROOK = ' R ' PAWN = ' P ' NOPIECE = ' 0 ' PATH = ' * ' BLACK = 'BLACK' WHITE = 'WHITE' JUMP = True NOJUMP = False NORTH = [-1, 0] SOUTH = [1, 0] EAST = [0, 1] WEST = [0, -1] NORTHEAST = [-1, 1] SOUTHEAST = [1, 1] SOUTHWEST = [1, -1] NORTHWEST = [-1, -1] KNIGHT1 = [-2, 1] KNIGHT2 = [-1, 2] KNIGHT3 = [1, 2] KNIGHT4 = [2, 1] KNIGHT5 = [2, -1] KNIGHT6 = [1, -2] KNIGHT7 = [-1, -2] KNIGHT8 = [-2, -1] UNLIM = True ONESTEP = False BOARDSIZE = 8 class ChessPiece: PIECES = [KING,QUEEN,BISHOP,KNIGHT,ROOK,PAWN] COLORS = [BLACK,WHITE] JUMPS = [JUMP,NOJUMP] UNLIMITED = [UNLIM,ONESTEP] def __init__(self, Piece, RangeOfMotion, Color, Jumps, Position): ''' piece is they type of piece per PIECES list representing the types of chess pieces ROM is the range of motion which is a list containing elements which list the possible single-move displacements along with a true or false for whether the piece can move unlimited in the direction color is the color of the piece per COLORS list representing the two sides of a chess board jumps is whether the piece can jump other pieces per the JUMPs list the knight is the only piece that can jump other pieces position is the coordinate two-element list of the piece position on the board ''' self.piece = Piece self.ROM = RangeOfMotion self.color = Color self.jumps = Jumps self.position = Position def printMoves(self): print self.ROM def returnRow(self): return self.position[0] def returnColumn(self): return self.position[1] class King(ChessPiece): def __init__(self, Color, Position): ChessPiece.__init__(self, KING, [[NORTH,ONESTEP],[NORTHEAST,ONESTEP],[EAST,ONESTEP],[SOUTHEAST,ONESTEP],[SOUTH,ONESTEP],[SOUTHWEST,ONESTEP],[WEST,ONESTEP],[NORTHWEST,ONESTEP]], Color, NOJUMP, Position) class Queen(ChessPiece): def __init__(self, Color, Position): ChessPiece.__init__(self, QUEEN, [[NORTH,UNLIM],[NORTHEAST,UNLIM],[EAST,UNLIM],[SOUTHEAST,UNLIM],[SOUTH,UNLIM],[SOUTHWEST,UNLIM],[WEST,UNLIM],[NORTHWEST,UNLIM]], Color, NOJUMP, Position) class Bishop(ChessPiece): def __init__(self, Color, Position): ChessPiece.__init__(self, BISHOP, [[NORTHEAST,UNLIM],[SOUTHEAST,UNLIM],[SOUTHWEST,UNLIM],[NORTHWEST,UNLIM]], Color, NOJUMP, Position) class Knight(ChessPiece): def __init__(self, Color, Position): ChessPiece.__init__(self, KNIGHT, [[KNIGHT1,ONESTEP],[KNIGHT2,ONESTEP],[KNIGHT3,ONESTEP],[KNIGHT4,ONESTEP],[KNIGHT5,ONESTEP],[KNIGHT6,ONESTEP],[KNIGHT7,ONESTEP],[KNIGHT8,ONESTEP]], Color, JUMP, Position) class Rook(ChessPiece): def __init__(self, Color, Position): ChessPiece.__init__(self, ROOK, [[NORTH,UNLIM],[SOUTH,UNLIM],[EAST,UNLIM],[WEST,UNLIM]], Color, NOJUMP, Position) class Pawn(ChessPiece): def __init__(self, Color, Position): if Color == WHITE: motion = [[SOUTHEAST,ONESTEP],[SOUTHWEST,ONESTEP]] else: motion = [[NORTHEAST,ONESTEP],[NORTHWEST,ONESTEP]] ChessPiece.__init__(self, PAWN, motion, Color, NOJUMP, Position) def printBoard(Board): for i in range(len(Board)): line = '' for j in range(len(Board[i])): line += Board[i][j] print line def changePosition(Board, piecePosition, Character): Board[piecePosition[ROW]-1][piecePosition[COLUMN]-1] = Character def showMoves(individual_piece, Board): piecePosition = individual_piece.position changePosition(Board, piecePosition, individual_piece.piece) for delta in individual_piece.ROM: newRow = piecePosition[ROW] + delta[COORDINATE][ROW] newColumn = piecePosition[COLUMN] + delta[COORDINATE][COLUMN] if delta[REPEAT]: while newRow <= BOARDSIZE and newColumn <= BOARDSIZE and newRow > 0 and newColumn > 0: changePosition(Board, [newRow, newColumn], PATH) newRow += delta[COORDINATE][ROW] newColumn += delta[COORDINATE][COLUMN] else: if newRow <= BOARDSIZE and newColumn <= BOARDSIZE and newRow > 0 and newColumn > 0: changePosition(Board, [newRow, newColumn], PATH) def resetBoard(): board = [] for i in range(BOARDSIZE): line = [] for j in range(BOARDSIZE): line.append(NOPIECE) board.append(line) return board def readBoard(): board = [] print "Welcome to the Check program." print "You will enter characters to simulate the layout on a chess board." print "Enter 8 characters for each row without spaces, followed by <Enter>." print "Enter all 8 rows of the chess board." print "White plays from the top of the board and black from the bottom." print "Enter lowercase letters for the white pieces." print "Enter uppercase letters for the black pieces." print "Enter '0' for blank squares." print "Enter 'K' or 'k' for the king." print "Enter 'Q' or 'q' for the king." print "Enter 'B' or 'b' for the bishop." print "Enter 'N' or 'n' for the knight." print "Enter 'R' or 'r' for the rook." print "Enter 'P' or 'p' for the pawn." rowNum = 1 while rowNum <= BOARDSIZE: row = [] passedValidation = True line = raw_input() if len(line) > 8 or len(line) < 8: print "Line not the correct length. Re-enter it." else: colNum = 1 for c in line: newPiece = " " + c + " " if c == c.lower(): pieceColor = WHITE else: pieceColor = BLACK if newPiece.upper() == KING: row.append(King(pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece.upper() == QUEEN: row.append(Queen(pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece.upper() == BISHOP: row.append(Bishop(pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece.upper() == KNIGHT: row.append(Knight(pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece.upper() == ROOK: row.append(Rook(pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece.upper() == PAWN: row.append(Pawn(pieceColor, [rowNum, colNum])) colNum += 1 elif newPiece.upper() == NOPIECE: colNum += 1 else: print "Incorrect character entered. Please re-enter line." passedValidation = False break if passedValidation: rowNum += 1 for i in range(len(row)): board.append(row[i]) return board pieces = readBoard() board = resetBoard() printBoard(board) print for nextPiece in pieces: dummy = raw_input() board = resetBoard() showMoves(nextPiece, board) printBoard(board)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question