コード例 #1
0
def minmax(turn, node, depth):
    if depth == 0:
        return None, gridValue(node)
    else:
        if colors[turn] == userColor:
            for piece in node[userColor]:
                possibleMoves = piece.getPossibleMoves(node)
                bestChild = [None, 100000000]
                for move in possibleMoves:
                    childGrid, mappedMove = Move.transferMoveToNewGrid(
                        node, move)
                    childGrid = mappedMove.apply(childGrid)
                    _, value = minmax(1 - turn, childGrid, depth - 1)
                    if value < bestChild[1]:
                        bestChild = [move, value]
                return bestChild
        if colors[turn] == aiColor:
            for piece in node[aiColor]:
                possibleMoves = piece.getPossibleMoves(node)
                bestChild = [None, -100000000]
                for move in possibleMoves:
                    childGrid, mappedMove = Move.transferMoveToNewGrid(
                        node, move)
                    childGrid = mappedMove.apply(childGrid)
                    _, value = minmax(1 - turn, childGrid, depth - 1)
                    if value > bestChild[1]:
                        bestChild = [move, value]
                return bestChild
コード例 #2
0
ファイル: piecetests.py プロジェクト: alesegovia/ceibal-chess
	def test_castling_forbidden_king_moved(self):
		'''Test that castling is forbidden once the king moved.'''
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		move = Move((4,7),(5,7))
		self.assertTrue(move in moves)
		move.perform(board)

		moves = king.get_moves((4, 7), board)
		self.assertFalse(Castling("left",board.white) in moves)
		self.assertFalse(Castling("right",board.white) in moves)
コード例 #3
0
ファイル: piecetests.py プロジェクト: alesegovia/ceibal-chess
	def test_castling_permitted_after_undo(self):
		board = Board(10, 10)
		king = King(board.white)
		rook_l = Rook(board.white)
		rook_r = Rook(board.white)

		board[0, 7].piece = rook_l
		board[7, 7].piece = rook_r
		board[4, 7].piece = king

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)

		move = Move((0,7),(1,7))
		self.assertTrue(move in rook_l.get_moves((0,7),board))
		move.perform(board)
		move.undo(board)

		moves = king.get_moves((4, 7), board)
		self.assertTrue(Castling("left",board.white) in moves)
		self.assertTrue(Castling("right",board.white) in moves)
コード例 #4
0
def main():
    checkmate = False
    # userColor = input("whites/blacks? ")
    turn = 0
    grid = generateGrid()
    printGrid(grid)
    while not checkmate:
        if userColor == colors[turn]:
            move = False
            while not move:
                move = input("Your turn: ")
                move = Move.ParseMove(move, grid, colors[turn])
            grid = move.apply(grid)
            printGrid(grid)
        else:
            move, value = minmax(turn, grid, 2)
            print(move, value)
        turn = 1 - turn
コード例 #5
0
	def move(self, move):
		'''Write a player's move to GNU Chess. Return the engine's move.'''
		move_str = self.move_to_gnuchess(move)

		print "Calling GNU Chess with move:", move_str
		self.fout.write(move_str + "\n")
		self.fout.flush()
		l = self.fin.readline()
		while l.find("My move is") == -1:
			if l.find("Illegal move") != -1:
				raise Exception("Illegal move")
			l = self.fin.readline()
		ans = l.split()[3]
		print ans
		if len(ans) == 4:
			return Move(self.gnuchess_to_coords(ans[:2]), \
				self.gnuchess_to_coords(ans[2:]))
		elif len(ans) == 5:
			return Crowning(self.gnuchess_to_coords(ans[:2]), \
				self.gnuchess_to_coords(ans[2:4]), \
				self.decode_piece(ans[4]))
		else:
			raise Exception("IA Error, unknown answer: " + ans)