def __init__(self, root): self.root = root #Knapparnas-container self.menu = Frame(self.root) self.menu.pack() #Ny-omgång knappen self.newGameButton = Button(self.menu, height=1, width=20, text="Ny spelomgång") self.newGameButton.bind("<Button-1>", self.newGame) self.newGameButton.pack(side=LEFT) #highscore-knappen self.highscoreButton = Button(self.menu, height=1, width=20, text="Toggla Highscores") self.highscoreButton.bind("<Button-1>", self.toggleHighscore) self.highscoreButton.pack(side=LEFT) #Highscore-vyn self.highscoreView = Highscore(self.root, "highscores.txt") self.highscoreBool = False #Spel-vyn self.gameView = ChessBoard(self, self.root) self.gameView.container.pack()
def main(): # 初始化棋盘 chessboard = ChessBoard() chessboard.initBoard() # 初始化引擎类 engine = Engine(chessboard) engine.play()
def initBoard(fileLocation): """This method reads a .fen file and returns a board representing its state Args: Complete or partial filepath to .fen file Returns: Board representing contents of file, or None if an error during analysis """ board = ChessBoard() try: infile = open(fileLocation, "r") except FileNotFoundError: print("File", fileLocation, "could not be found, please check your input") return data = infile.read().split('/') if (len(data) != 8): print("Input file", fileLocation, "did not contain enough ranks, please check your input") return counter = 8 for row in data: try: analyzeRow(row, counter, board) except ValueError: print("A bad value was in row", counter) return counter -= 1 infile.close() return board
def test5(): # 初始化棋盘 chessboard = ChessBoard() chessboard.initBoard() # 初始化棋子类 chessman = ChessMan() chessman.Color = 'o' # 初始化引擎类 engine = Engine(chessboard) while 1: # 获取用户下棋位置 userInput = input("请输入用户下棋位置:") engine.userGo(chessman, userInput) # 放置用户下棋位置 chessboard.setChessMan(chessman) # 打印棋盘 RET = engine.isWonMan(chessman) chessboard.printBoard() if RET: print('win') break
def testSkipCells(self): board = ChessBoard() analyzeRow("r6r", 1, board) row = board.getRank(1) self.assertEqual(str(row[0]), "ra1") self.assertEqual(str(row[7]), "rh1")
def testPawnConflictFirstSquareWhite(self): board = ChessBoard() board.addPieceToBoard("Pawn", 4, 2, "White") board.addPieceToBoard("Pawn", 4, 4, "White") moves = board.getSquare(4, 2).getMoveset(board.getRanks()) self.assertEqual(1, len(moves)) self.assertIn((4, 3), moves)
def testPawnBlockedDiffColor(self): board = ChessBoard() board.addPieceToBoard("Pawn", 4, 4, "White") board.addPieceToBoard("Pawn", 4, 5, "Black") moves = board.getSquare(4, 4).getMoveset(board.getRanks()) self.assertEqual(0, len(moves)) self.assertNotIn((4, 5), moves)
def testPawnUnblockedFirstSquareBlack(self): board = ChessBoard() board.addPieceToBoard("Pawn", 4, 7, "Black") moves = board.getSquare(4, 7).getMoveset(board.getRanks()) self.assertEqual(2, len(moves)) self.assertIn((4, 6), moves) self.assertIn((4, 5), moves)
def testSumFourPieces(self): board = ChessBoard() board.addPieceToBoard("Pawn", 5, 2, "White") board.addPieceToBoard("Pawn", 4, 2, "White") board.addPieceToBoard("Pawn", 6, 2, "White") board.addPieceToBoard("King", 5, 1, "White") self.assertEqual(board.calculateAllMoves(0), 8)
def testCenterAllMoves(self): board = ChessBoard() board.addPieceToBoard("Queen", 4, 4, "White") moves = board.getSquare(4, 4).getMoveset(board.getRanks()) self.assertEqual(27, len(moves)) self.assertIn((4, 3), moves) #1 self.assertIn((4, 2), moves) #2 self.assertIn((4, 1), moves) #3 self.assertIn((4, 5), moves) #4 self.assertIn((4, 6), moves) #5 self.assertIn((4, 7), moves) #6 self.assertIn((4, 8), moves) #7 self.assertIn((5, 4), moves) #8 self.assertIn((6, 4), moves) #9 self.assertIn((7, 4), moves) #10 self.assertIn((8, 4), moves) #11 self.assertIn((3, 4), moves) #12 self.assertIn((2, 4), moves) #13 self.assertIn((1, 4), moves) #14 self.assertIn((3, 3), moves) #15 self.assertIn((2, 2), moves) #16 self.assertIn((1, 1), moves) #17 self.assertIn((5, 5), moves) #18 self.assertIn((6, 6), moves) #19 self.assertIn((7, 7), moves) #20 self.assertIn((8, 8), moves) #21 self.assertIn((5, 3), moves) #22 self.assertIn((6, 2), moves) #23 self.assertIn((7, 1), moves) #24 self.assertIn((3, 5), moves) #25 self.assertIn((2, 6), moves) #26 self.assertIn((1, 7), moves) #27
def testPawnBlockedAttackBlackRight(self): board = ChessBoard() board.addPieceToBoard("Pawn", 4, 4, "White") board.addPieceToBoard("Pawn", 5, 5, "Black") moves = board.getSquare(5, 5).getMoveset(board.getRanks()) self.assertIn((4, 4), moves)
def testOppositeNothing(self): board = ChessBoard() board.addPieceToBoard("Pawn", 5, 2, "Black") board.addPieceToBoard("Pawn", 4, 2, "Black") board.addPieceToBoard("Pawn", 6, 2, "Black") board.addPieceToBoard("King", 5, 1, "Black") board.blackCanCastle() self.assertEqual(board.calculateAllMoves(0), 0)
def testSumFourPiecesBlackCanCastle(self): board = ChessBoard() board.addPieceToBoard("Pawn", 5, 2, "Black") board.addPieceToBoard("Pawn", 4, 2, "Black") board.addPieceToBoard("Pawn", 6, 2, "Black") board.addPieceToBoard("King", 4, 1, "Black") board.blackCanCastle() self.assertEqual(board.calculateAllMoves(1), 6)
def __init__(self): self.board = ChessBoard() self.display = Display(self) self.player1 = PolicyPlayer(1, 2, self) self.player2 = PolicyPlayer(2, 1, self) super(PolicyPKPolicyGame, self).__init__(self.board, self.display, self.player1, self.player2)
def __init__(self): self.board = ChessBoard() self.display = TkinterDisplay(self) self.player1 = TkinterHumanPlayer(1, 2, self) self.player2 = PolicyPlayer(2, 1, self) super(HumanPKPolicyGame, self).__init__(self.board, self.display, self.player1, self.player2)
def __init__(self): """Instantiate object.""" self.chessboard = ChessBoard() self.player_white = ChessPlayer('white') self.player_black = ChessPlayer('black') self.setup_board() self.current_player = self.player_white self.moves = 0 self.move_number = 1
def newGame(self, event): if self.highscoreBool: self.highscoreView.container.pack_forget() self.highscoreBool = not self.highscoreBool self.gameView.container.pack_forget() self.gameView.container.destroy() self.gameView = ChessBoard(self, self.root) #Ny instans av ChessBoard. self.gameView.container.pack()
def custom_game(): # 策略玩家自我对弈的动态效果 board = ChessBoard() game = Game(board, None, None, None) game.display = TkinterDisplay(game) game.player1 = PolicyPlayer(1, 2, game) game.player2 = PolicyPlayer(2, 1, game) game.player = game.player1 game.mainloop()
def testCenterAllMovesBlockedDiff(self): board = ChessBoard() board.addPieceToBoard("Bishop", 4, 4, "White") board.addPieceToBoard("Pawn", 3, 3, "Black") board.addPieceToBoard("Pawn", 5, 5, "Black") board.addPieceToBoard("Pawn", 3, 5, "Black") board.addPieceToBoard("Pawn", 5, 3, "Black") moves = board.getSquare(4, 4).getMoveset(board.getRanks()) self.assertEqual(4, len(moves))
def initUI(self): self.button1 = QPushButton("音乐", self) self.button1.clicked.connect(self.button1click) self.clicknum = 1 self.button2 = QPushButton("战况", self) self.button2.clicked.connect(self.button2click) self.button2.move(80, 530) self.button1.move(0, 530) self.win = 0 self.fail = 0 self.step_num = 0 self.chessboard = ChessBoard() # 棋盘类 palette1 = QPalette() # 设置棋盘背景 palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('img/chessboard.jpg'))) self.setPalette(palette1) # self.setStyleSheet("board-image:url(img/chessboard.jpg)") # 不知道这为什么不行 self.setCursor(Qt.PointingHandCursor) # 鼠标变成手指形状 self.sound_piece = QSound("sound/move.wav") # 加载落子音效 self.sound_win = QSound("sound/win.wav") # 加载胜利音效 self.sound_defeated = QSound("sound/defeated.wav") # 加载失败音效 self.resize(WIDTH, HEIGHT) # 固定大小 540*540 self.setMinimumSize(QtCore.QSize(WIDTH, HEIGHT)) self.setMaximumSize(QtCore.QSize(WIDTH, HEIGHT)) self.setWindowTitle("五子棋") # 窗口名称 self.setWindowIcon(QIcon('img/black.png')) # 窗口图标 # self.lb1 = QLabel(' ', self) # self.lb1.move(20, 10) self.black = QPixmap('img/black.png') self.white = QPixmap('img/white.png') self.piece_now = BLACK # 黑棋先行 self.my_turn = True # 玩家先行 self.step = 0 # 步数 self.x, self.y = 1000, 1000 self.mouse_point = LaBel(self) # 将鼠标图片改为棋子 self.mouse_point.setScaledContents(True) # self.mouse_point.setPixmap(self.black) # 加载黑棋 self.mouse_point.setGeometry(270, 270, PIECE, PIECE) self.pieces = [LaBel(self) for i in range(225)] # 新建棋子标签,准备在棋盘上绘制棋子 for piece in self.pieces: piece.setVisible(True) # 图片可视 piece.setScaledContents(True) # 图片大小根据标签大小可变 self.mouse_point.raise_() # 鼠标始终在最上层 self.ai_down = True # AI已下棋,主要是为了加锁,当值是False的时候说明AI正在思考,这时候玩家鼠标点击失效,要忽略掉 mousePressEvent self.setMouseTracking(True) self.show()
def new(self): self.turn = ESide.White self.currentPiece = None self.gameReview = [None, None, None ] #keeps track of the moves played during the game self.bgDraw() self.grid = self.getAndDrawGrid( ) #The key to transit between coords and the screen self.chessboard = ChessBoard() self.run()
def test_get_path(self): self.b = ChessBoard() # check path for a white bishop (C1) -> (E3), blocked by own pawn self.assertFalse(self.b._check_path([7, 2], [-1, 1], [5, 4])) # move black pawn and later white pawn as well as check path for same knight self.b.move('d2', 'd4') self.assertTrue(self.b._check_path([7, 2], [-1, 1], [5, 4])) # check path for white pawn (C3) -> (C5), no obstacles self.assertTrue(self.b._check_path([6, 2], [-1, 0], [4, 2]))
def testAnalyzeEmptyRow(self): board = ChessBoard() analyzeRow("8", 1, board) row = board.getRank(1) self.assertEqual(row[0], 0) self.assertEqual(row[1], 0) self.assertEqual(row[2], 0) self.assertEqual(row[3], 0) self.assertEqual(row[4], 0) self.assertEqual(row[5], 0) self.assertEqual(row[6], 0) self.assertEqual(row[7], 0)
def _simulate_play(self, state): board = ChessBoard(self.game.board.width, self.game.board.height, self.game.board.n_in_row, state) game = Game(board, None, None, None) display = Display(game) player1 = PolicyPlayer(self.other_index, self.index, game) player2 = PolicyPlayer(self.index, self.other_index, game) game.display = display game.player = game.player1 = player1 game.player2 = player2 game.mainloop() return display.win_player is not None and display.win_player.index == self.index
def testCenterAllMovesBlockedSame(self): board = ChessBoard() board.addPieceToBoard("Queen", 4, 4, "White") board.addPieceToBoard("Pawn", 3, 4, "White") board.addPieceToBoard("Pawn", 3, 5, "White") board.addPieceToBoard("Pawn", 4, 5, "White") board.addPieceToBoard("Pawn", 5, 5, "White") board.addPieceToBoard("Pawn", 5, 4, "White") board.addPieceToBoard("Pawn", 5, 3, "White") board.addPieceToBoard("Pawn", 4, 3, "White") board.addPieceToBoard("Pawn", 3, 3, "White") moves = board.getSquare(4, 4).getMoveset(board.getRanks()) self.assertEqual(0, len(moves))
def testKnightCenterAllMoves(self): board = ChessBoard() board.addPieceToBoard("Knight", 4, 4, 0) moves = board.getSquare(4, 4).getMoveset(board.getRanks()) self.assertEqual(8, len(moves)) self.assertIn((2, 3), moves) self.assertIn((2, 5), moves) self.assertIn((3, 6), moves) self.assertIn((3, 2), moves) self.assertIn((5, 2), moves) self.assertIn((5, 6), moves) self.assertIn((6, 3), moves) self.assertIn((6, 5), moves)
def testBlackFirstRow(self): board = ChessBoard() analyzeRow("rnbqkbnr", 1, board) row = board.getRank(1) self.assertEqual(str(row[0]), "ra1") self.assertEqual(str(row[1]), "nb1") self.assertEqual(str(row[2]), "bc1") self.assertEqual(str(row[3]), "qd1") self.assertEqual(str(row[4]), "ke1") self.assertEqual(str(row[5]), "bf1") self.assertEqual(str(row[6]), "ng1") self.assertEqual(str(row[7]), "rh1")
def testKingCenterAllMoves(self): board = ChessBoard() board.addPieceToBoard("King", 4, 4, "White") moves = board.getSquare(4, 4).getMoveset(board.getRanks()) self.assertEqual(8, len(moves)) self.assertIn((3, 3), moves) self.assertIn((3, 4), moves) self.assertIn((3, 5), moves) self.assertIn((4, 5), moves) self.assertIn((5, 5), moves) self.assertIn((5, 4), moves) self.assertIn((5, 3), moves) self.assertIn((4, 3), moves)
def __init__(self, n=15, m=15): ''' 初始化一个游戏 n,m:棋盘的长宽 ''' #棋盘 self._cb = ChessBoard(n, m) #当前先手 self._now = 0 #禁止走的位置,对于每个玩家而言是一个棋盘,棋盘上非-1的位置即是不能走的 self._bad = [ChessBoard(n, m) for _i in [0, 1]] #棋盘长宽 self._n = n self._m = m #分析器 self._ana = Analyzer(self._cb, nexa_range=1) #是否已经有人胜利了 self._got_winner = False #是否检查胜利 self._do_check_winner = True #是否在控制台输出胜利信息 self._out_winner_info = True #当前走了多少步 self._now_steps = 0 #之前走过的所有步骤 self._last_moves = [] #上一次走的位置 self._last_move = None
def initUI(self): self.chessboard = ChessBoard() # 棋盘类 self.chess_manual = [] self.Going = False palette1 = QPalette() # 设置棋盘背景 pix = QtGui.QPixmap('board_' + str(N_LINE) + '.jpg') pix = pix.scaled(WIDTH, HEIGHT) palette1.setBrush(self.backgroundRole(), QtGui.QBrush(pix)) self.setPalette(palette1) # self.setStyleSheet("board-image:url(img/chessboard.jpg)")# 不知道这为什么不行 self.setCursor(Qt.PointingHandCursor) # 鼠标变成手指形状 #self.sound_piece = QSound("sound/luozi.wav")# 加载落子音效 # self.sound_win = QSound("sound/win.wav")# 加载胜利音效 # self.sound_defeated = QSound("sound/defeated.wav")# 加载失败音效 self.resize(WIDTH, HEIGHT) # 固定大小 540*540 self.setMinimumSize(QtCore.QSize(WIDTH, HEIGHT)) self.setMaximumSize(QtCore.QSize(WIDTH, HEIGHT)) self.setWindowTitle("GoBang") # 窗口名称 # self.setWindowIcon(QIcon('img/black.png'))# 窗口图标 # self.lb1 = QLabel(' ', self) # self.lb1.move(20, 10) self.black = QPixmap('black.png') self.white = QPixmap('white.png') self.is_black_do = True # 黑棋回合先行 self.step = 0 # 步数 self.x, self.y = 1000, 1000 self.mouse_point = LaBel(self) # 将鼠标图片改为棋子 self.mouse_point.setScaledContents(True) self.mouse_point.setPixmap(self.black) #加载黑棋 self.mouse_point.setGeometry(270, 270, PIECE, PIECE) self.pieces = [LaBel(self) for i in range(225)] # 新建棋子标签,准备在棋盘上绘制棋子 for piece in self.pieces: piece.setVisible(True) # 图片可视 piece.setScaledContents(True) #图片大小根据标签大小可变 self.mouse_point.raise_() # 鼠标始终在最上层 #self.ai_down = True# AI已下棋,主要是为了加锁,当值是False的时候说明AI正在思考,这时候玩家鼠标点击失效,要忽略掉 mousePressEvent self.setMouseTracking(True) self.show() self.display_it()