def __init__(self, show=True): """ 初始化游戏状态. """ self.chessBoard = Chessboard(show) self.solves = [] self.gameInit()
def __init__(self, show=True): """ 初始化游戏状态. """ self.chessBoard = Chessboard(show) self.solves = [] self.queen = np.zeros((8, 8), dtype=np.int8) self.gameInit()
def __init__(self): self.board = Chessboard() self.player2 = Player("black") self.player1 = Player("white") self.players = (self.player1, self.player2) self.selected_piece = None self.turn = "p1" self.init_ui()
def __init__(self): """ Docstring. """ super(MainWindow, self).__init__() self.setWindowTitle("Chess Fighter 1.0") self.process_list = [] # self.showFullScreen() self.setGeometry(100, 100, 1000, 1000) # self.setMinimumSize(400, 200) self.chessDB = chess_db.Parser(CHESSDB_EXEC) self.boardDock = CustomQDockWidget("Board", self) self.board = Chessboard(self.sendEvent) self.createActions() self.createToolBars() self.createMenus() self.createDockWindows() self.createStatusBar() board_widget = QWidget() layout = QVBoxLayout() # layout.setAlignment(Qt.AlignCenter) layout.addWidget(self.board) # spacer widget for left # left_spacer = QWidget() # left_spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) # spacer widget for right # you can't add the same widget to both left and right. you need two different widgets. # right_spacer = QWidget() # right_spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) # layout.addWidget(self.board_controls) board_widget.setLayout(layout) self.boardDock.setWidget(board_widget) self.setCentralWidget(self.boardDock)
from evaluator import Evaluator from board import Chessboard board = Chessboard() evaluator = Evaluator(board)
#-*- encoding:utf-8 -*- from pieces import * from board import Chessboard cb = Chessboard() cb.prepare() print "Θες να κάνεις restore? " choice = raw_input("(N/O): ").upper() while choice not in "NYOΝΟ": choice = raw_input("N/O MONO!!! : ").upper() if choice in "ΝY": cb.restore() else: #del file fin = open("moves.txt", 'w') fin.close() paiktis = [] a = raw_input("Δωσε ονομα του λευκου:") paiktis.append(a) b = raw_input("Δωσε ονομα του μαυρου:") paiktis.append(b) #---- import pygame
class Game(object): def __init__(self): self.board = Chessboard() self.player2 = Player("black") self.player1 = Player("white") self.players = (self.player1, self.player2) self.selected_piece = None self.turn = "p1" self.init_ui() def init_ui(self): """ Initialize the GUI. """ pygame.init() width = 800 height = 800 self.screen = ui.set_mode((width, height)) ui.set_caption("Super awesome chess game!") """ Check whether the UI has been initialized. If this is not the case, we break. """ if ui.get_surface is None: raise self.draw_board() def draw_board(self): """ Main function that will draw the pieces on the board according to the contents of our chessboard object. """ """ Draw the chess tiles on the board """ red = (255, 0, 0) white = (150, 150, 150) black = (50, 50, 50) height = 800 width = 800 self.screen.fill(red) for x in range(0, width, width / 4): for y in range(0, height, height / 4): pygame.draw.rect(self.screen, white, (x, y, width / 8, height / 8)) for x in range(width / 8, width, width / 4): for y in range(0, height, height / 4): pygame.draw.rect(self.screen, black, (x, y, width / 8, height / 8)) for x in range(0, width, width / 4): for y in range(height / 8, height, height / 4): pygame.draw.rect(self.screen, black, (x, y, width / 8, height / 8)) for x in range(width / 8, width, width / 4): for y in range(height / 8, height, height / 4): pygame.draw.rect(self.screen, white, (x, y, width / 8, height / 8)) ui.update() for square in self.board.board: if self.board.board[square] is None: pass else: piece = self.board.board[square] image = pygame.transform.scale(piece.image, (100, 100)) self.screen.blit(image, piece.coords) ui.update() def passturn(self): if self.turn == "p1": self.turn = "p2" else: self.turn = "p1" def show_possible_squares(self, piece): red = (200, 0, 0) green = (0, 200, 0) for move in piece.possible_moves: coords = self.board.map_square(move) if self.board.is_empty(move): pygame.draw.rect(self.screen, green, coords, 5) else: pygame.draw.rect(self.screen, red, coords, 5) ui.update()
class Game: def __init__(self, show=True): """ 初始化游戏状态. """ self.chessBoard = Chessboard(show) self.solves = [] self.queen = np.zeros((8, 8), dtype=np.int8) self.gameInit() # 重置游戏 def gameInit(self, show=True): """ 重置棋盘. """ self.Queen_setRow = [-1] * 8 self.chessBoard.boardInit(False) ############################################################################## #### 请在以下区域中作答(可自由添加自定义函数) #### #### 输出:self.solves = 八皇后所有序列解的list #### #### 如:[[0,6,4,7,1,3,5,2],]代表八皇后的一个解为 #### #### (0,0),(1,6),(2,4),(3,7),(4,1),(5,3),(6,5),(7,2) #### ############################################################################## # # def check(self, x, y): for i in range(x): if self.queen[i][y] == 1: return False for i, j in zip(range(x - 1, -1, -1), range(y - 1, -1, -1)): if self.queen[i][j] == 1: return False for i, j in zip(range(x - 1, -1, -1), range(y + 1, 8)): if self.queen[i][j] == 1: return False return True def dfs(self, row=0): if row > 7: self.solves.append(copy.deepcopy(self.Queen_setRow)) return for col in range(8): if self.check(row, col): self.queen[row][col] = 1 self.Queen_setRow[row] = col self.dfs(row + 1) self.queen[row][col] = 0 def run(self, row=0): self.dfs(0) # self.solves.append([0,6,4,7,1,3,5,2]) # # ############################################################################## ################# 完成后请记得提交作业 ################# ############################################################################## def showResults(self, result): """ 结果展示. """ self.chessBoard.boardInit(False) for i, item in enumerate(result): if item >= 0: self.chessBoard.setQueen(i, item, False) self.chessBoard.printChessboard(False) def get_results(self): """ 输出结果(请勿修改此函数). return: 八皇后的序列解的list. """ self.run() return self.solves
class Game: def __init__(self, show=True): """ 初始化游戏状态. """ self.chessBoard = Chessboard(show) self.solves = [] self.gameInit() # 重置游戏 def gameInit(self, show=True): """ 重置棋盘. """ self.Queen_setRow = [-1] * 8 self.chessBoard.boardInit(False) ############################################################################## #### 输出:self.solves = 八皇后所有序列解的list #### #### 如:[[0,6,4,7,1,3,5,2],]代表八皇后的一个解为 #### #### (0,0),(1,6),(2,4),(3,7),(4,1),(5,3),(6,5),(7,2) #### ############################################################################## def collide(self, cur_queen, next_col): next_row = rows = len(cur_queen) for row in range(rows): column = cur_queen[row] if abs(column - next_col) in (0, next_row - row): return True return False def solve(self, num, cur_queen=[]): for pos in range(num): # 八皇后的数量N=0, 1, 2, 3, 4, 5, 6 , 7 你要在哪一列放置皇后 # 如果不冲突,则递归构造棋盘。 if not self.collide(cur_queen, pos): # 回溯法的体现 # 如果棋盘状态state已经等于num-1,即到达倒数第二行,而这时最后一行皇后又没冲突,直接yield,打出其位置(pos, ) if len(cur_queen) == num - 1: # cur_queen=() yield [ pos, ] else: for result in self.solve(num, cur_queen + [ pos, ]): yield [ pos, ] + result def run(self, row=0): solutions = self.solve(8) self.solves = [] for idx, solution in enumerate(solutions): self.solves.append(solution) def showResults(self, result): """ 结果展示. """ self.chessBoard.boardInit(False) for i, item in enumerate(result): if item >= 0: self.chessBoard.setQueen(i, item, False) self.chessBoard.printChessboard(False) def get_results(self): """ 输出结果(请勿修改此函数). return: 八皇后的序列解的list. """ self.run() return self.solves