def enter_player_piece(self): """Show selection buttons and return [player_piece, ai_piece].""" render_text("Do you want to be white or black?", colours.text, colours.bg1, center=(centerx, int(height / 2)), border=2, bcolour=colours.bg1) whitebtn = render_text("White", colours.text, colours.bg1, center=(centerx - 60, centery + 40), border=4) blackbtn = render_text("Black", colours.text, colours.bg1, center=(centerx + 60, centery + 40), border=4) while True: self.check_for_quit() button = self.get_button_click(whitebtn, blackbtn) if button == whitebtn: return [Piece(white), Piece(black)] elif button == blackbtn: return [Piece(black), Piece(white)] self.mainclock_tick()
def getThreatened(board, isWhite=True): threat = [] if isWhite: pieces = Piece.blackPieces() else: pieces = Piece.whitePieces() for p in pieces: threat.extend(getHitlistFor(p, board, not isWhite)) s = set(threat) return [Piece.abbreviation(p) for p in s]
def displayPossiblePieces(): """Display all possible pieces, mainly for testing purpose >>> Game.displayPossiblePieces() """ for c in PIECES: print(Piece(c)) print('--------------')
def determinePiece(self): self.dest = self.board.stringToLoc(self.dest) possible = Piece.piecesForType(self.pieceType, self.isWhite) if possible: for p in possible: loc = self.board.locationOfPiece(p) if loc == None: self.notes += 'Error: piece location not found\n' continue if self.piece: if self.piece.isalpha(): #in column if loc[0] != ord(self.piece) - 97: continue else: #in row if loc[1] != int(self.piece) - 1: continue if self.pieceType == 'R': if (loc[0] == self.dest[0] or loc[1] == self.dest[1]): self.piece = p self.location = loc break elif self.pieceType == 'N': if ((loc[0] + 2 == self.dest[0] and (loc[1] + 1 == self.dest[1] or loc[1] - 1 == self.dest[1])) or (loc[0] - 2 == self.dest[0] and (loc[1] + 1 == self.dest[1] or loc[1] - 1 == self.dest[1])) or (loc[1] + 2 == self.dest[1] and (loc[0] + 1 == self.dest[0] or loc[0] - 1 == self.dest[0])) or (loc[1] - 2 == self.dest[1] and (loc[0] + 1 == self.dest[0] or loc[0] - 1 == self.dest[0]))): self.piece = p self.location = loc break elif self.pieceType == 'B': if ((loc[0]+loc[1]) % 2 == (self.dest[0]+self.dest[1]) % 2): self.piece = p self.location = loc break elif self.pieceType == 'Q': if (loc[0] == self.dest[0] or loc[1] == self.dest[1] or ((loc[0]+loc[1]) % 2 == (self.dest[0]+self.dest[1]) % 2)): self.piece = p self.location = loc break elif self.pieceType == 'K': self.piece = p self.location = loc break else: #pawn if self.capture: if ((self.dest[0] == loc[0] + 1 or self.dest[0] == loc[0] - 1) and (self.dest[1] == loc[1] + 1 or self.dest[1] == loc[1] - 1)): self.piece = p self.location = loc break else: if (loc[0] == self.dest[0] and self.dest[1] <= loc[1] + 2 and self.dest[1] >= loc[1] - 2): self.piece = p self.location = loc break else: return
def make_move(self, loc): """Place new piece at `loc`, return list of captured locations.""" captured = board.get_captured(self.piece, loc) piece = Piece(self.piece.colour, board, loc) board.animate_move(captured, piece) for loc in captured: board[loc].flip() board.draw() reversi.draw_info() return captured
def get_next_board_positions(board, player): moves, n = get_valid_moves(board, player) boards = [copy.deepcopy(board) for _ in range(0, n)] i = 0 for m in moves: for position in moves[m]: boards[i].update_board( [Piece(m, position, player.color, player.piece_unicode)]) i += 1 return boards
def getHitlistFor(piece, board, isWhite): hitlist = [] t = Piece.getType(piece) l = board.locationOfPiece(piece) if not l: return [] if t == 'R': hitlist.extend( piecesInHorzView(l, board)) elif t == 'N': hitlist.append( board.pieceAt( (l[0]+1, l[1]+2) ) ) hitlist.append( board.pieceAt( (l[0]-1, l[1]+2) ) ) hitlist.append( board.pieceAt( (l[0]+1, l[1]-2) ) ) hitlist.append( board.pieceAt( (l[0]-1, l[1]-2) ) ) hitlist.append( board.pieceAt( (l[0]+2, l[1]+1) ) ) hitlist.append( board.pieceAt( (l[0]+2, l[1]-1) ) ) hitlist.append( board.pieceAt( (l[0]-2, l[1]+1) ) ) hitlist.append( board.pieceAt( (l[0]-2, l[1]-1) ) ) elif t == 'B': hitlist.extend( piecesInDiagonalView(l, board)) elif t == 'Q': hitlist.extend( piecesInHorzView(l, board)) hitlist.extend( piecesInDiagonalView(l, board)) elif t == 'K': for a in range(-1,2): for b in range(-1,2): if (not (a == 0)) or (not (b == 0)): hitlist.append( board.pieceAt( (l[0]+a, l[1]+b) ) ) else: #pawn if isWhite: hitlist.append( board.pieceAt( (l[0]-1, l[1]+1) ) ) hitlist.append( board.pieceAt( (l[0]+1, l[1]+1) ) ) else: hitlist.append( board.pieceAt( (l[0]-1, l[1]-1) ) ) hitlist.append( board.pieceAt( (l[0]+1, l[1]-1) ) ) s = set(hitlist); return [p for p in s if not p == None and p >= 0 and not Piece.isWhite(p) == isWhite]
def movesForPiece(self, piece): if Piece.isWhite(piece): return [move for move in self.whiteMoves if move.piece == piece] else: return [move for move in self.blackMoves if move.piece == piece]
from board import Board, Piece from player import Player from algorithms import best_move, free_knights_evaluation, euclidean_distance_evaluation, play if __name__ == "__main__": """ WK : White Knight BK : Black Knight """ p1_tokens = [ Piece("WK1", (2, 2), "white", unichr(9822)), Piece("WK2", (3, 3), "white", unichr(9822)) ] p2_tokens = [ Piece("BK1", (2, 3), "black", unichr(9816)), Piece("BK2", (3, 2), "black", unichr(9816)) ] p1 = Player(p1_tokens, "PLAYER1") p2 = Player(p2_tokens, "PLAYER2") pieces = p1_tokens + p2_tokens players = [p1, p2] b = Board(pieces, 6, players) def test_cases(test): """
def __init__(self): self.game = Game() self.Pieces = [Piece(elt) for elt in PIECES]
def drawPiece(color, name, position): square = grid[position[0]][position[1]] square.piece = Piece(color=color, name=name) square.filled = True
def next(self): """return the next piece """ num = self.rng.randint(0, len(PIECES) - 1) return (Piece(PIECES[num]), num)